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::*;
use crate::{compose::{opt::AllowUrl, Confirm, Opt},
text};
type MyOpt<'a> = Opt<'a, text::Plain, AllowUrl>;
#[derive(Clone, Debug, Hash, PartialEq, Ser, De)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct Overflow<'a> {
#[cfg_attr(feature = "validation", validate(length(max = 255)))]
action_id: Cow<'a, str>,
#[cfg_attr(feature = "validation", validate(length(min = 2, max = 5)))]
#[cfg_attr(feature = "validation", validate)]
options: Vec<MyOpt<'a>>,
#[cfg_attr(feature = "validation", validate)]
#[serde(skip_serializing_if = "Option::is_none")]
confirm: Option<Confirm>,
}
impl<'a> Overflow<'a> {
pub fn builder() -> build::OverflowBuilderInit<'a> {
build::OverflowBuilderInit::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)]
pub mod method {
#[derive(Copy, Clone, Debug)]
pub struct action_id;
#[derive(Copy, Clone, Debug)]
pub struct options;
}
pub type OverflowBuilderInit<'a> =
OverflowBuilder<'a,
RequiredMethodNotCalled<method::action_id>,
RequiredMethodNotCalled<method::options>>;
#[derive(Debug)]
pub struct OverflowBuilder<'a, A, O> {
action_id: Option<Cow<'a, str>>,
options: Option<Vec<MyOpt<'a>>>,
confirm: Option<Confirm>,
state: PhantomData<(A, O)>,
}
impl<'a, A, O> OverflowBuilder<'a, A, O> {
pub fn new() -> Self {
Self { action_id: None,
options: None,
confirm: None,
state: PhantomData::<_> }
}
fn cast_state<A2, O2>(self) -> OverflowBuilder<'a, A2, O2> {
OverflowBuilder { action_id: self.action_id,
options: self.options,
confirm: self.confirm,
state: PhantomData::<_> }
}
pub fn action_id<T>(mut self,
action_id: T)
-> OverflowBuilder<'a, Set<method::action_id>, O>
where T: Into<Cow<'a, str>>
{
self.action_id = Some(action_id.into());
self.cast_state()
}
pub fn options<U>(mut self,
options: Vec<Opt<'a, text::Plain, U>>)
-> OverflowBuilder<'a, A, Set<method::options>> {
self.options =
Some(options.into_iter().map(|o| o.as_allow_url()).collect());
self.cast_state()
}
pub fn option<U>(mut self,
option: Opt<'a, text::Plain, U>)
-> OverflowBuilder<'a, A, Set<method::options>> {
let options = match self.options {
| Some(mut options) => {
options.push(option.as_allow_url());
options
},
| None => vec![option.as_allow_url()],
};
self.options = Some(options);
self.cast_state()
}
#[cfg(feature = "blox")]
#[cfg_attr(docsrs, doc(cfg(feature = "blox")))]
pub fn child<U>(self,
option: Opt<'a, text::Plain, U>)
-> OverflowBuilder<'a, A, Set<method::options>> {
self.option(option)
}
pub fn confirm(mut self, confirm: Confirm) -> Self {
self.confirm = Some(confirm);
self
}
}
impl<'a> OverflowBuilder<'a, Set<method::action_id>, Set<method::options>> {
pub fn build(self) -> Overflow<'a> {
Overflow { action_id: self.action_id.unwrap(),
options: self.options.unwrap(),
confirm: self.confirm }
}
}
}