Skip to content

Commit

Permalink
Add PathArgs to Context
Browse files Browse the repository at this point in the history
Also refactor Context public methods
  • Loading branch information
pipex committed Nov 5, 2024
1 parent 5682112 commit f80578f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ fn plus_two(counter: Update<State, i32>, tgt: Target<State, i32>, Path(name): Pa
// A compound job returns a list of tasks that need to be executed
// to achieve a certain goal
vec![
plus_one.into_task(Context::new().with_target(*tgt).with_arg("name", &name)),
plus_one.into_task(Context::new().with_target(*tgt).with_arg("name", &name)),
plus_one.into_task(Context::new().target(*tgt).arg("name", &name)),
plus_one.into_task(Context::new().target(*tgt).arg("name", &name)),
]
}

Expand Down
37 changes: 18 additions & 19 deletions src/extract/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, i32> =
View::from_system(&system, &Context::default().with_path("/numbers/one")).unwrap();
View::from_system(&system, &Context::new().path("/numbers/one")).unwrap();

assert_eq!(view.as_ref(), Some(&1));

Expand Down Expand Up @@ -346,14 +346,13 @@ mod tests {

assert!(View::<State, i32>::from_system(
&system,
&Context::default().with_path("/numbers/one/two"),
)
.is_err());
assert!(View::<State, i32>::from_system(
&system,
&Context::default().with_path("/none/two"),
&Context::default().path("/numbers/one/two"),
)
.is_err());
assert!(
View::<State, i32>::from_system(&system, &Context::default().path("/none/two"),)
.is_err()
);
}

#[test]
Expand All @@ -367,7 +366,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, i32> =
View::from_system(&system, &Context::default().with_path("/numbers/three")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/three")).unwrap();

assert_eq!(view.as_ref(), None);

Expand Down Expand Up @@ -395,7 +394,7 @@ mod tests {
let system = System::from(state);

let mut view: Create<State, i32> =
Create::from_system(&system, &Context::default().with_path("/numbers/three")).unwrap();
Create::from_system(&system, &Context::default().path("/numbers/three")).unwrap();
*view = 3;

// Get the list changes to the view
Expand All @@ -421,7 +420,7 @@ mod tests {

assert!(Create::<State, i32>::from_system(
&system,
&Context::default().with_path("/none/three")
&Context::default().path("/none/three")
)
.is_err());
}
Expand All @@ -437,7 +436,7 @@ mod tests {
let system = System::from(state);

let mut view: Update<State, i32> =
Update::from_system(&system, &Context::default().with_path("/numbers/two")).unwrap();
Update::from_system(&system, &Context::default().path("/numbers/two")).unwrap();
*view = 3;

// Get the list changes to the view
Expand All @@ -463,12 +462,12 @@ mod tests {

assert!(Update::<State, i32>::from_system(
&system,
&Context::default().with_path("/numbers/three")
&Context::default().path("/numbers/three")
)
.is_err());
assert!(Update::<State, i32>::from_system(
&system,
&Context::default().with_path("/none/three")
&Context::default().path("/none/three")
)
.is_err());
}
Expand All @@ -484,7 +483,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, i32> =
View::from_system(&system, &Context::default().with_path("/numbers/three")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/three")).unwrap();

assert_eq!(view.as_ref(), None);

Expand Down Expand Up @@ -513,7 +512,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, i32> =
View::from_system(&system, &Context::default().with_path("/numbers/one")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/one")).unwrap();

// Delete the value
view.delete();
Expand All @@ -538,7 +537,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, String> =
View::from_system(&system, &Context::default().with_path("/numbers/1")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/1")).unwrap();

assert_eq!(view.as_ref(), Some(&"two".to_string()));

Expand All @@ -565,7 +564,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, String> =
View::from_system(&system, &Context::default().with_path("/numbers/2")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/2")).unwrap();

assert_eq!(view.as_ref(), None);
view.create("three".to_string());
Expand All @@ -590,7 +589,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, String> =
View::from_system(&system, &Context::default().with_path("/numbers/1")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/1")).unwrap();

// Remove the second element
view.delete();
Expand All @@ -617,7 +616,7 @@ mod tests {
let system = System::from(state);

let mut view: View<State, String> =
View::from_system(&system, &Context::default().with_path("/numbers/2")).unwrap();
View::from_system(&system, &Context::default().path("/numbers/2")).unwrap();

// Remove the third element
view.delete();
Expand Down
8 changes: 7 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ impl AsRef<Pointer> for Path {
pub(crate) struct PathArgs(pub Vec<(Arc<str>, String)>);

impl PathArgs {
pub fn new(params: matchit::Params) -> Self {
pub fn new() -> Self {
PathArgs(Vec::new())
}
}

impl<'k, 'v> From<matchit::Params<'k, 'v>> for PathArgs {
fn from(params: matchit::Params) -> PathArgs {
let params: Vec<(Arc<str>, String)> = params
.iter()
.map(|(k, v)| (Arc::from(k), String::from(v)))
Expand Down
37 changes: 31 additions & 6 deletions src/task/context.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
use crate::path::Path;
use std::sync::Arc;

use crate::path::{Path, PathArgs};

pub struct Context<S> {
pub target: Option<S>,
pub path: Path,
pub(crate) target: Option<S>,
pub(crate) path: Path,
pub(crate) args: PathArgs,
}

impl<S> Default for Context<S> {
fn default() -> Self {
Context {
target: None,
path: Path::default(),
args: PathArgs::new(),
}
}
}

impl<S> Context<S> {
pub fn from_target(target: S) -> Self {
pub fn new() -> Self {
Self::default()
}

pub fn target(self, target: S) -> Self {
Self {
target: Some(target),
path: Path::default(),
path: self.path,
args: self.args,
}
}

pub fn with_path(self, path: &'static str) -> Self {
// This will be used by the planner
#[allow(dead_code)]
pub(crate) fn path(self, path: &'static str) -> Self {
Self {
target: self.target,
path: Path::from_static(path),
args: self.args,
}
}

pub fn arg(self, key: impl AsRef<str>, value: impl Into<String>) -> Self {
let Self {
target,
path,
mut args,
} = self;

args.0.push((Arc::from(key.as_ref()), value.into()));

Self { target, path, args }
}
}

impl<S: Clone> Clone for Context<S> {
fn clone(&self) -> Self {
Context {
target: self.target.clone(),
path: self.path.clone(),
args: self.args.clone(),
}
}
}
6 changes: 3 additions & 3 deletions src/task/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<S> Domain<S> {
.at(path)
.map(|matched| {
(
PathArgs::new(matched.params),
PathArgs::from(matched.params),
matched
.value
.iter()
Expand Down Expand Up @@ -156,8 +156,8 @@ mod tests {
}

vec![
plus_one.into_task(Context::from_target(*tgt)),
plus_one.into_task(Context::from_target(*tgt)),
plus_one.into_task(Context::new().target(*tgt)),
plus_one.into_task(Context::new().target(*tgt)),
]
}

Expand Down
25 changes: 13 additions & 12 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ mod tests {
}

vec![
plus_one.into_task(Context::from_target(*tgt)),
plus_one.into_task(Context::from_target(*tgt)),
plus_one.into_task(Context::new().target(*tgt)),
plus_one.into_task(Context::new().target(*tgt)),
]
}

Expand All @@ -209,7 +209,7 @@ mod tests {
fn it_allows_to_dry_run_tasks() {
let system = System::from(0);
let job = plus_one.into_job();
let task = job.into_task(Context::from_target(1));
let task = job.into_task(Context::new().target(1));

// Get the list of changes that the action performs
let changes = task.dry_run(&system).unwrap();
Expand All @@ -226,7 +226,7 @@ mod tests {
fn it_allows_to_dry_run_composite_tasks() {
let system = System::from(0);
let job = plus_two.into_job();
let task = job.into_task(Context::from_target(2));
let task = job.into_task(Context::new().target(2));

// Get the list of changes that the method performs
let changes = task.dry_run(&system).unwrap();
Expand All @@ -243,7 +243,7 @@ mod tests {
#[tokio::test]
async fn it_allows_to_run_composite_tasks() {
let mut system = System::from(0);
let task = plus_two.into_task(Context::from_target(2));
let task = plus_two.into_task(Context::new().target(2));

// Run the action
task.run(&mut system).await.unwrap();
Expand All @@ -257,7 +257,7 @@ mod tests {
#[tokio::test]
async fn it_runs_async_actions() {
let mut system = System::from(0);
let task = plus_one.into_task(Context::from_target(1));
let task = plus_one.into_task(Context::new().target(1));

// Run the action
task.run(&mut system).await.unwrap();
Expand All @@ -272,7 +272,7 @@ mod tests {
async fn it_allows_extending_actions_with_effect() {
let mut system = System::from(0);
let job = plus_one_async.into_job();
let task = job.into_task(Context::from_target(1));
let task = job.into_task(Context::new().target(1));

// Run the action
task.run(&mut system).await.unwrap();
Expand All @@ -285,7 +285,7 @@ mod tests {
#[tokio::test]
async fn it_allows_actions_returning_errors() {
let mut system = System::from(1);
let task = plus_one_async.into_task(Context::from_target(1));
let task = plus_one_async.into_task(Context::new().target(1));

let res = task.run(&mut system).await;
assert!(res.is_err());
Expand Down Expand Up @@ -319,10 +319,11 @@ mod tests {
let mut system = System::from(state);
let task = update_counter.into_job();
let action = task.into_task(
Context::from_target(State {
counters: [("a".to_string(), 2), ("b".to_string(), 1)].into(),
})
.with_path("/counters/a"),
Context::new()
.target(State {
counters: [("a".to_string(), 2), ("b".to_string(), 1)].into(),
})
.path("/counters/a"),
);

// Run the action
Expand Down
2 changes: 1 addition & 1 deletion tests/library_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn my_task_effect(mut counter: Update<i32>, tgt: Target<i32>) -> Effect<Update<i
fn it_allows_to_dry_run_tasks() {
let system = System::from(0);
let job = my_task_effect.into_job();
let action = job.into_task(Context::from_target(1));
let action = job.into_task(Context::new().target(1));

// Get the list of changes that the action performs
let changes = action.dry_run(&system).unwrap();
Expand Down

0 comments on commit f80578f

Please sign in to comment.