mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
refactor: Introduce ConfigurationOptions::get method
This commit is contained in:
parent
26f39cac2f
commit
fc4c927788
2 changed files with 47 additions and 34 deletions
|
@ -1,5 +1,30 @@
|
||||||
pub trait ConfigurationOptions {
|
pub trait ConfigurationOptions {
|
||||||
fn get_available_options() -> Vec<(&'static str, OptionEntry)>;
|
fn get_available_options() -> Vec<(&'static str, OptionEntry)>;
|
||||||
|
|
||||||
|
/// Get an option entry by its fully-qualified name
|
||||||
|
/// (e.g. `foo.bar` refers to the `bar` option in the `foo` group).
|
||||||
|
fn get(name: Option<&str>) -> Option<OptionEntry> {
|
||||||
|
let mut entries = Self::get_available_options();
|
||||||
|
|
||||||
|
let mut parts_iter = name.into_iter().flat_map(|s| s.split('.'));
|
||||||
|
|
||||||
|
while let Some(part) = parts_iter.next() {
|
||||||
|
let (_, field) = entries.into_iter().find(|(name, _)| *name == part)?;
|
||||||
|
match field {
|
||||||
|
OptionEntry::Field(..) => {
|
||||||
|
if parts_iter.next().is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(field);
|
||||||
|
}
|
||||||
|
OptionEntry::Group(fields) => {
|
||||||
|
entries = fields;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(OptionEntry::Group(entries))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
@ -6,43 +6,31 @@ use ruff::settings::{
|
||||||
use crate::ExitStatus;
|
use crate::ExitStatus;
|
||||||
|
|
||||||
#[allow(clippy::print_stdout)]
|
#[allow(clippy::print_stdout)]
|
||||||
pub(crate) fn config(option: Option<&str>) -> ExitStatus {
|
pub(crate) fn config(key: Option<&str>) -> ExitStatus {
|
||||||
let entries = Options::get_available_options();
|
let Some(entry) = Options::get(key) else {
|
||||||
let mut entries = &entries;
|
|
||||||
|
|
||||||
let mut parts_iter = option.iter().flat_map(|s| s.split('.'));
|
|
||||||
|
|
||||||
while let Some(part) = parts_iter.next() {
|
|
||||||
let Some((_, field)) = entries.iter().find(|(name, _)| *name == part) else {
|
|
||||||
println!("Unknown option");
|
println!("Unknown option");
|
||||||
return ExitStatus::Error;
|
return ExitStatus::Error;
|
||||||
};
|
};
|
||||||
match field {
|
|
||||||
|
match entry {
|
||||||
OptionEntry::Field(OptionField {
|
OptionEntry::Field(OptionField {
|
||||||
doc,
|
doc,
|
||||||
default,
|
default,
|
||||||
value_type,
|
value_type,
|
||||||
example,
|
example,
|
||||||
}) => {
|
}) => {
|
||||||
if parts_iter.next().is_some() {
|
|
||||||
println!("Unknown option");
|
|
||||||
return ExitStatus::Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{doc}");
|
println!("{doc}");
|
||||||
println!();
|
println!();
|
||||||
println!("Default value: {default}");
|
println!("Default value: {default}");
|
||||||
println!("Type: {value_type}");
|
println!("Type: {value_type}");
|
||||||
println!("Example usage:\n```toml\n{example}\n```");
|
println!("Example usage:\n```toml\n{example}\n```");
|
||||||
return ExitStatus::Success;
|
|
||||||
}
|
|
||||||
OptionEntry::Group(fields) => {
|
|
||||||
entries = fields;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
OptionEntry::Group(entries) => {
|
||||||
for (name, _) in entries {
|
for (name, _) in entries {
|
||||||
println!("{name}");
|
println!("{name}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ExitStatus::Success
|
ExitStatus::Success
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue