Parameters

Struct Parameters 

Source
pub struct Parameters { /* private fields */ }

Implementations§

Source§

impl Parameters

Source

pub fn new() -> Self

Create a new empty Parameters instance.

§Examples
use parameterx::Parameters;
let params = Parameters::new();
Source

pub fn insert<K, V>(&mut self, key: K, value: V)
where K: Into<String>, V: ParameterValue + 'static,

Insert a key-value pair into the Parameters.

§Arguments
  • key - A key that can be converted into a String.
  • value - A value that implements the ParameterValue trait.
§Examples
use parameterx::Parameters;
use parameterx::ParameterValue;

#[derive(Debug, Clone)]
struct MyValue;

impl ToString for MyValue {
    fn to_string(&self) -> String {
        "my_value".to_string()
    }
}
let mut params = Parameters::new();
params.insert("key", MyValue);
Source

pub fn get<T: 'static>(&self, key: &str) -> Option<&T>

Get a reference to a value of type T associated with the given key.

§Arguments
  • key - A string slice that holds the key.
§Returns

An Option containing a reference to the value if found, or None if not found.

§Examples
use parameterx::Parameters;
use parameterx::ParameterValue;

#[derive(Debug, Clone)]
struct MyValue;

impl ToString for MyValue {
    fn to_string(&self) -> String {
        "my_value".to_string()
    }
}

let mut params = Parameters::new();
params.insert("key", MyValue);
let value: Option<&MyValue> = params.get("key");
Source

pub fn get_required<T: 'static>(&self, key: &str) -> Result<&T>

Get a reference to a value of type T associated with the given key, or return an error if not found.

§Arguments
  • key - A string slice that holds the key.
§Returns

A Result containing a reference to the value if found, or a ParameterError if not found.

§Examples
use parameterx::Parameters;
use parameterx::ParameterValue;

#[derive(Debug, Clone)]
struct MyValue;

impl ToString for MyValue {
    fn to_string(&self) -> String {
        "my_value".to_string()
    }
}

let mut params = Parameters::new();
params.insert("key", MyValue);
let value: Result<&MyValue, _> = params.get_required("key");
Source

pub fn get_string(&self, key: &str) -> Option<String>

Get the string representation of the value associated with the given key.

§Arguments
  • key - A string slice that holds the key.
§Returns

An Option containing the string representation of the value if found, or None if not found.

§Examples
use parameterx::Parameters;
use parameterx::ParameterValue;

#[derive(Debug, Clone)]
struct MyValue;

impl ToString for MyValue {
    fn to_string(&self) -> String {
        "my_value".to_string()
    }
}

let mut params = Parameters::new();
params.insert("key", MyValue);
let value: Option<String> = params.get_string("key");
Source

pub fn contains_key(&self, key: &str) -> bool

Check if the Parameters contains the given key.

§Arguments
  • key - A string slice that holds the key.
§Returns

true if the key is present, false otherwise.

§Examples
use parameterx::Parameters;

let mut params = Parameters::new();
let exists: bool = params.contains_key("key");
Source

pub fn try_get<T>(&self, key: &str) -> Result<T>
where T: TryFrom<String>, T::Error: Error + Send + Sync + 'static,

Try to get a value of type T associated with the given key, converting from a String if necessary.

§Arguments
  • key - A string slice that holds the key.
§Returns

A Result containing the value if found and successfully converted, or a ParameterError if not found or conversion failed.

§Examples
use parameterx::Parameters;

let mut params = Parameters::new();
params.insert("key", "123".to_string());
let value: Result<String, _> = params.try_get("key");
Source

pub fn with<K, V>(self, key: K, value: V) -> Self
where K: Into<String>, V: ParameterValue + 'static,

Insert a key-value pair into the Parameters and return the modified Parameters.

§Arguments
  • key - A key that can be converted into a String.
  • value - A value that implements the ParameterValue trait.
§Returns

The modified Parameters.

§Examples
use parameterx::Parameters;
use parameterx::ParameterValue;

#[derive(Debug, Clone)]
struct MyValue;
impl ToString for MyValue {
    fn to_string(&self) -> String {
        "MyValue".into()
    }
}

let params = Parameters::new().with("key", MyValue);
Source

pub fn merge(&mut self, other: Parameters)

Merge another Parameters instance into this one.

§Arguments
  • other - Another Parameters instance.
§Examples
use parameterx::Parameters;

let mut params1 = Parameters::new();
let mut params2 = Parameters::new();
params1.merge(params2);
Source

pub fn keys(&self) -> impl Iterator<Item = &String>

Get an iterator over the keys in the Parameters.

§Returns

An iterator over the keys.

§Examples
use parameterx::Parameters;

let params = Parameters::new();
for key in params.keys() {
    println!("{}", key);
}
Source

pub fn iter(&self) -> impl Iterator<Item = (&String, &Arc<dyn ParameterValue>)>

Get an iterator over the key-value pairs in the Parameters.

§Returns

An iterator over the key-value pairs.

§Examples
use parameterx::Parameters;

let params = Parameters::new();
for (key, value) in params.iter() {
    println!("{}: {:?}", key, value);
}
Source

pub fn to_json(&self) -> Result<Value>

Convert the Parameters to a JSON value.

§Returns

A Result containing the JSON value if successful, or a ParameterError if conversion failed.

§Examples
use parameterx::Parameters;

let params = Parameters::new();
let json = params.to_json().unwrap();

Trait Implementations§

Source§

impl Clone for Parameters

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Parameters

Source§

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

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

impl Default for Parameters

Source§

fn default() -> Parameters

Returns the “default value” for a type. Read more
Source§

impl From<&str> for Parameters

Source§

fn from(text: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Parameters

Source§

fn from(text: String) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(String, String)> for Parameters

Source§

fn from_iter<I: IntoIterator<Item = (String, String)>>(iter: I) -> Self

Creates a value from an iterator. 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 = 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.