#[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>
impl<EB: Clone> Clone for MultilineListBuilder<EB>
source§fn clone(&self) -> MultilineListBuilder<EB>
fn clone(&self) -> MultilineListBuilder<EB>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<EB: Debug> Debug for MultilineListBuilder<EB>
impl<EB: Debug> Debug for MultilineListBuilder<EB>
source§impl<EB> Default for MultilineListBuilder<EB>
impl<EB> Default for MultilineListBuilder<EB>
source§impl<'de, EB: Deserialize<'de>> Deserialize<'de> for MultilineListBuilder<EB>
impl<'de, EB: Deserialize<'de>> Deserialize<'de> for MultilineListBuilder<EB>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
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> Serialize for MultilineListBuilder<EB>where
EB: Serialize,
impl<EB> Serialize for MultilineListBuilder<EB>where EB: Serialize,
Auto Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more