Skip to main content

OptionSet

Struct OptionSet 

Source
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

Source

pub fn of<T>() -> Self
where T: OptionsMetadata + 'static,

Source

pub fn record(&self, visit: &mut dyn Visit)

Visits the options in this set by calling visit for each option.

Source

pub fn documentation(&self) -> Option<&'static str>

Source

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"));
Source

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());
Source

pub fn collect_fields(&self) -> Vec<(String, OptionField)>

Trait Implementations§

Source§

impl Clone for OptionSet

Source§

fn clone(&self) -> OptionSet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for OptionSet

Source§

impl Debug for OptionSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for OptionSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.