// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: sh.tangled.repo
//
// This file was automatically generated from Lexicon schemas.
// Any manual changes will be overwritten on the next regeneration.

pub mod add_secret;
pub mod archive;
pub mod artifact;
pub mod blob;
pub mod branch;
pub mod branches;
pub mod collaborator;
pub mod compare;
pub mod create;
pub mod delete;
pub mod delete_branch;
pub mod diff;
pub mod fork_status;
pub mod fork_sync;
pub mod get_default_branch;
pub mod hidden_ref;
pub mod issue;
pub mod languages;
pub mod list_secrets;
pub mod log;
pub mod merge;
pub mod merge_check;
pub mod pull;
pub mod remove_secret;
pub mod set_default_branch;
pub mod tag;
pub mod tags;
pub mod tree;


#[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, Datetime, 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};

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct Repo<'a> {
    pub created_at: Datetime,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub description: Option<CowStr<'a>>,
    ///knot where the repo was created
    #[serde(borrow)]
    pub knot: CowStr<'a>,
    ///List of labels that this repo subscribes to
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub labels: Option<Vec<AtUri<'a>>>,
    ///name of the repo
    #[serde(borrow)]
    pub name: CowStr<'a>,
    ///source of the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub source: Option<UriValue<'a>>,
    ///CI runner to send jobs to and receive results from
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub spindle: Option<CowStr<'a>>,
    ///Topics related to the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub topics: Option<Vec<CowStr<'a>>>,
    ///Any URI related to the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub website: Option<UriValue<'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 RepoGetRecordOutput<'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: Repo<'a>,
}

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

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

#[derive(Debug, Serialize, Deserialize)]
pub struct RepoRecord;
impl XrpcResp for RepoRecord {
    const NSID: &'static str = "sh.tangled.repo";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = RepoGetRecordOutput<'de>;
    type Err<'de> = RecordError<'de>;
}

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

impl Collection for Repo<'_> {
    const NSID: &'static str = "sh.tangled.repo";
    type Record = RepoRecord;
}

impl Collection for RepoRecord {
    const NSID: &'static str = "sh.tangled.repo";
    type Record = RepoRecord;
}

impl<'a> LexiconSchema for Repo<'a> {
    fn nsid() -> &'static str {
        "sh.tangled.repo"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_sh_tangled_repo()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.description {
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count > 140usize {
                    return Err(ConstraintError::MaxGraphemes {
                        path: ValidationPath::from_field("description"),
                        max: 140usize,
                        actual: count,
                    });
                }
            }
        }
        if let Some(ref value) = self.description {
            {
                let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
                if count < 1usize {
                    return Err(ConstraintError::MinGraphemes {
                        path: ValidationPath::from_field("description"),
                        min: 1usize,
                        actual: count,
                    });
                }
            }
        }
        if let Some(ref value) = self.topics {
            #[allow(unused_comparisons)]
            if value.len() > 50usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("topics"),
                    max: 50usize,
                    actual: value.len(),
                });
            }
        }
        Ok(())
    }
}

pub mod repo_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 CreatedAt;
        type Knot;
        type Name;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type CreatedAt = Unset;
        type Knot = Unset;
        type Name = Unset;
    }
    ///State transition - sets the `created_at` field to Set
    pub struct SetCreatedAt<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetCreatedAt<S> {}
    impl<S: State> State for SetCreatedAt<S> {
        type CreatedAt = Set<members::created_at>;
        type Knot = S::Knot;
        type Name = S::Name;
    }
    ///State transition - sets the `knot` field to Set
    pub struct SetKnot<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetKnot<S> {}
    impl<S: State> State for SetKnot<S> {
        type CreatedAt = S::CreatedAt;
        type Knot = Set<members::knot>;
        type Name = S::Name;
    }
    ///State transition - sets the `name` field to Set
    pub struct SetName<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetName<S> {}
    impl<S: State> State for SetName<S> {
        type CreatedAt = S::CreatedAt;
        type Knot = S::Knot;
        type Name = Set<members::name>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `created_at` field
        pub struct created_at(());
        ///Marker type for the `knot` field
        pub struct knot(());
        ///Marker type for the `name` field
        pub struct name(());
    }
}

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

impl<'a> Repo<'a> {
    /// Create a new builder for this type
    pub fn new() -> RepoBuilder<'a, repo_state::Empty> {
        RepoBuilder::new()
    }
}

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

impl<'a, S> RepoBuilder<'a, S>
where
    S: repo_state::State,
    S::CreatedAt: repo_state::IsUnset,
{
    /// Set the `createdAt` field (required)
    pub fn created_at(
        mut self,
        value: impl Into<Datetime>,
    ) -> RepoBuilder<'a, repo_state::SetCreatedAt<S>> {
        self._fields.0 = Option::Some(value.into());
        RepoBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `description` field (optional)
    pub fn description(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `description` field to an Option value (optional)
    pub fn maybe_description(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S> RepoBuilder<'a, S>
where
    S: repo_state::State,
    S::Knot: repo_state::IsUnset,
{
    /// Set the `knot` field (required)
    pub fn knot(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> RepoBuilder<'a, repo_state::SetKnot<S>> {
        self._fields.2 = Option::Some(value.into());
        RepoBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `labels` field (optional)
    pub fn labels(mut self, value: impl Into<Option<Vec<AtUri<'a>>>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `labels` field to an Option value (optional)
    pub fn maybe_labels(mut self, value: Option<Vec<AtUri<'a>>>) -> Self {
        self._fields.3 = value;
        self
    }
}

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

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `source` field (optional)
    pub fn source(mut self, value: impl Into<Option<UriValue<'a>>>) -> Self {
        self._fields.5 = value.into();
        self
    }
    /// Set the `source` field to an Option value (optional)
    pub fn maybe_source(mut self, value: Option<UriValue<'a>>) -> Self {
        self._fields.5 = value;
        self
    }
}

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `spindle` field (optional)
    pub fn spindle(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.6 = value.into();
        self
    }
    /// Set the `spindle` field to an Option value (optional)
    pub fn maybe_spindle(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.6 = value;
        self
    }
}

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `topics` field (optional)
    pub fn topics(mut self, value: impl Into<Option<Vec<CowStr<'a>>>>) -> Self {
        self._fields.7 = value.into();
        self
    }
    /// Set the `topics` field to an Option value (optional)
    pub fn maybe_topics(mut self, value: Option<Vec<CowStr<'a>>>) -> Self {
        self._fields.7 = value;
        self
    }
}

impl<'a, S: repo_state::State> RepoBuilder<'a, S> {
    /// Set the `website` field (optional)
    pub fn website(mut self, value: impl Into<Option<UriValue<'a>>>) -> Self {
        self._fields.8 = value.into();
        self
    }
    /// Set the `website` field to an Option value (optional)
    pub fn maybe_website(mut self, value: Option<UriValue<'a>>) -> Self {
        self._fields.8 = value;
        self
    }
}

impl<'a, S> RepoBuilder<'a, S>
where
    S: repo_state::State,
    S::CreatedAt: repo_state::IsSet,
    S::Knot: repo_state::IsSet,
    S::Name: repo_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> Repo<'a> {
        Repo {
            created_at: self._fields.0.unwrap(),
            description: self._fields.1,
            knot: self._fields.2.unwrap(),
            labels: self._fields.3,
            name: self._fields.4.unwrap(),
            source: self._fields.5,
            spindle: self._fields.6,
            topics: self._fields.7,
            website: self._fields.8,
            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>,
        >,
    ) -> Repo<'a> {
        Repo {
            created_at: self._fields.0.unwrap(),
            description: self._fields.1,
            knot: self._fields.2.unwrap(),
            labels: self._fields.3,
            name: self._fields.4.unwrap(),
            source: self._fields.5,
            spindle: self._fields.6,
            topics: self._fields.7,
            website: self._fields.8,
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_sh_tangled_repo() -> 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("sh.tangled.repo"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    key: Some(CowStr::new_static("tid")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("name"), SmolStr::new_static("knot"),
                                SmolStr::new_static("createdAt")
                            ],
                        ),
                        properties: {
                            #[allow(unused_mut)]
                            let mut map = BTreeMap::new();
                            map.insert(
                                SmolStr::new_static("createdAt"),
                                LexObjectProperty::String(LexString {
                                    format: Some(LexStringFormat::Datetime),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("description"),
                                LexObjectProperty::String(LexString {
                                    min_graphemes: Some(1usize),
                                    max_graphemes: Some(140usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("knot"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("knot where the repo was created"),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("labels"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static(
                                            "List of labels that this repo subscribes to",
                                        ),
                                    ),
                                    items: LexArrayItem::String(LexString {
                                        format: Some(LexStringFormat::AtUri),
                                        ..Default::default()
                                    }),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("name"),
                                LexObjectProperty::String(LexString {
                                    description: Some(CowStr::new_static("name of the repo")),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("source"),
                                LexObjectProperty::String(LexString {
                                    description: Some(CowStr::new_static("source of the repo")),
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("spindle"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static(
                                            "CI runner to send jobs to and receive results from",
                                        ),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("topics"),
                                LexObjectProperty::Array(LexArray {
                                    description: Some(
                                        CowStr::new_static("Topics related to the repo"),
                                    ),
                                    items: LexArrayItem::String(LexString {
                                        min_length: Some(1usize),
                                        max_length: Some(50usize),
                                        ..Default::default()
                                    }),
                                    max_length: Some(50usize),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("website"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("Any URI related to the repo"),
                                    ),
                                    format: Some(LexStringFormat::Uri),
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}