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

//! Generated bindings for the `sh.tangled.repo` Lexicon namespace/module.
pub mod add_collaborator;
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 describe_repo;
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_artifacts;
pub mod list_artifacts_by;
pub mod list_collaborators;
pub mod list_collaborators_by;
pub mod list_issues;
pub mod list_issues_by;
pub mod list_pulls;
pub mod list_pulls_by;
pub mod list_repos;
pub mod list_secrets;
pub mod log;
pub mod merge;
pub mod merge_check;
pub mod pull;
pub mod remove_collaborator;
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, 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::{Did, AtUri, Cid, Datetime, 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};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "sh.tangled.repo",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Repo<S: BosStr = DefaultStr> {
    pub created_at: Datetime,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<S>,
    ///knot where the repo was created
    pub knot: S,
    ///List of labels that this repo subscribes to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub labels: Option<Vec<AtUri<S>>>,
    ///Cosmetic name of the repo.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<S>,
    ///DID of the repo itself, if assigned
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_did: Option<Did<S>>,
    ///source of the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<UriValue<S>>,
    ///CI runner to send jobs to and receive results from
    #[serde(skip_serializing_if = "Option::is_none")]
    pub spindle: Option<S>,
    ///Topics related to the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    pub topics: Option<Vec<S>>,
    ///Any URI related to the repo
    #[serde(skip_serializing_if = "Option::is_none")]
    pub website: Option<UriValue<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 RepoGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Repo<S>,
}

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

/// 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<S: BosStr> = RepoGetRecordOutput<S>;
    type Err = RecordError;
}

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

impl<S: BosStr> Collection for Repo<S> {
    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<S: BosStr> LexiconSchema for Repo<S> {
    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;
    }
    /// 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;
    }
    ///State transition - sets the `created_at` field to Set
    pub struct SetCreatedAt<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetCreatedAt<St> {}
    impl<St: State> State for SetCreatedAt<St> {
        type CreatedAt = Set<members::created_at>;
        type Knot = St::Knot;
    }
    ///State transition - sets the `knot` field to Set
    pub struct SetKnot<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetKnot<St> {}
    impl<St: State> State for SetKnot<St> {
        type CreatedAt = St::CreatedAt;
        type Knot = Set<members::knot>;
    }
    /// 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(());
    }
}

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

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

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

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

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

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

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

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

impl<St: repo_state::State, S: BosStr> RepoBuilder<St, S> {
    /// Set the `labels` field (optional)
    pub fn labels(mut self, value: impl Into<Option<Vec<AtUri<S>>>>) -> 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<S>>>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St: repo_state::State, S: BosStr> RepoBuilder<St, S> {
    /// Set the `name` field (optional)
    pub fn name(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.4 = value.into();
        self
    }
    /// Set the `name` field to an Option value (optional)
    pub fn maybe_name(mut self, value: Option<S>) -> Self {
        self._fields.4 = value;
        self
    }
}

impl<St: repo_state::State, S: BosStr> RepoBuilder<St, S> {
    /// Set the `repoDid` field (optional)
    pub fn repo_did(mut self, value: impl Into<Option<Did<S>>>) -> Self {
        self._fields.5 = value.into();
        self
    }
    /// Set the `repoDid` field to an Option value (optional)
    pub fn maybe_repo_did(mut self, value: Option<Did<S>>) -> Self {
        self._fields.5 = value;
        self
    }
}

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

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

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

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

impl<St, S: BosStr> RepoBuilder<St, S>
where
    St: repo_state::State,
    St::CreatedAt: repo_state::IsSet,
    St::Knot: repo_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Repo<S> {
        Repo {
            created_at: self._fields.0.unwrap(),
            description: self._fields.1,
            knot: self._fields.2.unwrap(),
            labels: self._fields.3,
            name: self._fields.4,
            repo_did: self._fields.5,
            source: self._fields.6,
            spindle: self._fields.7,
            topics: self._fields.8,
            website: self._fields.9,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Repo<S> {
        Repo {
            created_at: self._fields.0.unwrap(),
            description: self._fields.1,
            knot: self._fields.2.unwrap(),
            labels: self._fields.3,
            name: self._fields.4,
            repo_did: self._fields.5,
            source: self._fields.6,
            spindle: self._fields.7,
            topics: self._fields.8,
            website: self._fields.9,
            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("any")),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                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("Cosmetic name of the repo."),
                                    ),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("repoDid"),
                                LexObjectProperty::String(LexString {
                                    description: Some(
                                        CowStr::new_static("DID of the repo itself, if assigned"),
                                    ),
                                    format: Some(LexStringFormat::Did),
                                    ..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()
    }
}