Skip to main content

Option

Struct Option 

Source
pub struct Option<T: OptionType> { /* private fields */ }
Expand description

A typed config option. The user-visible name + metadata, plus the cell holding the current value. Constructed once and handed to crate::ConfigRegistry::register, which returns a typed OptionHandle consumers use to read / write.

Implementations§

Source§

impl<T: OptionType> Option<T>

Source

pub fn new( name: impl Into<Cow<'static, str>>, default: T, doc: impl Into<Cow<'static, str>>, ) -> Self

Build a typed option with a default value. Most options use the OptionBuilder (see Option::builder) instead so optional fields don’t need defaults restated.

Source

pub fn builder( name: impl Into<Cow<'static, str>>, default: T, doc: impl Into<Cow<'static, str>>, ) -> OptionBuilder<T>

Builder entry point.

§Examples
use lattice_config::ErasedOption;
use lattice_config::option::Option;

let opt = Option::<i64>::builder("width", 8, "Tab visual width.")
    .aliases(&["wd"])
    .validate(|i| {
        (1..=32).contains(i).then_some(()).ok_or_else(|| format!("out of range: {i}"))
    })
    .build();

assert_eq!(*opt.get(), 8);
assert_eq!(opt.set(99), Err("out of range: 99".to_string()));
opt.set(2).unwrap();
// The erased view formats; the default is captured at build time.
assert_eq!(opt.get_formatted(), "2");
assert_eq!(opt.default_formatted(), "8");
Source

pub fn structured( name: impl Into<Cow<'static, str>>, schema: ConfigSchema, default: T, doc: impl Into<Cow<'static, str>>, ) -> Self

TC.3: an option whose shape is declared rather than derived — the plugin-contributed structured option.

default is NOT validated here; the caller (config_host::register_structured_option) checks it against schema first, so a declaration that does not fit registers nothing at all rather than producing an option that exists and cannot hold a legal value.

Source

pub fn declared_schema(&self) -> ConfigSchema

The option’s declared shape: what was given to Self::structured, or the type’s own crate::OptionType::schema.

Source

pub fn name(&self) -> &str

Borrows the option’s name. PL8.F: was -> &'static str when the field was a leaked &'static str; a Cow field can only lend a &str bound to &self, so callers that need an owned name .to_owned() it (registry).

Source

pub fn get(&self) -> Arc<T> ⓘ

Wait-free read of the current value. The returned Arc<T> is stable for the caller’s frame; concurrent writers may publish a newer value, observed by the next call.

Source

pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R

Borrow the current value through a closure. Avoids the Arc::clone cost of Self::get for one-shot reads – useful in render hot paths that read+drop in the same statement.

Source

pub fn set(&self, value: T) -> Result<(), String>

Set the current value, running the (private) validate closure first. Returns the validator’s error verbatim on rejection; otherwise commits and returns Ok(()).

Trait Implementations§

Source§

impl<T: OptionType + Debug> Debug for Option<T>

Source§

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

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

impl<T: OptionType> ErasedOption for Option<T>

Source§

fn name(&self) -> &str

Canonical name (tabstop, ui.diagnostics.inline). The name crate::ConfigRegistry keys failures and events under, even when the user typed an alias.
Source§

fn aliases(&self) -> &'static [&'static str]

Alternative names that resolve to this option (ts for tabstop). Empty for most options.
Source§

fn doc(&self) -> &str

Human-readable description, shown by :describe-option and :customize.
Source§

fn type_label(&self) -> &'static str

The value type’s OptionType::type_label (boolean, integer, string, signcolumn, …).
Source§

fn parse_and_set(&self, value: &str) -> Result<(), String>

Parse value against the option’s OptionType, run the post-parse validator, store the new value. Returns the concatenated error if either stage fails.
Source§

fn parse_to_erased( &self, value: &str, ) -> Result<Arc<dyn Any + Send + Sync>, String>

Parse value against the option’s OptionType and run the validator, but do not write to the option’s storage. Returns the typed value erased as Arc<dyn Any + Send + Sync>. Used by ConfigRegistry::parse_for_buffer_local to produce an OptionOverride for the buffer-local layer (BL.1) without touching the global registry.
Source§

fn get_formatted(&self) -> String

Render the current value (:set foo? echo, customize buffer view).
Source§

fn default_formatted(&self) -> &str

Render the default value (the value the option held at registration time). Used by :describe-option to show “default: X” alongside “current: Y”.
Source§

fn enumerate_values(&self) -> Option<Vec<&'static str>>

Enumerate valid string values for :set foo=<Tab>. None means free-form (no completion).
Source§

fn enumerate_values_with_docs(&self) -> Option<Vec<EnumeratedValue>>

Enumerate valid string values + per-value doc strings. Slice 3c.unify.option-doc-annotator — drives the marginalia column on :set foo=<Tab> completion. None means free-form. Default impl wraps enumerate_values with empty docs; rich-help types override on OptionType::enumerate_with_docs.
Source§

fn name_forms(&self) -> Vec<String>

Alternate name forms the cmdline accepts (noNAME for booleans). Drives :set <Tab> enumeration.
Source§

fn is_bool(&self) -> bool

Whether this option supports :set noNAME. Booleans true, everything else false.
Source§

fn accepts_list(&self) -> bool

Whether this option’s value is list-shaped (ML.5). The config loader consults this to decide whether a TOML array for this key should be joined into the option’s delimited parse form (true) or rejected as a scalar/array mismatch (false). Forwards to OptionType::accepts_list.
Source§

fn negate(&self) -> Result<(), String>

Set the option to its negation value (only meaningful when Self::is_bool is true). Used by the :set noNAME path. Returns Err if called on a non-bool option (registry guards this; this is defense in depth).
Source§

fn format_erased_value( &self, value: &Arc<dyn Any + Send + Sync>, ) -> Option<String>

Format an already-erased value of this option’s type. Used by the query echo path (:set name?) to display the resolved value when the concrete type isn’t known at the call site. Returns None if value doesn’t downcast to this option’s Value type (caller falls back to get_formatted() in that case).
Source§

fn as_any(&self) -> &dyn Any

Project back to the concrete type for typed-handle reads. Read more
Source§

fn current_value_erased(&self) -> Arc<dyn Any + Send + Sync> ⓘ

Return the option’s current value as an erased Arc<dyn Any + Send + Sync>. Used by crate::ConfigRegistry::bootstrap_resolved_with_current_values to seed the crate::ResolvedOptions cache with each option’s current registry value (resolution layer 5) before mode/buffer-local layers overlay on top.
Source§

fn schema(&self) -> ConfigSchema

This option’s declared shape.
Source§

fn get_value(&self) -> ConfigValue

The current value as a schema-shaped tree.
Source§

fn set_value(&self, value: &ConfigValue) -> Result<(), String>

Validate value against Self::schema, then commit it. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Option<T>

§

impl<T> RefUnwindSafe for Option<T>
where T: RefUnwindSafe,

§

impl<T> Send for Option<T>

§

impl<T> Sync for Option<T>

§

impl<T> Unpin for Option<T>

§

impl<T> UnsafeUnpin for Option<T>

§

impl<T> UnwindSafe for Option<T>
where T: RefUnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more