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 4477005
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 deletions.
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 with_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 with_path(self, path: &'static str) -> Self {
Self {
target: self.target,
path: Path::from_static(path),
args: PathArgs::new(),
}
}

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

args.0.push((Arc::from(key.into()), 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().with_target(*tgt)),
plus_one.into_task(Context::new().with_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().with_target(*tgt)),
plus_one.into_task(Context::new().with_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().with_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().with_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().with_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().with_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().with_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().with_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()
.with_target(State {
counters: [("a".to_string(), 2), ("b".to_string(), 1)].into(),
})
.with_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().with_target(1));

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

0 comments on commit 4477005

Please sign in to comment.