// @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;

#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
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::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.

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Project<'a> {
    #[serde(borrow)]
    pub description: CowStr<'a>,
    #[serde(borrow)]
    pub language: CowStr<'a>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub link: Option<UriValue<'a>>,
    #[serde(borrow)]
    pub repo: UriValue<'a>,
    #[serde(borrow)]
    pub title: CowStr<'a>,
}

/// 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<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
    #[serde(borrow)]
    pub uri: AtUri<'a>,
    #[serde(borrow)]
    pub value: Project<'a>,
}

impl<'a> Project<'a> {
    pub fn uri(
        uri: impl Into<CowStr<'a>>,
    ) -> Result<RecordUri<'a, ProjectRecord>, UriError> {
        RecordUri::try_from_uri(AtUri::new_cow(uri.into())?)
    }
}

/// 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<'de> = ProjectGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

impl From<ProjectGetRecordOutput<'_>> for Project<'_> {
    fn from(output: ProjectGetRecordOutput<'_>) -> Self {
        use jacquard_common::IntoStatic;
        output.value.into_static()
    }
}

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

/// Builder for constructing an instance of this type
pub struct ProjectBuilder<'a, S: project_state::State> {
    _state: PhantomData<fn() -> S>,
    _fields: (
        Option<CowStr<'a>>,
        Option<CowStr<'a>>,
        Option<UriValue<'a>>,
        Option<UriValue<'a>>,
        Option<CowStr<'a>>,
    ),
    _lifetime: PhantomData<&'a ()>,
}

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

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

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

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

impl<'a, S: project_state::State> ProjectBuilder<'a, S> {
    /// Set the `link` field (optional)
    pub fn link(mut self, value: impl Into<Option<UriValue<'a>>>) -> 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<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

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

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

impl<'a, S> ProjectBuilder<'a, S>
where
    S: project_state::State,
    S::Repo: project_state::IsSet,
    S::Title: project_state::IsSet,
    S::Description: project_state::IsSet,
    S::Language: project_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Project<'a> {
        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<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> Project<'a> {
        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()
    }
}