mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
refactor(config): centralize Mihomo YAML output
This commit is contained in:
@@ -18,8 +18,6 @@ pub enum RuntimeBundleError {
|
||||
},
|
||||
#[error("runtime config has no parent directory")]
|
||||
MissingConfigDirectory,
|
||||
#[error("failed to serialize service runtime config")]
|
||||
SerializeConfig(#[source] serde_yaml_ng::Error),
|
||||
#[error("runtime provider destination {0:?} is declared for two different sources")]
|
||||
DifferentSources(String),
|
||||
#[error("runtime provider destination {0:?} is claimed more than once")]
|
||||
|
||||
@@ -33,17 +33,11 @@ pub struct RuntimeAsset {
|
||||
}
|
||||
|
||||
pub struct RuntimeBundlePlan {
|
||||
pub yaml: String,
|
||||
pub config: Value,
|
||||
pub assets: Vec<RuntimeAsset>,
|
||||
pub remote_providers: Vec<RemoteProviderRef>,
|
||||
}
|
||||
|
||||
struct RuntimeParts {
|
||||
config: Value,
|
||||
assets: Vec<RuntimeAsset>,
|
||||
remote_providers: Vec<RemoteProviderRef>,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, RuntimeBundleError>;
|
||||
|
||||
pub async fn collect_runtime_bundle(config_file: &Path) -> Result<RuntimeBundlePlan> {
|
||||
@@ -58,12 +52,7 @@ pub async fn collect_runtime_bundle(config_file: &Path) -> Result<RuntimeBundleP
|
||||
source,
|
||||
})?;
|
||||
let config_root = config_file.parent().ok_or(RuntimeBundleError::MissingConfigDirectory)?;
|
||||
let parts = collect_runtime_parts(config, config_root)?;
|
||||
Ok(RuntimeBundlePlan {
|
||||
yaml: serde_yaml_ng::to_string(&parts.config).map_err(RuntimeBundleError::SerializeConfig)?,
|
||||
assets: parts.assets,
|
||||
remote_providers: parts.remote_providers,
|
||||
})
|
||||
collect_runtime_parts(config, config_root)
|
||||
}
|
||||
|
||||
pub fn remote_providers_of(config: &Mapping, config_root: &Path) -> Result<Vec<RemoteProviderRef>> {
|
||||
@@ -158,7 +147,7 @@ fn allocate_provider_destination(destination: &str, url: &str, reserved: &mut Ha
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result<RuntimeParts> {
|
||||
fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result<RuntimeBundlePlan> {
|
||||
let config_root = std::fs::canonicalize(config_root)?;
|
||||
let mut assets = Vec::new();
|
||||
let mut destinations = HashSet::new();
|
||||
@@ -191,7 +180,7 @@ fn collect_runtime_parts(mut config: Value, config_root: &Path) -> Result<Runtim
|
||||
}
|
||||
}
|
||||
|
||||
Ok(RuntimeParts {
|
||||
Ok(RuntimeBundlePlan {
|
||||
config,
|
||||
assets,
|
||||
remote_providers,
|
||||
@@ -465,7 +454,7 @@ mod tests {
|
||||
"resolving an already generated config must be idempotent"
|
||||
);
|
||||
|
||||
assert_eq!(serde_yaml_ng::from_str::<serde_yaml_ng::Mapping>(&bundle.yaml)?, config);
|
||||
assert_eq!(bundle.config.as_mapping(), Some(&config));
|
||||
let readback = remote_providers_of(&config, &root)?;
|
||||
assert_eq!(bundle.remote_providers.len(), 2);
|
||||
for declared in readback {
|
||||
@@ -539,7 +528,7 @@ mod tests {
|
||||
|
||||
let bundle = collect_runtime_bundle(&config).await?;
|
||||
|
||||
let bundled_config: serde_yaml_ng::Value = serde_yaml_ng::from_str(&bundle.yaml)?;
|
||||
let bundled_config = &bundle.config;
|
||||
assert_eq!(
|
||||
bundled_config["proxy-providers"]["local"]["path"].as_str(),
|
||||
Some("providers/local.yaml")
|
||||
@@ -548,7 +537,7 @@ mod tests {
|
||||
bundled_config["proxy-providers"]["remote"]["path"].as_str(),
|
||||
Some("providers/remote.yaml")
|
||||
);
|
||||
assert!(!bundle.yaml.contains(&root.to_string_lossy().into_owned()));
|
||||
assert!(!serde_yaml_ng::to_string(bundled_config)?.contains(root.to_string_lossy().as_ref()));
|
||||
assert!(
|
||||
bundle
|
||||
.assets
|
||||
@@ -639,9 +628,8 @@ mod tests {
|
||||
"only the provider that could be classified is declared"
|
||||
);
|
||||
assert_eq!(bundle.remote_providers[0].destination, "rules/g.yaml");
|
||||
let reserialized: serde_yaml_ng::Value = serde_yaml_ng::from_str(&bundle.yaml)?;
|
||||
assert_eq!(
|
||||
reserialized["rule-providers"]["nourl"]["path"].as_str(),
|
||||
bundle.config["rule-providers"]["nourl"]["path"].as_str(),
|
||||
Some("./rules/nourl.yaml"),
|
||||
"an unclassifiable provider keeps the path the core was given"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user