Skip to main content

ValueTypes

Enum ValueTypes 

Source
pub enum ValueTypes {
    RequiredSingle(Value),
    OptionalSingle(Option<Value>),
    RequiredMultiple(Vec<Value>, Option<usize>),
    OptionalMultiple(Option<Vec<Value>>, Option<usize>),
}
Expand description

Defines the type and cardinality of values an option can accept.

This enum enforces compile-time guarantees about option value requirements:

  • Whether values are required or optional
  • Whether single or multiple values are accepted
  • Default values when not provided

§Examples

use fli::option_parser::{Value, ValueTypes};
// Flag option (no value) - use Bool(false) as default
let verbose = ValueTypes::OptionalSingle(Some(Value::Bool(false)));

// Required single value
let output = ValueTypes::RequiredSingle(Value::Str(String::new()));

// Optional single value with default
let port = ValueTypes::OptionalSingle(Some(Value::Int(8080)));

// Multiple required values (at least 1)
let files = ValueTypes::RequiredMultiple(vec![], None);

// Exactly 3 values required
let coords = ValueTypes::RequiredMultiple(vec![], Some(3));

// Multiple optional values
let tags = ValueTypes::OptionalMultiple(None, None);

Variants§

§

RequiredSingle(Value)

Requires exactly one value

  • First field: default value
§

OptionalSingle(Option<Value>)

Optionally accepts one value

  • First field: default value if not provided
  • For flag options, use OptionalSingle(Some(Value::Bool(false)))
§

RequiredMultiple(Vec<Value>, Option<usize>)

Requires at least one value, optionally up to a maximum

  • First field: collected values
  • Second field: exact count required (None = unlimited)
§

OptionalMultiple(Option<Vec<Value>>, Option<usize>)

Optionally accepts multiple values

  • First field: collected values if any
  • Second field: maximum count (None = unlimited)

Implementations§

Source§

impl ValueTypes

Source

pub fn expects_value(&self) -> bool

Checks if this value type expects to consume arguments.

§Returns

true for types that require values, false for flags

§Examples
use fli::option_parser::{Value, ValueTypes};

// Flag option (Bool) - doesn't expect argument after the flag
assert_eq!(ValueTypes::OptionalSingle(Some(Value::Bool(false))).expects_value(), false);

// String option - expects an argument
assert_eq!(ValueTypes::RequiredSingle(Value::Str(String::new())).expects_value(), true);
Source

pub fn as_str(&self) -> Option<&str>

Extracts a string value if this is a single-value type.

§Returns
  • Some(&str) - If this contains a string value
  • None - If this is not a single string value
§Examples
use fli::option_parser::{Value, ValueTypes};
let val = ValueTypes::RequiredSingle(Value::Str("hello".to_string()));
assert_eq!(val.as_str(), Some("hello"));

// Flag option returns None (it's a Bool, not a String)
let flag = ValueTypes::OptionalSingle(Some(Value::Bool(true)));
assert_eq!(flag.as_str(), None);
Source

pub fn as_strings(&self) -> Option<Vec<&str>>

Extracts multiple string values if this is a multi-value type.

§Returns
  • Some(Vec<&str>) - Vector of string slices if values are strings
  • None - If this is not a multi-value type or contains non-strings
§Examples
let values = vec![
    Value::Str("file1.txt".to_string()),
    Value::Str("file2.txt".to_string()),
];
let val = ValueTypes::RequiredMultiple(values, None);

let strings = val.as_strings().unwrap();
assert_eq!(strings, vec!["file1.txt", "file2.txt"]);

Trait Implementations§

Source§

impl Clone for ValueTypes

Source§

fn clone(&self) -> ValueTypes

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 Debug for ValueTypes

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.