jacquard-api 0.12.0-beta.2

Generated AT Protocol API bindings for Jacquard
Documentation
// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: fyi.questionable.richtext.header
//
// This file was automatically generated from Lexicon schemas.
// Any manual changes will be overwritten on the next regeneration.

#[allow(unused_imports)]
use alloc::collections::BTreeMap;

#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{BosStr, CowStr, DefaultStr, FromStaticStr};

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::value::Data;
use jacquard_derive::IntoStatic;
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

use crate::fyi_questionable::richtext::facet::Facet;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Header<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub facets: Option<Vec<Facet<S>>>,
    pub level: i64,
    pub plaintext: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

impl<S: BosStr> LexiconSchema for Header<S> {
    fn nsid() -> &'static str {
        "fyi.questionable.richtext.header"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_fyi_questionable_richtext_header()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.level;
            if *value > 6i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("level"),
                    max: 6i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.level;
            if *value < 1i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("level"),
                    min: 1i64,
                    actual: *value,
                });
            }
        }
        {
            let value = &self.plaintext;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2000usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("plaintext"),
                    max: 2000usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.plaintext;
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 1000usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("plaintext"),
                        max: 1000usize,
                        actual: count,
                    });
                }
            }
        }
        Ok(())
    }
}

pub mod header_state {

    pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
    #[allow(unused)]
    use ::core::marker::PhantomData;
    mod sealed {
        pub trait Sealed {}
    }
    /// State trait tracking which required fields have been set
    pub trait State: sealed::Sealed {
        type Plaintext;
        type Level;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Plaintext = Unset;
        type Level = Unset;
    }
    ///State transition - sets the `plaintext` field to Set
    pub struct SetPlaintext<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetPlaintext<St> {}
    impl<St: State> State for SetPlaintext<St> {
        type Plaintext = Set<members::plaintext>;
        type Level = St::Level;
    }
    ///State transition - sets the `level` field to Set
    pub struct SetLevel<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetLevel<St> {}
    impl<St: State> State for SetLevel<St> {
        type Plaintext = St::Plaintext;
        type Level = Set<members::level>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `plaintext` field
        pub struct plaintext(());
        ///Marker type for the `level` field
        pub struct level(());
    }
}

/// Builder for constructing an instance of this type.
pub struct HeaderBuilder<S: BosStr, St: header_state::State> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<Vec<Facet<S>>>, Option<i64>, Option<S>),
    _type: PhantomData<fn() -> S>,
}

impl<S: BosStr> Header<S> {
    /// Create a new builder for this type.
    pub fn new() -> HeaderBuilder<S, header_state::Empty> {
        HeaderBuilder::new()
    }
}

impl<S: BosStr> HeaderBuilder<S, header_state::Empty> {
    /// Create a new builder with all fields unset.
    pub fn new() -> Self {
        HeaderBuilder {
            _state: PhantomData,
            _fields: (None, None, None),
            _type: PhantomData,
        }
    }
}

impl<S: BosStr, St: header_state::State> HeaderBuilder<S, St> {
    /// Set the `facets` field (optional)
    pub fn facets(mut self, value: impl Into<Option<Vec<Facet<S>>>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `facets` field to an Option value (optional)
    pub fn maybe_facets(mut self, value: Option<Vec<Facet<S>>>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<S: BosStr, St> HeaderBuilder<S, St>
where
    St: header_state::State,
    St::Level: header_state::IsUnset,
{
    /// Set the `level` field (required)
    pub fn level(mut self, value: impl Into<i64>) -> HeaderBuilder<S, header_state::SetLevel<St>> {
        self._fields.1 = Option::Some(value.into());
        HeaderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<S: BosStr, St> HeaderBuilder<S, St>
where
    St: header_state::State,
    St::Plaintext: header_state::IsUnset,
{
    /// Set the `plaintext` field (required)
    pub fn plaintext(
        mut self,
        value: impl Into<S>,
    ) -> HeaderBuilder<S, header_state::SetPlaintext<St>> {
        self._fields.2 = Option::Some(value.into());
        HeaderBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<S: BosStr, St> HeaderBuilder<S, St>
where
    St: header_state::State,
    St::Plaintext: header_state::IsSet,
    St::Level: header_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Header<S> {
        Header {
            facets: self._fields.0,
            level: self._fields.1.unwrap(),
            plaintext: self._fields.2.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Header<S> {
        Header {
            facets: self._fields.0,
            level: self._fields.1.unwrap(),
            plaintext: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_fyi_questionable_richtext_header() -> LexiconDoc<'static> {
    use alloc::collections::BTreeMap;
    #[allow(unused_imports)]
    use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
    use jacquard_lexicon::lexicon::*;
    LexiconDoc {
        lexicon: Lexicon::Lexicon1,
        id: CowStr::new_static("fyi.questionable.richtext.header"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Object(LexObject {
                    required: Some(vec![
                        SmolStr::new_static("plaintext"),
                        SmolStr::new_static("level"),
                    ]),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("facets"),
                            LexObjectProperty::Array(LexArray {
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("fyi.questionable.richtext.facet"),
                                    ..Default::default()
                                }),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("level"),
                            LexObjectProperty::Integer(LexInteger {
                                minimum: Some(1i64),
                                maximum: Some(6i64),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("plaintext"),
                            LexObjectProperty::String(LexString {
                                max_length: Some(2000usize),
                                max_graphemes: Some(1000usize),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}