diff --git a/crates/settings/src/config.rs b/crates/settings/src/config.rs index 81e36c45f..bbe724303 100644 --- a/crates/settings/src/config.rs +++ b/crates/settings/src/config.rs @@ -35,7 +35,7 @@ pub struct Config { pub sentry: Option, pub raindex_version: Option, #[typeshare(typescript(type = "Record"))] - pub watchlist: HashMap>, + pub watchlist: Option>>, } pub type Subgraph = Url; @@ -171,11 +171,11 @@ impl TryFrom for Config { }) .collect::>, ParseConfigSourceError>>()?; - let watchlist = item - .watchlist - .into_iter() - .map(|(name, address)| Ok((name, Arc::new(address)))) - .collect::>, ParseConfigSourceError>>()?; + let watchlist = item.watchlist.map(|wl| { + wl.into_iter() + .map(|(name, address)| (name, Arc::new(address))) + .collect::>>() + }); let config = Config { raindex_version: item.raindex_version, @@ -282,7 +282,10 @@ mod tests { let charts = HashMap::new(); let deployments = HashMap::new(); let sentry = Some(true); - let watchlist = HashMap::from([("name-one".to_string(), "address-one".to_string())]); + let watchlist = Some(HashMap::from([( + "name-one".to_string(), + "address-one".to_string(), + )])); let config_string = ConfigSource { raindex_version: Some("0x123".to_string()), @@ -359,9 +362,10 @@ mod tests { assert_eq!(config.raindex_version, Some("0x123".to_string())); // Verify watchlist - assert!(!config.watchlist.is_empty()); - assert_eq!(config.watchlist.len(), 1); - let (name, address) = config.watchlist.iter().next().unwrap(); + assert!(config.watchlist.is_some()); + let watchlist = config.watchlist.as_ref().unwrap(); + assert_eq!(watchlist.len(), 1); + let (name, address) = watchlist.iter().next().unwrap(); assert_eq!(name, "name-one"); assert_eq!(address.as_str(), "address-one"); } diff --git a/crates/settings/src/config_source.rs b/crates/settings/src/config_source.rs index bc95263cc..95e143d40 100644 --- a/crates/settings/src/config_source.rs +++ b/crates/settings/src/config_source.rs @@ -38,8 +38,8 @@ pub struct ConfigSource { pub sentry: Option, #[serde(skip_serializing_if = "Option::is_none")] pub raindex_version: Option, - #[serde(skip_serializing_if = "HashMap::is_empty")] - pub watchlist: HashMap, + #[serde(skip_serializing_if = "Option::is_none")] + pub watchlist: Option>, } #[typeshare] @@ -483,8 +483,9 @@ watchlist: assert_eq!(config.raindex_version, Some("123".to_string())); - assert_eq!(config.watchlist.get("name-one").unwrap(), "address-one"); - assert_eq!(config.watchlist.get("name-two").unwrap(), "address-two"); + let watchlist = config.watchlist.unwrap(); + assert_eq!(watchlist.get("name-one").unwrap(), "address-one"); + assert_eq!(watchlist.get("name-two").unwrap(), "address-two"); } #[tokio::test] diff --git a/crates/settings/src/merge.rs b/crates/settings/src/merge.rs index fef697723..feb2f67b0 100644 --- a/crates/settings/src/merge.rs +++ b/crates/settings/src/merge.rs @@ -151,12 +151,19 @@ impl ConfigSource { }?; // Watchlist - let watchlist = &mut self.watchlist; - for (key, value) in other.watchlist { - if watchlist.contains_key(&key) { - return Err(MergeError::WatchlistCollision(key)); + match (&mut self.watchlist, other.watchlist) { + (Some(watchlist), Some(other_watchlist)) => { + for (key, value) in other_watchlist { + if watchlist.contains_key(&key) { + return Err(MergeError::WatchlistCollision(key)); + } + watchlist.insert(key, value); + } } - watchlist.insert(key, value); + (None, Some(other_watchlist)) => { + self.watchlist = Some(other_watchlist); + } + _ => {} } Ok(()) @@ -264,12 +271,19 @@ impl Config { }?; // Watchlist - let watchlist = &mut self.watchlist; - for (key, value) in other.watchlist { - if watchlist.contains_key(&key) { - return Err(MergeError::WatchlistCollision(key)); + match (&mut self.watchlist, other.watchlist) { + (Some(watchlist), Some(other_watchlist)) => { + for (key, value) in other_watchlist { + if watchlist.contains_key(&key) { + return Err(MergeError::WatchlistCollision(key)); + } + watchlist.insert(key, value); + } + } + (None, Some(other_watchlist)) => { + self.watchlist = Some(other_watchlist); } - watchlist.insert(key, value); + _ => {} } Ok(()) @@ -298,7 +312,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; let other = ConfigSource { @@ -315,7 +329,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; assert_eq!(config.merge(other), Ok(())); @@ -337,7 +351,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; let mut other = ConfigSource { @@ -354,7 +368,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; // Add a collision to cause an unsuccessful merge @@ -390,7 +404,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; let mut other = ConfigSource { @@ -407,7 +421,7 @@ mod tests { networks: HashMap::new(), deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), + watchlist: None, }; other.metaboards.insert( diff --git a/crates/settings/src/scenario.rs b/crates/settings/src/scenario.rs index 6c6c84eef..6d457d6df 100644 --- a/crates/settings/src/scenario.rs +++ b/crates/settings/src/scenario.rs @@ -213,7 +213,7 @@ mod tests { charts: HashMap::new(), // Assuming no charts for simplification deployments: HashMap::new(), sentry: None, - watchlist: HashMap::new(), // Assuming no watchlist for simplification + watchlist: None, // Assuming no watchlist for simplification }; // Perform the conversion