Skip to content

Commit

Permalink
refactor: make watchlist an optional value
Browse files Browse the repository at this point in the history
  • Loading branch information
findolor committed Sep 6, 2024
1 parent 62ef525 commit 1ffefd2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
24 changes: 14 additions & 10 deletions crates/settings/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Config {
pub sentry: Option<bool>,
pub raindex_version: Option<String>,
#[typeshare(typescript(type = "Record<string, string>"))]
pub watchlist: HashMap<String, Arc<String>>,
pub watchlist: Option<HashMap<String, Arc<String>>>,
}

pub type Subgraph = Url;
Expand Down Expand Up @@ -171,11 +171,11 @@ impl TryFrom<ConfigSource> for Config {
})
.collect::<Result<HashMap<String, Arc<Chart>>, ParseConfigSourceError>>()?;

let watchlist = item
.watchlist
.into_iter()
.map(|(name, address)| Ok((name, Arc::new(address))))
.collect::<Result<HashMap<String, Arc<String>>, ParseConfigSourceError>>()?;
let watchlist = item.watchlist.map(|wl| {
wl.into_iter()
.map(|(name, address)| (name, Arc::new(address)))
.collect::<HashMap<String, Arc<String>>>()
});

let config = Config {
raindex_version: item.raindex_version,
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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");
}
Expand Down
9 changes: 5 additions & 4 deletions crates/settings/src/config_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub struct ConfigSource {
pub sentry: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub raindex_version: Option<String>,
#[serde(skip_serializing_if = "HashMap::is_empty")]
pub watchlist: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub watchlist: Option<HashMap<String, String>>,
}

#[typeshare]
Expand Down Expand Up @@ -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]
Expand Down
46 changes: 30 additions & 16 deletions crates/settings/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -298,7 +312,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: HashMap::new(),
watchlist: None,
};

let other = ConfigSource {
Expand All @@ -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(()));
Expand All @@ -337,7 +351,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: HashMap::new(),
watchlist: None,
};

let mut other = ConfigSource {
Expand All @@ -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
Expand Down Expand Up @@ -390,7 +404,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: HashMap::new(),
watchlist: None,
};

let mut other = ConfigSource {
Expand All @@ -407,7 +421,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: HashMap::new(),
watchlist: None,
};

other.metaboards.insert(
Expand Down
2 changes: 1 addition & 1 deletion crates/settings/src/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ffefd2

Please sign in to comment.