fts_core/models/
config.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4/// Flow trading takes place within a context. This config describes this context.
5#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
6#[serde(from = "RawConfig", into = "RawConfig")]
7pub struct Config {
8    /// The rate to use for converting rates into batch quantities.
9    pub trade_rate: Duration,
10}
11
12// To seamlessly (de)serialize, we create a "raw" version of of our struct
13// that contains only primitive values. We tell Serde to use the raw version
14// to handle (de)serialization, then call .into() to get our rich version.
15// It's a little verbose, but fairly clean.
16// Note that the u64<>u32 conversion is because JSON serialization does not
17// support 64 bit integers.
18
19#[derive(Serialize, Deserialize)]
20pub struct RawConfig {
21    pub trade_rate: u32,
22}
23
24impl From<RawConfig> for Config {
25    fn from(value: RawConfig) -> Self {
26        Self {
27            trade_rate: Duration::from_secs(value.trade_rate as u64),
28        }
29    }
30}
31
32impl From<Config> for RawConfig {
33    fn from(value: Config) -> Self {
34        Self {
35            trade_rate: value.trade_rate.as_secs() as u32,
36        }
37    }
38}