Files
aria2-rust-pro/crates/aria2-rust-pro-compat/src/options/query.rs
T

78 lines
2.7 KiB
Rust

use super::registry::OPTION_SPECS;
use super::reserved::{RESERVED_OPTION_NAMES, ReservedOptionName};
use super::{OptionScope, OptionSource, OptionSpec, OptionStatus};
/// Returns the canonical option specification for a known spelling.
#[must_use]
pub fn option_spec(name: &str) -> Option<&'static OptionSpec> {
OPTION_SPECS
.iter()
.find(|s| s.name == name)
.or_else(|| OPTION_SPECS.iter().find(|s| s.aliases.contains(&name)))
}
/// Returns whether an option is required by the `aria2-rust-pro` surface.
#[must_use]
pub fn is_required_pro_option(option: &str) -> bool {
OPTION_SPECS.iter().any(|s| {
s.name == option
&& (s.source == OptionSource::Pro || s.source == OptionSource::CompatibilityAlias)
})
}
/// Returns whether a status should be exposed in live option listings.
#[must_use]
pub const fn is_live_option_status(status: OptionStatus) -> bool {
matches!(
status,
OptionStatus::Implemented | OptionStatus::Verified | OptionStatus::Deprecated
)
}
/// Filters the registry while preserving canonical-name uniqueness.
fn unique_option_specs(predicate: impl Fn(&OptionSpec) -> bool) -> Vec<&'static OptionSpec> {
let mut seen = std::collections::BTreeSet::new();
OPTION_SPECS
.iter()
.filter(|spec| predicate(spec) && seen.insert(spec.name))
.collect()
}
/// Returns all live option specs without duplicate canonical names.
#[must_use]
pub fn live_option_specs() -> Vec<&'static OptionSpec> {
unique_option_specs(|spec| is_live_option_status(spec.status))
}
/// Returns all options available in the global scope.
#[must_use]
pub fn global_option_specs() -> Vec<&'static OptionSpec> {
unique_option_specs(|spec| {
matches!(spec.metadata.scope, OptionScope::Global | OptionScope::Both)
})
}
/// Returns all options available in the per-download scope.
#[must_use]
pub fn per_download_option_specs() -> Vec<&'static OptionSpec> {
unique_option_specs(|spec| {
matches!(
spec.metadata.scope,
OptionScope::PerDownload | OptionScope::Both
)
})
}
/// Returns the reserved option-name table.
#[must_use]
pub const fn reserved_option_names() -> &'static [ReservedOptionName] {
RESERVED_OPTION_NAMES
}
/// Returns the canonical RPC option name for a known spelling.
#[must_use]
pub fn rpc_option_name(name: &str) -> Option<&'static str> {
option_spec(name).and_then(|s| s.rpc_names.first().copied())
}
/// Returns the environment-variable name associated with an option.
#[must_use]
pub fn env_var_for_option(name: &str) -> Option<String> {
option_spec(name).map(|s| format!("ARIA2_{}", s.name.replace('-', "_").to_ascii_uppercase()))
}