Skip to main content

Params

Struct Params 

Source
pub struct Params(/* private fields */);
Expand description

The parameters of a custom control kind, by name.

These are every field a manifest carried beside kind for a kind the library does not know, kept as data so the factory registered for the kind can read them. The accessors answer None when a parameter is absent or of another type, so a factory reports what it needed rather than guessing.

§Examples

use pamoja_profile::{Param, Params};

let params = Params::new()
    .with("warn_below", 2.0)
    .with("latching", true)
    .with("zone", "north");
assert_eq!(params.number("warn_below"), Some(2.0));
assert_eq!(params.flag("latching"), Some(true));
assert_eq!(params.text("zone"), Some("north"));
assert_eq!(params.number("zone"), None);
assert_eq!(params.get("missing"), None);
assert!(matches!(params.get("zone"), Some(Param::Text(_))));

Implementations§

Source§

impl Params

Source

pub fn new() -> Self

Starts with no parameters.

§Returns

An empty set.

Source

pub fn with(self, name: impl Into<String>, value: impl Into<Param>) -> Self

Adds a parameter, replacing one of the same name.

§Arguments
  • name - the parameter’s name, as the manifest writes it.
  • value - a number, a flag, or text.
§Returns

The parameters, for chaining.

Source

pub fn get(&self, name: &str) -> Option<&Param>

Looks a parameter up by name.

§Arguments
  • name - the parameter’s name.
§Returns

The parameter, or None when the manifest carried none of that name.

Source

pub fn number(&self, name: &str) -> Option<f64>

Reads a numeric parameter.

§Arguments
  • name - the parameter’s name.
§Returns

The number, or None when the parameter is absent or not a number.

Source

pub fn flag(&self, name: &str) -> Option<bool>

Reads a flag parameter.

§Arguments
  • name - the parameter’s name.
§Returns

The flag, or None when the parameter is absent or not a flag.

Source

pub fn text(&self, name: &str) -> Option<&str>

Reads a text parameter.

§Arguments
  • name - the parameter’s name.
§Returns

The text, or None when the parameter is absent or not text.

Source

pub fn from_json(text: &str) -> Result<Self>

Loads parameters from the JSON object a manifest carries beside a custom kind.

§Arguments
  • text - the JSON object.
§Returns

The parameters.

§Errors

Returns Error::Codec if the text is not an object of numbers, flags, and text.

Source

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

Writes the parameters as the JSON object a manifest carries beside a custom kind.

§Returns

The JSON text, on one line.

§Errors

Returns Error::Codec if the parameters cannot be serialized.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Param)>

Walks the parameters in name order.

§Returns

Each name with its parameter.

Source

pub fn len(&self) -> usize

Counts the parameters.

§Returns

How many the manifest carried.

Source

pub fn is_empty(&self) -> bool

Whether there are no parameters.

§Returns

true when the manifest carried only the kind.

Trait Implementations§

Source§

impl Clone for Params

Source§

fn clone(&self) -> Params

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 Params

Source§

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

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

impl Default for Params

Source§

fn default() -> Params

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

impl<'de> Deserialize<'de> for Params

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<BTreeMap<String, Param>> for Params

Source§

fn from(map: BTreeMap<String, Param>) -> Self

Converts to this type from the input type.
Source§

impl From<Params> for BTreeMap<String, Param>

Source§

fn from(params: Params) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Params

Source§

fn eq(&self, other: &Params) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Params

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Params

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.