// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: com.chrisvanderloo.project
//
// 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::{CowStr, BosStr, DefaultStr, FromStaticStr};

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, UriValue};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
/// A project for display on my website.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "com.chrisvanderloo.project",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Project<S: BosStr = DefaultStr> {
    pub description: S,
    pub language: S,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link: Option<UriValue<S>>,
    pub repo: UriValue<S>,
    pub title: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// Typed wrapper for GetRecord response with this collection's record type.

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct ProjectGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Project<S>,
}

impl<S: BosStr> Project<S> {
    pub fn uri(uri: S) -> Result<RecordUri<S, ProjectRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new(uri)?)
    }
}

/// Marker type for deserializing records from this collection.

#[derive(Debug, Serialize, Deserialize)]
pub struct ProjectRecord;
impl XrpcResp for ProjectRecord {
    const NSID: &'static str = "com.chrisvanderloo.project";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = ProjectGetRecordOutput<S>;
    type Err = RecordError;
}

impl<S: BosStr> From<ProjectGetRecordOutput<S>> for Project<S> {
    fn from(output: ProjectGetRecordOutput<S>) -> Self {
        output.value
    }
}

impl<S: BosStr> Collection for Project<S> {
    const NSID: &'static str = "com.chrisvanderloo.project";
    type Record = ProjectRecord;
}

impl Collection for ProjectRecord {
    const NSID: &'static str = "com.chrisvanderloo.project";
    type Record = ProjectRecord;
}

impl<S: BosStr> LexiconSchema for Project<S> {
    fn nsid() -> &'static str {
        "com.chrisvanderloo.project"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_com_chrisvanderloo_project()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.description;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2048usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("description"),
                    max: 2048usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.language;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 64usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("language"),
                    max: 64usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        if let Some(ref value) = self.link {
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2048usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("link"),
                    max: 2048usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.repo;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 2048usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("repo"),
                    max: 2048usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        {
            let value = &self.title;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 120usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("title"),
                    max: 120usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

pub mod project_state {

    pub use crate::builder_types::{Set, Unset, IsSet, IsUnset};
    #[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 Description;
        type Language;
        type Repo;
        type Title;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Description = Unset;
        type Language = Unset;
        type Repo = Unset;
        type Title = Unset;
    }
    ///State transition - sets the `description` field to Set
    pub struct SetDescription<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetDescription<St> {}
    impl<St: State> State for SetDescription<St> {
        type Description = Set<members::description>;
        type Language = St::Language;
        type Repo = St::Repo;
        type Title = St::Title;
    }
    ///State transition - sets the `language` field to Set
    pub struct SetLanguage<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetLanguage<St> {}
    impl<St: State> State for SetLanguage<St> {
        type Description = St::Description;
        type Language = Set<members::language>;
        type Repo = St::Repo;
        type Title = St::Title;
    }
    ///State transition - sets the `repo` field to Set
    pub struct SetRepo<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetRepo<St> {}
    impl<St: State> State for SetRepo<St> {
        type Description = St::Description;
        type Language = St::Language;
        type Repo = Set<members::repo>;
        type Title = St::Title;
    }
    ///State transition - sets the `title` field to Set
    pub struct SetTitle<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetTitle<St> {}
    impl<St: State> State for SetTitle<St> {
        type Description = St::Description;
        type Language = St::Language;
        type Repo = St::Repo;
        type Title = Set<members::title>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `description` field
        pub struct description(());
        ///Marker type for the `language` field
        pub struct language(());
        ///Marker type for the `repo` field
        pub struct repo(());
        ///Marker type for the `title` field
        pub struct title(());
    }
}

/// Builder for constructing an instance of this type.
pub struct ProjectBuilder<St: project_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<S>, Option<S>, Option<UriValue<S>>, Option<UriValue<S>>, Option<S>),
    _type: PhantomData<fn() -> S>,
}

impl Project<DefaultStr> {
    /// Create a new builder for this type, using the default string type (DefaultStr = SmolStr) if needed
    pub fn new() -> ProjectBuilder<project_state::Empty, DefaultStr> {
        ProjectBuilder::new()
    }
}

impl<S: BosStr> Project<S> {
    /// Create a new builder for this type
    pub fn builder() -> ProjectBuilder<project_state::Empty, S> {
        ProjectBuilder::builder()
    }
}

impl ProjectBuilder<project_state::Empty, DefaultStr> {
    /// Create a new builder with all fields unset, using the default string type, if needed
    pub fn new() -> Self {
        ProjectBuilder {
            _state: PhantomData,
            _fields: (None, None, None, None, None),
            _type: PhantomData,
        }
    }
}

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

impl<St, S: BosStr> ProjectBuilder<St, S>
where
    St: project_state::State,
    St::Description: project_state::IsUnset,
{
    /// Set the `description` field (required)
    pub fn description(
        mut self,
        value: impl Into<S>,
    ) -> ProjectBuilder<project_state::SetDescription<St>, S> {
        self._fields.0 = Option::Some(value.into());
        ProjectBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProjectBuilder<St, S>
where
    St: project_state::State,
    St::Language: project_state::IsUnset,
{
    /// Set the `language` field (required)
    pub fn language(
        mut self,
        value: impl Into<S>,
    ) -> ProjectBuilder<project_state::SetLanguage<St>, S> {
        self._fields.1 = Option::Some(value.into());
        ProjectBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: project_state::State, S: BosStr> ProjectBuilder<St, S> {
    /// Set the `link` field (optional)
    pub fn link(mut self, value: impl Into<Option<UriValue<S>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `link` field to an Option value (optional)
    pub fn maybe_link(mut self, value: Option<UriValue<S>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<St, S: BosStr> ProjectBuilder<St, S>
where
    St: project_state::State,
    St::Repo: project_state::IsUnset,
{
    /// Set the `repo` field (required)
    pub fn repo(
        mut self,
        value: impl Into<UriValue<S>>,
    ) -> ProjectBuilder<project_state::SetRepo<St>, S> {
        self._fields.3 = Option::Some(value.into());
        ProjectBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProjectBuilder<St, S>
where
    St: project_state::State,
    St::Title: project_state::IsUnset,
{
    /// Set the `title` field (required)
    pub fn title(
        mut self,
        value: impl Into<S>,
    ) -> ProjectBuilder<project_state::SetTitle<St>, S> {
        self._fields.4 = Option::Some(value.into());
        ProjectBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> ProjectBuilder<St, S>
where
    St: project_state::State,
    St::Description: project_state::IsSet,
    St::Language: project_state::IsSet,
    St::Repo: project_state::IsSet,
    St::Title: project_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Project<S> {
        Project {
            description: self._fields.0.unwrap(),
            language: self._fields.1.unwrap(),
            link: self._fields.2,
            repo: self._fields.3.unwrap(),
            title: self._fields.4.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>>) -> Project<S> {
        Project {
            description: self._fields.0.unwrap(),
            language: self._fields.1.unwrap(),
            link: self._fields.2,
            repo: self._fields.3.unwrap(),
            title: self._fields.4.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_com_chrisvanderloo_project() -> LexiconDoc<'static> {
    #[allow(unused_imports)]
    use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
    use jacquard_lexicon::lexicon::*;
    use alloc::collections::BTreeMap;
    LexiconDoc {
        lexicon: Lexicon::Lexicon1,
        id: CowStr::new_static("com.chrisvanderloo.project"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("A project for display on my website."),
                    ),
                    key: Some(CowStr::new_static("any")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("title"),
                                SmolStr::new_static("description"),
                                SmolStr::new_static("repo"), SmolStr::new_static("language")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::String(LexString {
                                    max_length: Some(2048usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("language"),
                                LexObjectProperty::String(LexString {
                                    max_length: Some(64usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("link"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Uri),
                                    max_length: Some(2048usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("repo"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Uri),
                                    max_length: Some(2048usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("title"),
                                LexObjectProperty::String(LexString {
                                    max_length: Some(120usize),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}