pub struct OptionSet { /* private fields */ }Expand description
A set of options.
It extracts the options by calling the OptionsMetadata::record of a type implementing
OptionsMetadata.
Implementations§
Source§impl OptionSet
impl OptionSet
pub fn of<T>() -> Selfwhere
T: OptionsMetadata + 'static,
Sourcepub fn record(&self, visit: &mut dyn Visit)
pub fn record(&self, visit: &mut dyn Visit)
Visits the options in this set by calling visit for each option.
pub fn documentation(&self) -> Option<&'static str>
Sourcepub fn has(&self, name: &str) -> bool
pub fn has(&self, name: &str) -> bool
Returns true if this set has an option that resolves to name.
The name can be separated by . to find a nested option.
§Examples
§Test for the existence of a child option
struct WithOptions;
impl OptionsMetadata for WithOptions {
fn record(visit: &mut dyn Visit) {
visit.record_field("ignore-git-ignore", OptionField {
doc: "Whether Ruff should respect the gitignore file",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None,
});
}
}
assert!(WithOptions::metadata().has("ignore-git-ignore"));
assert!(!WithOptions::metadata().has("does-not-exist"));§Test for the existence of a nested option
struct Root;
impl OptionsMetadata for Root {
fn record(visit: &mut dyn Visit) {
visit.record_field("ignore-git-ignore", OptionField {
doc: "Whether Ruff should respect the gitignore file",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None
});
visit.record_set("format", Nested::metadata());
}
}
struct Nested;
impl OptionsMetadata for Nested {
fn record(visit: &mut dyn Visit) {
visit.record_field("hard-tabs", OptionField {
doc: "Use hard tabs for indentation and spaces for alignment.",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None
});
}
}
assert!(Root::metadata().has("format.hard-tabs"));
assert!(!Root::metadata().has("format.spaces"));
assert!(!Root::metadata().has("lint.hard-tabs"));Sourcepub fn find(&self, name: &str) -> Option<OptionEntry>
pub fn find(&self, name: &str) -> Option<OptionEntry>
Returns Some if this set has an option that resolves to name and None otherwise.
The name can be separated by . to find a nested option.
§Examples
§Find a child option
struct WithOptions;
static IGNORE_GIT_IGNORE: OptionField = OptionField {
doc: "Whether Ruff should respect the gitignore file",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None
};
impl OptionsMetadata for WithOptions {
fn record(visit: &mut dyn Visit) {
visit.record_field("ignore-git-ignore", IGNORE_GIT_IGNORE.clone());
}
}
assert_eq!(WithOptions::metadata().find("ignore-git-ignore").and_then(OptionEntry::into_field), Some(IGNORE_GIT_IGNORE.clone()));
assert!(WithOptions::metadata().find("does-not-exist").is_none());§Find a nested option
static HARD_TABS: OptionField = OptionField {
doc: "Use hard tabs for indentation and spaces for alignment.",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None
};
struct Root;
impl OptionsMetadata for Root {
fn record(visit: &mut dyn Visit) {
visit.record_field("ignore-git-ignore", OptionField {
doc: "Whether Ruff should respect the gitignore file",
default: "false",
value_type: "bool",
example: "",
scope: None,
deprecated: None
});
visit.record_set("format", Nested::metadata());
}
}
struct Nested;
impl OptionsMetadata for Nested {
fn record(visit: &mut dyn Visit) {
visit.record_field("hard-tabs", HARD_TABS.clone());
}
}
assert_eq!(Root::metadata().find("format.hard-tabs").and_then(OptionEntry::into_field), Some(HARD_TABS.clone()));
assert!(matches!(Root::metadata().find("format"), Some(OptionEntry::Set(_))));
assert!(Root::metadata().find("format.spaces").is_none());
assert!(Root::metadata().find("lint.hard-tabs").is_none());pub fn collect_fields(&self) -> Vec<(String, OptionField)>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for OptionSet
impl RefUnwindSafe for OptionSet
impl Send for OptionSet
impl Sync for OptionSet
impl Unpin for OptionSet
impl UnsafeUnpin for OptionSet
impl UnwindSafe for OptionSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more