From 9d1bd12570e3e1d4b83f2b5c2abd19a7ce1fb42c Mon Sep 17 00:00:00 2001
From: Nikos Kontakis <wirednkod@gmail.com>
Date: Tue, 5 Sep 2023 23:22:17 +0300
Subject: [PATCH] latest changes

---
 crates/configuration/src/network.rs      | 38 ++++++++++++++++++++----
 crates/configuration/src/relaychain.rs   |  2 +-
 crates/configuration/src/shared/node.rs  |  2 +-
 crates/configuration/src/shared/types.rs | 28 ++++++-----------
 4 files changed, 43 insertions(+), 27 deletions(-)

diff --git a/crates/configuration/src/network.rs b/crates/configuration/src/network.rs
index 16f3ffe..48b4e1c 100644
--- a/crates/configuration/src/network.rs
+++ b/crates/configuration/src/network.rs
@@ -180,11 +180,37 @@ impl NetworkConfigBuilder<Initial> {
         Self::default()
     }
 
-    pub fn toml_to_network(self, path: &str) -> Result<NetworkConfig, toml::ser::Error> {
+    pub fn toml_to_network(path: &str) -> Result<NetworkConfig, toml::ser::Error> {
         let file_str = fs::read_to_string(path).unwrap();
-        let toml: NetworkConfig = toml::from_str(&file_str).unwrap();
+        let mut network_config: NetworkConfig = toml::from_str(&file_str).unwrap();
+
+        if network_config.relaychain.is_some() {
+            for node in network_config.relaychain.as_mut().unwrap().nodes.iter() {
+                node.command = Some(
+                    network_config
+                        .relaychain
+                        .unwrap()
+                        .default_command()
+                        .clone()
+                        .unwrap()
+                        .clone(),
+                );
+                println!("{:?}", node);
+            }
+        }
+
+        //     if toml.relaychain.default_command.is_some() {
+        //           for node in toml.relaychain.nodes.iter_mut() {
+        //                  node.command = toml.relaychain.default_command.clone();
+        //           }
+        //     }
+
+        //     /// repeat for all defaults
+
+        //     /// do the same for parachain but with a for to iterate over the vec of parachains
+
         // TODO: (nikos) Add error
-        Ok(toml)
+        Ok(network_config)
     }
 
     /// Set the relay chain using a nested [`RelaychainConfigBuilder`].
@@ -864,9 +890,9 @@ mod tests {
 
     #[test]
     fn the_toml_config_should_be_imported_and_match_a_network() {
-        let toml_to_network = NetworkConfigBuilder::new()
-            .toml_to_network("./testing/snapshots/0000-small-network.toml")
-            .unwrap();
+        let toml_to_network =
+            NetworkConfigBuilder::toml_to_network("./testing/snapshots/0000-small-network.toml")
+                .unwrap();
 
         let expected = NetworkConfigBuilder::new()
             .with_relaychain(|relaychain| {
diff --git a/crates/configuration/src/relaychain.rs b/crates/configuration/src/relaychain.rs
index cd51b09..3e85725 100644
--- a/crates/configuration/src/relaychain.rs
+++ b/crates/configuration/src/relaychain.rs
@@ -25,7 +25,7 @@ pub struct RelaychainConfig {
     random_nominators_count: Option<u32>,
     max_nominations: Option<u8>,
     #[serde(skip_serializing_if = "std::vec::Vec::is_empty")]
-    nodes: Vec<NodeConfig>,
+    pub(crate) nodes: Vec<NodeConfig>,
 }
 
 impl RelaychainConfig {
diff --git a/crates/configuration/src/shared/node.rs b/crates/configuration/src/shared/node.rs
index 086d551..1fde5e3 100644
--- a/crates/configuration/src/shared/node.rs
+++ b/crates/configuration/src/shared/node.rs
@@ -56,7 +56,7 @@ impl From<(&str, &str)> for EnvVar {
 pub struct NodeConfig {
     name: String,
     image: Option<Image>,
-    command: Option<Command>,
+    pub(crate) command: Option<Command>,
     #[serde(default)]
     args: Vec<Arg>,
     #[serde(default)]
diff --git a/crates/configuration/src/shared/types.rs b/crates/configuration/src/shared/types.rs
index 3e00e70..a476007 100644
--- a/crates/configuration/src/shared/types.rs
+++ b/crates/configuration/src/shared/types.rs
@@ -23,7 +23,7 @@ pub type ParaId = u32;
 
 /// Custom type wrapping u128 to add custom Serialization/Deserialization logic because it's not supported
 /// issue tracking the problem: <https://github.com/toml-rs/toml/issues/540>
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Default, Debug, Clone, PartialEq)]
 pub struct U128(pub(crate) u128);
 
 impl From<u128> for U128 {
@@ -32,12 +32,6 @@ impl From<u128> for U128 {
     }
 }
 
-impl Default for U128 {
-    fn default() -> Self {
-        U128(30)
-    }
-}
-
 impl Serialize for U128 {
     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
     where
@@ -67,11 +61,12 @@ impl<'de> Deserialize<'de> for U128 {
             where
                 E: de::Error,
             {
+                println!("------------ > visit_u128: {}", value);
                 Ok(shared::types::U128(value))
             }
         }
 
-        deserializer.deserialize_any(U128Visitor)
+        deserializer.deserialize_u128(U128Visitor)
     }
 }
 
@@ -327,19 +322,18 @@ impl<'de> Deserialize<'de> for Arg {
                 formatter.write_str("a string")
             }
 
-            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
+            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
             where
                 E: de::Error,
             {
                 // TODO: (nikos) This needs to be beautified somehow
-                if v.contains("=") {
-                    let split: Vec<&str> = v.split("=").collect::<Vec<&str>>();
-                    Ok(Arg::Option(split[0].to_string(), split[1].to_string()))
-                } else if v.starts_with("--") || v.starts_with("-") {
-                    let split: Vec<&str> = v.split(" ").collect::<Vec<&str>>();
+                println!("------------ > visit_str: {}", v);
+
+                if v.contains('=') || v.starts_with("--") || v.starts_with('-') {
+                    let split: Vec<&str> = v.split('=').collect::<Vec<&str>>();
                     Ok(Arg::Option(split[0].to_string(), split[1].to_string()))
                 } else {
-                    Ok(Arg::Flag(v))
+                    Ok(Arg::Flag(v.to_string()))
                 }
             }
         }
@@ -356,13 +350,9 @@ pub struct ValidationContext {
 
 #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
 pub struct ChainDefaultContext {
-    #[serde(default)]
     pub(crate) default_command: Option<Command>,
-    #[serde(default)]
     pub(crate) default_image: Option<Image>,
-    #[serde(default)]
     pub(crate) default_resources: Option<Resources>,
-    #[serde(default)]
     pub(crate) default_db_snapshot: Option<AssetLocation>,
     #[serde(default)]
     pub(crate) default_args: Vec<Arg>,
-- 
GitLab