use std::borrow::Cow;
use serde::{Deserialize, Serialize};
#[cfg(feature = "validation")]
use validator::Validate;
use crate::text;
#[cfg(feature = "validation")]
use crate::val_helpr::*;
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct Header<'a> {
#[cfg_attr(feature = "validation", validate(custom = "validate_text"))]
text: text::Text,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "validation",
validate(custom = "super::validate_block_id"))]
block_id: Option<Cow<'a, str>>,
}
#[cfg(feature = "validation")]
fn validate_text(t: &text::Text) -> ValidatorResult {
below_len("text", 150, t)
}
impl<'a> Header<'a> {
pub fn builder() -> build::HeaderBuilderInit<'a> {
build::HeaderBuilder::new()
}
#[cfg(feature = "validation")]
#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
pub fn validate(&self) -> ValidationResult {
todo!()
}
}
pub mod build {
use std::marker::PhantomData;
use super::*;
use crate::build::*;
#[allow(non_camel_case_types)]
pub mod method {
#[derive(Debug, Clone, Copy)]
pub struct text;
}
pub type HeaderBuilderInit<'a> =
HeaderBuilder<'a, RequiredMethodNotCalled<method::text>>;
#[derive(Clone, Debug)]
pub struct HeaderBuilder<'a, T> {
text: Option<text::Text>,
block_id: Option<Cow<'a, str>>,
state: PhantomData<T>,
}
impl<'a, T> HeaderBuilder<'a, T> {
pub fn new() -> Self {
Self { text: None,
block_id: None,
state: PhantomData::<_> }
}
pub fn text(self,
text: impl Into<text::Plain>)
-> HeaderBuilder<'a, Set<method::text>> {
HeaderBuilder { text: Some(text.into().into()),
block_id: self.block_id,
state: PhantomData::<_> }
}
#[cfg(feature = "blox")]
#[cfg_attr(docsrs, doc(cfg(feature = "blox")))]
pub fn child(self,
text: impl Into<text::Plain>)
-> HeaderBuilder<'a, Set<method::text>> {
self.text(text)
}
pub fn block_id(mut self, block_id: impl Into<Cow<'a, str>>) -> Self {
self.block_id = Some(block_id.into());
self
}
}
impl<'a> HeaderBuilder<'a, Set<method::text>> {
pub fn build(self) -> Header<'a> {
Header { text: self.text.unwrap(),
block_id: self.block_id }
}
}
}