-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generalize context for states and add state machine execution
- Loading branch information
Showing
6 changed files
with
76 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,31 @@ | ||
use anyhow::{anyhow, Error}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub trait Context { | ||
type Output: Deserialize<'static>; | ||
type Input: Serialize; | ||
|
||
fn read(&self) -> Self::Output; | ||
fn write(&self, ctx_input: &Self::Input); | ||
fn read<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>; | ||
fn write<T: Serialize>(&mut self, data: &T) -> Result<(), Error>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ContextInput {} | ||
|
||
#[derive(Debug)] | ||
pub struct ContextOutput {} | ||
|
||
impl Context for ContextInput { | ||
type Output = String; | ||
type Input = String; | ||
|
||
fn read(&self) -> Self::Output { | ||
"hello".to_string() | ||
} | ||
pub struct RawContext { | ||
data: String, | ||
} | ||
|
||
fn write(&self, ctx_input: &Self::Input) { | ||
let _x = ctx_input; | ||
impl RawContext { | ||
pub fn new() -> Self { | ||
Self { | ||
data: "{}".to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Context for ContextOutput { | ||
type Input = String; | ||
type Output = String; | ||
|
||
fn read(&self) -> Self::Output { | ||
"hello".to_string() | ||
impl Context for RawContext { | ||
fn read<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error> { | ||
serde_json::from_str(&self.data).map_err(|e| anyhow!("error on deserialize: {}", e)) | ||
} | ||
|
||
fn write(&self, ctx_input: &Self::Input) { | ||
let _x = ctx_input; | ||
fn write<T: Serialize>(&mut self, data: &T) -> Result<(), Error> { | ||
self.data = serde_json::to_string(data)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters