-
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.
- Loading branch information
Showing
12 changed files
with
96 additions
and
11 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::config::Config; | ||
use anyhow::{anyhow, Error}; | ||
use mfm_machine::state::context::Context; | ||
use serde_derive::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum ConfigSource { | ||
File(String), | ||
} | ||
|
||
impl Context for ConfigSource { | ||
fn read(&self) -> Result<Value, Error> { | ||
serde_json::to_value(self).map_err(|e| anyhow!(e)) | ||
} | ||
|
||
fn write(&mut self, _: &Value) -> Result<(), Error> { | ||
// do nothing | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct ReadConfig { | ||
pub config_source: ConfigSource, | ||
pub config: Config, | ||
} |
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,6 +1,6 @@ | ||
pub mod config; | ||
pub mod contexts; | ||
pub mod hidden; | ||
pub mod password; | ||
pub mod states; | ||
pub mod tasks; | ||
|
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,5 +1,55 @@ | ||
use crate::config::Config; | ||
use mfm_machine_derive::StateMetadataReqs; | ||
|
||
use mfm_machine::state::{ | ||
context::ContextWrapper, DependencyStrategy, Label, StateHandler, StateMetadata, StateResult, | ||
Tag, | ||
}; | ||
|
||
use crate::contexts; | ||
|
||
#[derive(Debug, Clone, PartialEq, StateMetadataReqs)] | ||
pub struct ReadConfig { | ||
pub config: Config, | ||
label: Label, | ||
tags: Vec<Tag>, | ||
depends_on: Vec<Tag>, | ||
depends_on_strategy: DependencyStrategy, | ||
} | ||
|
||
impl ReadConfig { | ||
fn new() -> Self { | ||
Check warning on line 19 in mfm_core/src/states/mod.rs GitHub Actions / clippyassociated function `new` is never used
|
||
Self { | ||
label: Label::new("read_config").unwrap(), | ||
tags: vec![Tag::new("setup").unwrap()], | ||
depends_on: vec![Tag::new("config").unwrap()], | ||
depends_on_strategy: DependencyStrategy::Latest, | ||
} | ||
} | ||
} | ||
|
||
impl StateHandler for ReadConfig { | ||
fn handler(&self, context: ContextWrapper) -> StateResult { | ||
let value = context.lock().unwrap().read().unwrap(); | ||
let data: contexts::ConfigSource = serde_json::from_value(value).unwrap(); | ||
println!("data: {:?}", data); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use mfm_machine::state::{context::wrap_context, StateHandler}; | ||
|
||
use crate::contexts::ConfigSource; | ||
|
||
use super::ReadConfig; | ||
|
||
#[test] | ||
fn test_readconfig_from_source_file() { | ||
let state = ReadConfig::new(); | ||
let ctx_input = wrap_context(ConfigSource::File("test_config.toml".to_string())); | ||
let result = state.handler(ctx_input); | ||
assert!(result.is_ok()) | ||
} | ||
|
||
// TODO: add a test transitioning between states and contexts. | ||
} |