Skip to content

Commit

Permalink
feat(preview): implement previewing an action
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsan committed Jun 5, 2023
1 parent 27f0ad6 commit 37aad75
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# TODO: Activate `deny warnings`
# rustflags = "-C target-cpu=native -D warnings"
rustflags = "-C target-cpu=native"
incremental = true
# incremental = true

# TODO: Introduced for `cargo public-api`,
# remove when it's working on sparse registry
Expand Down
17 changes: 4 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,10 @@
}
},
"args": [
"filter",
"empty",
"-r",
"-m",
"4",
"-l",
"C:\\Users\\dailyuse\\dev-src\\organize\\tests",
"--ignore-path",
".git",
"--ignore-name",
"toml",
"--ignore-path",
"fixtures"
"run",
"config",
"--paths",
"config\\empty.yaml",
],
"cwd": "${workspaceFolder}"
},
Expand Down
18 changes: 18 additions & 0 deletions config/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rules:
- name: Recursively trash empty entries
enabled: true
locations:
- !recursive
path: C:\Users\dailyuse\dev-src\
max_depth: 10
target: both
filter_groups:
- filters:
- !empty
results: include
match: all
actions:
- mode: preview
action: !trash
tags:
- !custom Test::Filter::EmptyEntries
12 changes: 12 additions & 0 deletions crates/organize-rs_core/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type ActionClosure<'a, C> =
pub enum ActionResultKind {
/// A preview of a to be executed action
Preview {
/// message to be printed
msg: String,
/// file or directory path the action should be executed on
path: PathBuf,
/// corresponding action
Expand Down Expand Up @@ -67,6 +69,16 @@ pub enum ActionApplicationKind {
UserInput,
}

impl ActionApplicationKind {
/// Returns `true` if the action application kind is [`Preview`].
///
/// [`Preview`]: ActionApplicationKind::Preview
#[must_use]
pub fn is_preview(&self) -> bool {
matches!(self, Self::Preview)
}
}

#[derive(Debug, Clone, Deserialize, Serialize, Display)]
pub struct ActionContainer {
pub mode: ActionApplicationKind,
Expand Down
8 changes: 8 additions & 0 deletions crates/organize-rs_core/src/actions/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl ActionKind {
fn action_no_action<C: ClientState>(&self) -> ActionClosure<C> {
Box::new(|entry, _preview| {
Ok(ActionResultKind::Preview {
msg: format!("(Preview) No action: {}", entry.path().display()),
path: entry.path(),
action: self.to_owned(),
})
Expand All @@ -66,6 +67,7 @@ impl ActionKind {
Box::new(|entry, preview| {
if preview {
Ok(ActionResultKind::Preview {
msg: format!("(Preview) Trash: {}", entry.path().display()),
path: entry.path(),
action: self.to_owned(),
})
Expand All @@ -81,6 +83,7 @@ impl ActionKind {
Box::new(|entry, preview| {
if preview {
Ok(ActionResultKind::Preview {
msg: format!("(Preview) Delete: {}", entry.path().display()),
path: entry.path(),
action: self.to_owned(),
})
Expand All @@ -96,6 +99,11 @@ impl ActionKind {
Box::new(move |entry, preview| {
if preview {
Ok(ActionResultKind::Preview {
msg: format!(
"(Preview) Symlink: {} -> {}",
dst.display(),
entry.path().display()
),
path: entry.path(),
action: self.to_owned(),
})
Expand Down
11 changes: 11 additions & 0 deletions crates/organize-rs_core/src/actors/action_applicator.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
use crate::actions::ActionApplicationCollection;

#[derive(Debug, Default)]
pub struct ActionApplicator {
actions: ActionApplicationCollection,
}

impl ActionApplicator {
pub fn new(actions: ActionApplicationCollection) -> Self {
ActionApplicator { actions }
}
}
6 changes: 5 additions & 1 deletion crates/organize-rs_core/src/actors/location_walker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use itertools::Itertools;
use jwalk::{ClientState, DirEntry};
use std::{fmt::Display, fs::FileType, path::Path, vec::IntoIter};
use std::{fmt::Display, fs::FileType, path::Path, slice::Iter, vec::IntoIter};

use crate::{
error::{OrganizeResult, WalkerErrorKind},
Expand Down Expand Up @@ -32,6 +32,10 @@ impl DirEntryData {
});
println!("Total entry count: {count}");
}

pub(crate) fn iter(&self) -> Iter<'_, jwalk::DirEntry<((), ())>> {
self.0.iter()
}
}

impl Display for DirEntryData {
Expand Down
11 changes: 1 addition & 10 deletions crates/organize-rs_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod actions;
pub mod actors;
pub mod aliases;
pub mod concurrency;
pub mod config;
Expand All @@ -11,15 +12,5 @@ pub mod py_config;
pub mod rules;
pub mod runner;
pub mod ser_de;
pub mod actors;
pub mod state;
pub mod tags;









96 changes: 77 additions & 19 deletions crates/organize-rs_core/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,44 @@ use std::{collections::HashSet, path::Path};
use itertools::Itertools;

use crate::{
actions::{ActionApplicationKind, ActionResultKind},
actors::{filter_applicator::FilterApplicator, location_walker::LocationWalker},
config::OrganizeConfig,
state::{HandleConflicts, Init, Inspect, ProcessingState, Start},
error::OrganizeResult,
state::{
ActionApplication, ActionPreview, ConflictHandling, Filtering, Initialize, Inspection,
ProcessingStage,
},
tags::{Tag, TagCollection},
};

use std::iter::FromIterator;

pub struct Runner<S>
where
S: ProcessingState,
S: ProcessingStage,
{
configs: Vec<OrganizeConfig>,
extra: S,
}

impl Runner<Init> {
pub fn load_configs(paths: &[impl AsRef<Path>]) -> Runner<Start> {
impl Runner<Initialize> {
pub fn load_configs(paths: &[impl AsRef<Path>]) -> Runner<Filtering> {
let mut configs = vec![];
paths.into_iter().for_each(|path| {
paths.iter().for_each(|path| {
let config = OrganizeConfig::load_from_file(path);
configs.push(config);
});

Runner::<Start> {
Runner::<Filtering> {
configs,
extra: Start::default(),
extra: Filtering::default(),
}
}
}

impl Runner<Start> {
pub fn apply_filters(self, tags: Vec<Tag>) -> Runner<Inspect> {
impl Runner<Filtering> {
pub fn apply_filters(self, tags: Vec<Tag>) -> Runner<Inspection> {
let mut entries = vec![];
self.configs.iter().for_each(|config| {
config.rules().iter().for_each(|rule| {
Expand All @@ -59,9 +64,9 @@ impl Runner<Start> {
})
});

Runner::<Inspect> {
Runner::<Inspection> {
configs: self.configs,
extra: Inspect::with_entries(entries),
extra: Inspection::with_entries(entries),
}
}

Expand All @@ -77,20 +82,73 @@ impl Runner<Start> {
}
}

impl Runner<Inspect> {
pub fn handle_conflicts(self) -> Runner<HandleConflicts> {
let entries = self.extra.entries();

Runner::<HandleConflicts> {
impl Runner<Inspection> {
pub fn finish_inspection(self) -> Runner<ActionPreview> {
Runner::<ActionPreview> {
configs: self.configs,
extra: HandleConflicts::with_entries(entries),
extra: ActionPreview::with_entries(self.extra.entries()),
}
}

pub fn inspect_entries(self) -> Runner<Inspect> {
pub fn inspect_entries(self) -> Runner<Inspection> {
self.extra.print_entries();
self
}
}

impl Runner<HandleConflicts> {}
impl Runner<ActionPreview> {
pub fn preview_actions(self) -> OrganizeResult<()> {
// * if action::mode == `is_preview()` we return Runner<Reporting>
// * and only println!() what an action might do
// * else we continue to Runner<ActionApplication>
self.extra.entries().iter().for_each(|(rule, entry)| {
rule.actions().iter().for_each(|action_container| {
entry.iter().for_each(|entry| {
match action_container.action.get_action()(
entry,
true, // * is always true, as it's preview
// ! for application we can use:
// ! `action_container.mode.is_preview()`
) {
Ok(ActionResultKind::Preview {
msg,
path: _,
action: _,
}) => println!("{msg}"),
Err(err) => eprintln!("{err}"),
_ => (),
}
})
})
});
Ok(())
}

pub fn ask_confirmation(self) -> OrganizeResult<Runner<ActionApplication>> {
todo!()
}
}

impl Runner<ActionApplication> {
pub fn apply_actions(self) -> OrganizeResult<Runner<ActionApplication>> {
todo!()
}

pub fn check_conflicts(self) -> Runner<ConflictHandling> {
todo!()
}
}

// pub fn handle_conflicts(self) -> Runner<HandleConflicts> {
// let entries = self.extra.entries();

// Runner::<HandleConflicts> {
// configs: self.configs,
// extra: HandleConflicts::with_entries(entries),
// }
// }
impl Runner<ConflictHandling> {
pub fn view_conflicts(self) -> Runner<ActionPreview> {
todo!()
}
}
Loading

0 comments on commit 37aad75

Please sign in to comment.