|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use hpsvm_fixture::Compare; |
| 4 | + |
| 5 | +use crate::error::CliError; |
| 6 | + |
| 7 | +#[derive(Debug, serde::Deserialize)] |
| 8 | +pub(crate) struct CompareConfigFile { |
| 9 | + compares: Vec<Compare>, |
| 10 | +} |
| 11 | + |
| 12 | +pub(crate) fn load_compares( |
| 13 | + path: Option<&Path>, |
| 14 | + fallback: &[Compare], |
| 15 | + ignore_compute_units: bool, |
| 16 | +) -> Result<Vec<Compare>, CliError> { |
| 17 | + let mut compares = if let Some(path) = path { |
| 18 | + let file = std::fs::read_to_string(path)?; |
| 19 | + match path.extension().and_then(|value| value.to_str()) { |
| 20 | + Some("yaml" | "yml") => serde_yaml::from_str::<CompareConfigFile>(&file) |
| 21 | + .map(|config| config.compares) |
| 22 | + .map_err(|error| CliError::ConfigParse { |
| 23 | + path: path.display().to_string(), |
| 24 | + reason: error.to_string(), |
| 25 | + })?, |
| 26 | + Some("json") => serde_json::from_str::<CompareConfigFile>(&file) |
| 27 | + .map(|config| config.compares) |
| 28 | + .map_err(|error| CliError::ConfigParse { |
| 29 | + path: path.display().to_string(), |
| 30 | + reason: error.to_string(), |
| 31 | + })?, |
| 32 | + _ => return Err(CliError::UnsupportedConfigFormat { path: path.display().to_string() }), |
| 33 | + } |
| 34 | + } else { |
| 35 | + fallback.to_vec() |
| 36 | + }; |
| 37 | + |
| 38 | + if ignore_compute_units { |
| 39 | + compares.retain(|compare| !matches!(compare, Compare::ComputeUnits)); |
| 40 | + } |
| 41 | + |
| 42 | + Ok(compares) |
| 43 | +} |
| 44 | + |
| 45 | +#[cfg(test)] |
| 46 | +mod tests { |
| 47 | + use std::path::PathBuf; |
| 48 | + |
| 49 | + use hpsvm_fixture::Compare; |
| 50 | + use solana_address::Address; |
| 51 | + |
| 52 | + use super::load_compares; |
| 53 | + use crate::error::CliError; |
| 54 | + |
| 55 | + fn temp_config_path(extension: &str) -> PathBuf { |
| 56 | + std::env::temp_dir().join(format!( |
| 57 | + "hpsvm-cli-compare-config-{}.{}", |
| 58 | + Address::new_unique(), |
| 59 | + extension |
| 60 | + )) |
| 61 | + } |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn load_compares_uses_fallback_and_can_ignore_compute_units() { |
| 65 | + let compares = load_compares(None, &[Compare::Status, Compare::ComputeUnits], true) |
| 66 | + .expect("fallback compares should load"); |
| 67 | + |
| 68 | + assert_eq!(compares, vec![Compare::Status]); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn load_compares_reads_yaml_config() { |
| 73 | + let path = temp_config_path("yaml"); |
| 74 | + std::fs::write(&path, "compares:\n - Status\n - ComputeUnits\n") |
| 75 | + .expect("yaml config should write"); |
| 76 | + |
| 77 | + let compares = |
| 78 | + load_compares(Some(&path), &[Compare::Fee], false).expect("yaml compares should load"); |
| 79 | + |
| 80 | + assert_eq!(compares, vec![Compare::Status, Compare::ComputeUnits]); |
| 81 | + |
| 82 | + std::fs::remove_file(path).ok(); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn load_compares_reads_json_config() { |
| 87 | + let path = temp_config_path("json"); |
| 88 | + std::fs::write(&path, r#"{"compares":["Status","Logs"]}"#) |
| 89 | + .expect("json config should write"); |
| 90 | + |
| 91 | + let compares = |
| 92 | + load_compares(Some(&path), &[Compare::Fee], false).expect("json compares should load"); |
| 93 | + |
| 94 | + assert_eq!(compares, vec![Compare::Status, Compare::Logs]); |
| 95 | + |
| 96 | + std::fs::remove_file(path).ok(); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn load_compares_rejects_unsupported_config_format() { |
| 101 | + let path = temp_config_path("txt"); |
| 102 | + std::fs::write(&path, "compares: []").expect("text config should write"); |
| 103 | + |
| 104 | + let error = load_compares(Some(&path), &[Compare::Fee], false) |
| 105 | + .expect_err("unsupported extension should fail"); |
| 106 | + |
| 107 | + assert!(matches!(error, CliError::UnsupportedConfigFormat { .. })); |
| 108 | + |
| 109 | + std::fs::remove_file(path).ok(); |
| 110 | + } |
| 111 | +} |
0 commit comments