use serde::{Deserialize, Serialize};
#[cfg(feature = "validation")]
use validator::Validate;
use crate::text;
#[cfg(feature = "validation")]
use crate::val_helpr::ValidationResult;
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct Confirm {
#[cfg_attr(feature = "validation", validate(custom = "validate::title"))]
title: text::Text,
#[cfg_attr(feature = "validation", validate(custom = "validate::text"))]
text: text::Text,
#[cfg_attr(feature = "validation", validate(custom = "validate::confirm"))]
confirm: text::Text,
#[cfg_attr(feature = "validation", validate(custom = "validate::deny"))]
deny: text::Text,
#[serde(skip_serializing_if = "Option::is_none")]
style: Option<ConfirmStyle>,
}
impl Confirm {
pub fn builder() -> build::ConfirmBuilderInit {
build::ConfirmBuilderInit::new()
}
#[cfg(feature = "validation")]
#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
pub fn validate(&self) -> ValidationResult {
Validate::validate(self)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfirmStyle {
Danger,
Primary,
}
pub mod build {
use std::marker::PhantomData;
use super::*;
use crate::build::*;
#[allow(non_camel_case_types)]
pub mod method {
#[derive(Debug, Copy, Clone)]
pub struct title;
#[derive(Debug, Copy, Clone)]
pub struct text;
#[derive(Debug, Copy, Clone)]
pub struct confirm;
#[derive(Debug, Copy, Clone)]
pub struct deny;
}
pub type ConfirmBuilderInit =
ConfirmBuilder<RequiredMethodNotCalled<method::title>,
RequiredMethodNotCalled<method::text>,
RequiredMethodNotCalled<method::confirm>,
RequiredMethodNotCalled<method::deny>>;
#[derive(Debug)]
pub struct ConfirmBuilder<Title, Text, Confirm, Deny> {
title: Option<text::Text>,
text: Option<text::Text>,
confirm: Option<text::Text>,
deny: Option<text::Text>,
style: Option<ConfirmStyle>,
state: PhantomData<(Title, Text, Confirm, Deny)>,
}
impl<Title, Text, Confirm, Deny> ConfirmBuilder<Title, Text, Confirm, Deny> {
pub fn new() -> Self {
Self { title: None,
text: None,
confirm: None,
deny: None,
style: None,
state: PhantomData::<_> }
}
pub fn style(mut self, style: ConfirmStyle) -> Self {
self.style = Some(style);
self
}
pub fn title(self,
t: impl Into<text::Plain>)
-> ConfirmBuilder<Set<method::title>, Text, Confirm, Deny>
{
ConfirmBuilder { text: self.text,
title: Some(t.into().into()),
confirm: self.confirm,
deny: self.deny,
style: self.style,
state: PhantomData::<_> }
}
pub fn confirm(
self,
t: impl Into<text::Plain>)
-> ConfirmBuilder<Title, Text, Set<method::confirm>, Deny> {
ConfirmBuilder { text: self.text,
title: self.title,
confirm: Some(t.into().into()),
deny: self.deny,
style: self.style,
state: PhantomData::<_> }
}
pub fn deny(self,
t: impl Into<text::Plain>)
-> ConfirmBuilder<Title, Text, Confirm, Set<method::deny>> {
ConfirmBuilder { text: self.text,
title: self.title,
confirm: self.confirm,
deny: Some(t.into().into()),
style: self.style,
state: PhantomData::<_> }
}
pub fn text(self,
t: impl Into<text::Text>)
-> ConfirmBuilder<Title, Set<method::text>, Confirm, Deny> {
ConfirmBuilder { text: Some(t.into().into()),
title: self.title,
confirm: self.confirm,
deny: self.deny,
style: self.style,
state: PhantomData::<_> }
}
pub fn text_plain(
self,
t: impl Into<text::Plain>)
-> ConfirmBuilder<Title, Set<method::text>, Confirm, Deny> {
self.text(t.into())
}
pub fn text_md(
self,
t: impl Into<text::Mrkdwn>)
-> ConfirmBuilder<Title, Set<method::text>, Confirm, Deny> {
self.text(t.into())
}
}
impl
ConfirmBuilder<Set<method::title>,
Set<method::text>,
Set<method::confirm>,
Set<method::deny>>
{
pub fn build(self) -> Confirm {
Confirm { text: self.text.unwrap(),
title: self.title.unwrap(),
confirm: self.confirm.unwrap(),
deny: self.deny.unwrap(),
style: self.style }
}
}
}
#[cfg(feature = "validation")]
mod validate {
use crate::{text, val_helpr::*};
pub(super) fn text(text: &text::Text) -> ValidatorResult {
below_len("Confirmation Dialog text", 300, text.as_ref())
}
pub(super) fn title(text: &text::Text) -> ValidatorResult {
below_len("Confirmation Dialog title", 100, text.as_ref())
}
pub(super) fn confirm(text: &text::Text) -> ValidatorResult {
below_len("Confirmation Dialog confirmation text", 30, text.as_ref())
}
pub(super) fn deny(text: &text::Text) -> ValidatorResult {
below_len("Confirmation Dialog deny text", 30, text.as_ref())
}
}