Enum tor_config::list_builder::MultilineListBuilder

source ·
#[non_exhaustive]
pub enum MultilineListBuilder<EB> { Unspecified, String(String), List(Vec<EB>), }
Expand description

Configuration item specifiable as a list, or a single multi-line string

If a list is supplied, they are deserialized as builders. If a single string is supplied, it is split into lines, and #-comments and blank lines and whitespace are stripped, and then each line is parsed as a builder. (Eventually, the builders will be built.)

For use with sub_builder and define_list_builder_helper, with #[serde(try_from)] and #[serde(into)].

§Example

use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use tor_config::{ConfigBuildError, MultilineListBuilder};
use tor_config::convert_helper_via_multi_line_list_builder;
use tor_config::{define_list_builder_accessors, define_list_builder_helper};
use tor_config::impl_standard_builder;


#[derive(Debug, Clone, Builder, Eq, PartialEq)]
#[builder(build_fn(error = "ConfigBuildError"))]
#[builder(derive(Debug, Serialize, Deserialize))]
#[non_exhaustive]
pub struct LotteryConfig {
    /// What numbers should win the lottery?  Setting this is lottery fraud.
    #[builder(sub_builder, setter(custom))]
    #[builder_field_attr(serde(default))]
    winners: LotteryNumberList,
}
impl_standard_builder! { LotteryConfig }

/// List of lottery winners
//
// This type alias arranges that we can put `LotteryNumberList` in `LotteryConfig`
// and have derive_builder put a `LotteryNumberListBuilder` in `LotteryConfigBuilder`.
pub type LotteryNumberList = Vec<u16>;

define_list_builder_helper! {
    struct LotteryNumberListBuilder {
        numbers: [u16],
    }
    built: LotteryNumberList = numbers;
    default = generate_random();
    item_build: |number| Ok(*number);
    #[serde(try_from="MultilineListBuilder<u16>")]
    #[serde(into="MultilineListBuilder<u16>")]
}

convert_helper_via_multi_line_list_builder! {
    struct LotteryNumberListBuilder {
        numbers: [u16],
    }
}

define_list_builder_accessors! {
    struct LotteryConfigBuilder {
        pub winners: [u16],
    }
}

let lc: LotteryConfigBuilder = toml::from_str(r#"winners = [1,2,3]"#).unwrap();
let lc = lc.build().unwrap();
assert_eq!{ lc.winners, [1,2,3] }

let lc = r#"
winners = '''
  # Enny tells us this is the ticket they bought:

  4
  5
  6
'''
"#;
let lc: LotteryConfigBuilder = toml::from_str(lc).unwrap();
let lc = lc.build().unwrap();
assert_eq!{ lc.winners, [4,5,6] }

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Unspecified

Config key not present

§

String(String)

Config key was a string which is to be parsed line-by-line

§

List(Vec<EB>)

Config key was a list of the individual entry builders

Trait Implementations§

source§

impl<EB: Clone> Clone for MultilineListBuilder<EB>

source§

fn clone(&self) -> MultilineListBuilder<EB>

Returns a copy 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<EB: Debug> Debug for MultilineListBuilder<EB>

source§

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

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

impl<EB> Default for MultilineListBuilder<EB>

source§

fn default() -> MultilineListBuilder<EB>

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

impl<'de, EB: Deserialize<'de>> Deserialize<'de> for MultilineListBuilder<EB>

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<EB> From<Option<Vec<EB>>> for MultilineListBuilder<EB>

source§

fn from(list: Option<Vec<EB>>) -> Self

Converts to this type from the input type.
source§

impl<EB> Serialize for MultilineListBuilder<EB>
where EB: Serialize,

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<EB> TryInto<Option<Vec<EB>>> for MultilineListBuilder<EB>
where EB: FromStr, EB::Err: Error + Clone + Send + Sync,

§

type Error = MultilineListBuilderError<<EB as FromStr>::Err>

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

fn try_into(self) -> Result<Option<Vec<EB>>, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<EB> Freeze for MultilineListBuilder<EB>

§

impl<EB> RefUnwindSafe for MultilineListBuilder<EB>
where EB: RefUnwindSafe,

§

impl<EB> Send for MultilineListBuilder<EB>
where EB: Send,

§

impl<EB> Sync for MultilineListBuilder<EB>
where EB: Sync,

§

impl<EB> Unpin for MultilineListBuilder<EB>
where EB: Unpin,

§

impl<EB> UnwindSafe for MultilineListBuilder<EB>
where EB: UnwindSafe,

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

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

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

§

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>,

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

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
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,