use std::borrow::Cow;
use serde::{Deserialize as De, Serialize as Ser};
#[cfg(feature = "validation")]
use validator::Validate;
#[cfg(feature = "validation")]
use crate::val_helpr::ValidationResult;
use crate::{compose::{opt::{AnyText, NoUrl},
Confirm,
Opt},
text};
pub type RadioButtonOpt<'a> = Opt<'a, AnyText, NoUrl>;
#[derive(Clone, Debug, Hash, PartialEq, Ser, De)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct Radio<'a> {
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
action_id: Cow<'a, str>,
#[cfg_attr(feature = "validation", validate(length(max = 10)))]
#[cfg_attr(feature = "validation", validate)]
options: Vec<RadioButtonOpt<'a>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "validation", validate)]
initial_option: Option<RadioButtonOpt<'a>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "validation", validate)]
confirm: Option<Confirm>,
}
impl<'a> Radio<'a> {
pub fn builder() -> build::RadioBuilderInit<'a> {
build::RadioBuilderInit::new()
}
#[cfg(feature = "validation")]
#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
pub fn validate(&self) -> ValidationResult {
Validate::validate(self)
}
}
pub mod build {
use std::marker::PhantomData;
use super::*;
use crate::build::*;
#[allow(non_camel_case_types)]
mod method {
#[derive(Copy, Clone, Debug)]
pub struct action_id;
#[derive(Copy, Clone, Debug)]
pub struct options;
}
pub type RadioBuilderInit<'a> =
RadioBuilder<'a,
AnyText,
RequiredMethodNotCalled<method::action_id>,
RequiredMethodNotCalled<method::options>>;
#[derive(Debug)]
pub struct RadioBuilder<'a, T, A, O> {
action_id: Option<Cow<'a, str>>,
options: Option<Vec<RadioButtonOpt<'a>>>,
initial_option: Option<RadioButtonOpt<'a>>,
confirm: Option<Confirm>,
state: PhantomData<(T, A, O)>,
}
impl<'a, T, A, O> RadioBuilder<'a, T, A, O> {
pub fn new() -> Self {
Self { action_id: None,
options: None,
initial_option: None,
confirm: None,
state: PhantomData::<_> }
}
fn cast_state<A2, O2>(self) -> RadioBuilder<'a, T, A2, O2> {
RadioBuilder { action_id: self.action_id,
options: self.options,
initial_option: self.initial_option,
confirm: self.confirm,
state: PhantomData::<_> }
}
pub fn action_id<S>(mut self,
action_id: S)
-> RadioBuilder<'a, T, Set<method::action_id>, O>
where S: Into<Cow<'a, str>>
{
self.action_id = Some(action_id.into());
self.cast_state()
}
pub fn options<I, T2: Into<text::Text>>(
self,
options: I)
-> RadioBuilder<'a, T2, A, Set<method::options>>
where I: IntoIterator<Item = Opt<'a, T2, NoUrl>>
{
let options = options.into_iter().map(|o| o.into()).collect();
RadioBuilder { action_id: self.action_id,
options: Some(options),
initial_option: self.initial_option,
confirm: self.confirm,
state: PhantomData::<_> }
}
pub fn option<T2: Into<text::Text>>(
self,
opt: Opt<'a, T2, NoUrl>)
-> RadioBuilder<'a, T2, A, Set<method::options>> {
let options = match self.options {
| Some(mut os) => {
os.push(opt.into());
os
},
| None => vec![opt.into()],
};
RadioBuilder { action_id: self.action_id,
options: Some(options),
initial_option: self.initial_option,
confirm: self.confirm,
state: PhantomData::<_> }
}
pub fn initial_option<T2>(mut self, option: Opt<'a, T2, NoUrl>) -> Self
where T2: Into<text::Text>
{
self.initial_option = Some(option.into());
self
}
#[cfg(feature = "blox")]
#[cfg_attr(docsrs, doc(cfg(feature = "blox")))]
pub fn child<T2: Into<text::Text>>(
self,
opt: Opt<'a, T2, NoUrl>)
-> RadioBuilder<'a, T2, A, Set<method::options>> {
self.option(opt)
}
pub fn confirm(mut self, confirm: Confirm) -> Self {
self.confirm = Some(confirm);
self
}
}
impl<'a, T> RadioBuilder<'a, T, Set<method::action_id>, Set<method::options>> {
pub fn build(self) -> Radio<'a> {
Radio { action_id: self.action_id.unwrap(),
options: self.options.unwrap(),
initial_option: self.initial_option,
confirm: self.confirm }
}
}
}