Skip to content

Commit

Permalink
add readconfig state
Browse files Browse the repository at this point in the history
  • Loading branch information
willyrgf committed Dec 17, 2023
1 parent 1cecbcc commit d65cf6c
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 11 deletions.
5 changes: 5 additions & 0 deletions mfm_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
mfm_machine_derive = { path = "../mfm_machine_derive" }
mfm_machine = { path = "../mfm_machine" }
serde = "1.0.193"
zeroize = "1.7.0"
serde_json = "1.0.108"
serde_derive = "1.0.193"
anyhow = "1.0.75"
2 changes: 1 addition & 1 deletion mfm_core/src/config/authentication/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod wallet;

use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use self::wallet::Wallet;

Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/config/authentication/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use crate::password::{deserialize_safe_password, SafePassword};

Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/config/dexes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

pub mod authentication;
pub mod dexes;
Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/config/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::token::Token;
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/config/token.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
28 changes: 28 additions & 0 deletions mfm_core/src/contexts/mod.rs
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,
}
2 changes: 1 addition & 1 deletion mfm_core/src/hidden/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// ! A wrapper to conceal secrets when output into logs or displayed.
//
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use std::{fmt, ops::DerefMut};

/// A simple struct with a single inner value to wrap content of any type.
Expand Down
2 changes: 1 addition & 1 deletion mfm_core/src/lib.rs
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;

4 changes: 3 additions & 1 deletion mfm_core/src/password/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hidden::Hidden;
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};
use std::{error::Error, fmt, str::FromStr};
use zeroize::Zeroize;

Expand Down Expand Up @@ -54,6 +54,8 @@ pub fn deserialize_safe_password<'de, D>(deserializer: D) -> Result<SafePassword
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;

let password: String = Deserialize::deserialize(deserializer)?;
Ok(SafePassword::from(password))
}
54 changes: 52 additions & 2 deletions mfm_core/src/states/mod.rs
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

View workflow job for this annotation

GitHub Actions / clippy

associated function `new` is never used

warning: associated function `new` is never used --> mfm_core/src/states/mod.rs:19:8 | 18 | impl ReadConfig { | --------------- associated function in this implementation 19 | fn new() -> Self { | ^^^ | = note: `#[warn(dead_code)]` on by default
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.
}

0 comments on commit d65cf6c

Please sign in to comment.