// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.wisp.fs
//
// 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::blob::BlobRef;
use jacquard_common::types::collection::{Collection, RecordError};
use jacquard_common::types::string::{AtUri, Cid, Datetime};
use jacquard_common::types::uri::{RecordUri, UriError};
use jacquard_common::types::value::Data;
use jacquard_common::xrpc::XrpcResp;
use jacquard_derive::{IntoStatic, lexicon, open_union};
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;

#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Serialize, Deserialize};
use crate::place_wisp::fs;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Directory<S: BosStr = DefaultStr> {
    pub entries: Vec<fs::Entry<S>>,
    pub r#type: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Entry<S: BosStr = DefaultStr> {
    pub name: S,
    pub node: EntryNode<S>,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}


#[open_union]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(tag = "$type", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub enum EntryNode<S: BosStr = DefaultStr> {
    #[serde(rename = "place.wisp.fs#file")]
    File(Box<fs::File<S>>),
    #[serde(rename = "place.wisp.fs#directory")]
    Directory(Box<fs::Directory<S>>),
    #[serde(rename = "place.wisp.fs#subfs")]
    Subfs(Box<fs::Subfs<S>>),
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct File<S: BosStr = DefaultStr> {
    ///True if blob content is base64-encoded (used to bypass PDS content sniffing)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base64: Option<bool>,
    ///Content blob ref
    pub blob: BlobRef<S>,
    ///Content encoding (e.g., gzip for compressed files)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encoding: Option<S>,
    ///Original MIME type before compression
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<S>,
    pub r#type: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

/// Virtual filesystem manifest for a Wisp site

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
    rename_all = "camelCase",
    rename = "place.wisp.fs",
    tag = "$type",
    bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Fs<S: BosStr = DefaultStr> {
    pub created_at: Datetime,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_count: Option<i64>,
    pub root: fs::Directory<S>,
    pub site: 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 FsGetRecordOutput<S: BosStr = DefaultStr> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cid: Option<Cid<S>>,
    pub uri: AtUri<S>,
    pub value: Fs<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct Subfs<S: BosStr = DefaultStr> {
    ///If true, the subfs record's root entries are merged (flattened) into the parent directory, replacing the subfs entry. If false (default), the subfs entries are placed in a subdirectory with the subfs entry's name. Flat merging is useful for splitting large directories across multiple records while maintaining a flat structure.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flat: Option<bool>,
    ///AT-URI pointing to a place.wisp.subfs record containing this subtree.
    pub subject: AtUri<S>,
    pub r#type: S,
    #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}

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

impl<S: BosStr> LexiconSchema for Directory<S> {
    fn nsid() -> &'static str {
        "place.wisp.fs"
    }
    fn def_name() -> &'static str {
        "directory"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_wisp_fs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.entries;
            #[allow(unused_comparisons)]
            if value.len() > 500usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("entries"),
                    max: 500usize,
                    actual: value.len(),
                });
            }
        }
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for Entry<S> {
    fn nsid() -> &'static str {
        "place.wisp.fs"
    }
    fn def_name() -> &'static str {
        "entry"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_wisp_fs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.name;
            #[allow(unused_comparisons)]
            if <str>::len(value.as_ref()) > 255usize {
                return Err(ConstraintError::MaxLength {
                    path: ValidationPath::from_field("name"),
                    max: 255usize,
                    actual: <str>::len(value.as_ref()),
                });
            }
        }
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for File<S> {
    fn nsid() -> &'static str {
        "place.wisp.fs"
    }
    fn def_name() -> &'static str {
        "file"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_wisp_fs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        {
            let value = &self.blob;
            {
                let size = value.blob().size;
                if size > 1000000000usize {
                    return Err(ConstraintError::BlobTooLarge {
                        path: ValidationPath::from_field("blob"),
                        max: 1000000000usize,
                        actual: size,
                    });
                }
            }
        }
        {
            let value = &self.blob;
            {
                let mime = value.blob().mime_type.as_str();
                let accepted: &[&str] = &["*/*"];
                let matched = accepted
                    .iter()
                    .any(|pattern| {
                        if *pattern == "*/*" {
                            true
                        } else if pattern.ends_with("/*") {
                            let prefix = &pattern[..pattern.len() - 2];
                            mime.starts_with(prefix)
                                && mime.as_bytes().get(prefix.len()) == Some(&b'/')
                        } else {
                            mime == *pattern
                        }
                    });
                if !matched {
                    return Err(ConstraintError::BlobMimeTypeNotAccepted {
                        path: ValidationPath::from_field("blob"),
                        accepted: vec!["*/*".to_string()],
                        actual: mime.to_string(),
                    });
                }
            }
        }
        Ok(())
    }
}

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

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

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

impl<S: BosStr> Collection for Fs<S> {
    const NSID: &'static str = "place.wisp.fs";
    type Record = FsRecord;
}

impl Collection for FsRecord {
    const NSID: &'static str = "place.wisp.fs";
    type Record = FsRecord;
}

impl<S: BosStr> LexiconSchema for Fs<S> {
    fn nsid() -> &'static str {
        "place.wisp.fs"
    }
    fn def_name() -> &'static str {
        "main"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_wisp_fs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        if let Some(ref value) = self.file_count {
            if *value > 1000i64 {
                return Err(ConstraintError::Maximum {
                    path: ValidationPath::from_field("file_count"),
                    max: 1000i64,
                    actual: *value,
                });
            }
        }
        if let Some(ref value) = self.file_count {
            if *value < 0i64 {
                return Err(ConstraintError::Minimum {
                    path: ValidationPath::from_field("file_count"),
                    min: 0i64,
                    actual: *value,
                });
            }
        }
        Ok(())
    }
}

impl<S: BosStr> LexiconSchema for Subfs<S> {
    fn nsid() -> &'static str {
        "place.wisp.fs"
    }
    fn def_name() -> &'static str {
        "subfs"
    }
    fn lexicon_doc() -> LexiconDoc<'static> {
        lexicon_doc_place_wisp_fs()
    }
    fn validate(&self) -> Result<(), ConstraintError> {
        Ok(())
    }
}

pub mod directory_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 Entries;
        type Type;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Entries = Unset;
        type Type = Unset;
    }
    ///State transition - sets the `entries` field to Set
    pub struct SetEntries<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetEntries<St> {}
    impl<St: State> State for SetEntries<St> {
        type Entries = Set<members::entries>;
        type Type = St::Type;
    }
    ///State transition - sets the `type` field to Set
    pub struct SetType<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetType<St> {}
    impl<St: State> State for SetType<St> {
        type Entries = St::Entries;
        type Type = Set<members::r#type>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `entries` field
        pub struct entries(());
        ///Marker type for the `type` field
        pub struct r#type(());
    }
}

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

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

impl<S: BosStr> Directory<S> {
    /// Create a new builder for this type
    pub fn builder() -> DirectoryBuilder<directory_state::Empty, S> {
        DirectoryBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> DirectoryBuilder<St, S>
where
    St: directory_state::State,
    St::Entries: directory_state::IsUnset,
{
    /// Set the `entries` field (required)
    pub fn entries(
        mut self,
        value: impl Into<Vec<fs::Entry<S>>>,
    ) -> DirectoryBuilder<directory_state::SetEntries<St>, S> {
        self._fields.0 = Option::Some(value.into());
        DirectoryBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> DirectoryBuilder<St, S>
where
    St: directory_state::State,
    St::Type: directory_state::IsUnset,
{
    /// Set the `type` field (required)
    pub fn r#type(
        mut self,
        value: impl Into<S>,
    ) -> DirectoryBuilder<directory_state::SetType<St>, S> {
        self._fields.1 = Option::Some(value.into());
        DirectoryBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> DirectoryBuilder<St, S>
where
    St: directory_state::State,
    St::Entries: directory_state::IsSet,
    St::Type: directory_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Directory<S> {
        Directory {
            entries: self._fields.0.unwrap(),
            r#type: self._fields.1.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>>,
    ) -> Directory<S> {
        Directory {
            entries: self._fields.0.unwrap(),
            r#type: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

fn lexicon_doc_place_wisp_fs() -> 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("place.wisp.fs"),
        defs: {
            let mut map = BTreeMap::new();
            map.insert(
                SmolStr::new_static("directory"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![SmolStr::new_static("type"), SmolStr::new_static("entries")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("entries"),
                            LexObjectProperty::Array(LexArray {
                                items: LexArrayItem::Ref(LexRef {
                                    r#ref: CowStr::new_static("#entry"),
                                    ..Default::default()
                                }),
                                max_length: Some(500usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("type"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("entry"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![SmolStr::new_static("name"), SmolStr::new_static("node")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("name"),
                            LexObjectProperty::String(LexString {
                                max_length: Some(255usize),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("node"),
                            LexObjectProperty::Union(LexRefUnion {
                                refs: vec![
                                    CowStr::new_static("#file"),
                                    CowStr::new_static("#directory"),
                                    CowStr::new_static("#subfs")
                                ],
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("file"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![SmolStr::new_static("type"), SmolStr::new_static("blob")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("base64"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("blob"),
                            LexObjectProperty::Blob(LexBlob { ..Default::default() }),
                        );
                        map.insert(
                            SmolStr::new_static("encoding"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "Content encoding (e.g., gzip for compressed files)",
                                    ),
                                ),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("mimeType"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static("Original MIME type before compression"),
                                ),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("type"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("main"),
                LexUserType::Record(LexRecord {
                    description: Some(
                        CowStr::new_static("Virtual filesystem manifest for a Wisp site"),
                    ),
                    record: LexRecordRecord::Object(LexObject {
                        required: Some(
                            vec![
                                SmolStr::new_static("site"), SmolStr::new_static("root"),
                                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("fileCount"),
                                LexObjectProperty::Integer(LexInteger {
                                    minimum: Some(0i64),
                                    maximum: Some(1000i64),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("root"),
                                LexObjectProperty::Ref(LexRef {
                                    r#ref: CowStr::new_static("#directory"),
                                    ..Default::default()
                                }),
                            );
                            map.insert(
                                SmolStr::new_static("site"),
                                LexObjectProperty::String(LexString {
                                    ..Default::default()
                                }),
                            );
                            map
                        },
                        ..Default::default()
                    }),
                    ..Default::default()
                }),
            );
            map.insert(
                SmolStr::new_static("subfs"),
                LexUserType::Object(LexObject {
                    required: Some(
                        vec![SmolStr::new_static("type"), SmolStr::new_static("subject")],
                    ),
                    properties: {
                        #[allow(unused_mut)]
                        let mut map = BTreeMap::new();
                        map.insert(
                            SmolStr::new_static("flat"),
                            LexObjectProperty::Boolean(LexBoolean {
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("subject"),
                            LexObjectProperty::String(LexString {
                                description: Some(
                                    CowStr::new_static(
                                        "AT-URI pointing to a place.wisp.subfs record containing this subtree.",
                                    ),
                                ),
                                format: Some(LexStringFormat::AtUri),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            SmolStr::new_static("type"),
                            LexObjectProperty::String(LexString { ..Default::default() }),
                        );
                        map
                    },
                    ..Default::default()
                }),
            );
            map
        },
        ..Default::default()
    }
}

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

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

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

impl<S: BosStr> Entry<S> {
    /// Create a new builder for this type
    pub fn builder() -> EntryBuilder<entry_state::Empty, S> {
        EntryBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> EntryBuilder<St, S>
where
    St: entry_state::State,
    St::Name: entry_state::IsUnset,
{
    /// Set the `name` field (required)
    pub fn name(
        mut self,
        value: impl Into<S>,
    ) -> EntryBuilder<entry_state::SetName<St>, S> {
        self._fields.0 = Option::Some(value.into());
        EntryBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> EntryBuilder<St, S>
where
    St: entry_state::State,
    St::Node: entry_state::IsUnset,
{
    /// Set the `node` field (required)
    pub fn node(
        mut self,
        value: impl Into<EntryNode<S>>,
    ) -> EntryBuilder<entry_state::SetNode<St>, S> {
        self._fields.1 = Option::Some(value.into());
        EntryBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> EntryBuilder<St, S>
where
    St: entry_state::State,
    St::Name: entry_state::IsSet,
    St::Node: entry_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Entry<S> {
        Entry {
            name: self._fields.0.unwrap(),
            node: self._fields.1.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>>) -> Entry<S> {
        Entry {
            name: self._fields.0.unwrap(),
            node: self._fields.1.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

pub mod file_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 Blob;
        type Type;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Blob = Unset;
        type Type = Unset;
    }
    ///State transition - sets the `blob` field to Set
    pub struct SetBlob<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetBlob<St> {}
    impl<St: State> State for SetBlob<St> {
        type Blob = Set<members::blob>;
        type Type = St::Type;
    }
    ///State transition - sets the `type` field to Set
    pub struct SetType<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetType<St> {}
    impl<St: State> State for SetType<St> {
        type Blob = St::Blob;
        type Type = Set<members::r#type>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `blob` field
        pub struct blob(());
        ///Marker type for the `type` field
        pub struct r#type(());
    }
}

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

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

impl<S: BosStr> File<S> {
    /// Create a new builder for this type
    pub fn builder() -> FileBuilder<file_state::Empty, S> {
        FileBuilder::builder()
    }
}

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

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

impl<St: file_state::State, S: BosStr> FileBuilder<St, S> {
    /// Set the `base64` field (optional)
    pub fn base64(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `base64` field to an Option value (optional)
    pub fn maybe_base64(mut self, value: Option<bool>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St, S: BosStr> FileBuilder<St, S>
where
    St: file_state::State,
    St::Blob: file_state::IsUnset,
{
    /// Set the `blob` field (required)
    pub fn blob(
        mut self,
        value: impl Into<BlobRef<S>>,
    ) -> FileBuilder<file_state::SetBlob<St>, S> {
        self._fields.1 = Option::Some(value.into());
        FileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

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

impl<St: file_state::State, S: BosStr> FileBuilder<St, S> {
    /// Set the `mimeType` field (optional)
    pub fn mime_type(mut self, value: impl Into<Option<S>>) -> Self {
        self._fields.3 = value.into();
        self
    }
    /// Set the `mimeType` field to an Option value (optional)
    pub fn maybe_mime_type(mut self, value: Option<S>) -> Self {
        self._fields.3 = value;
        self
    }
}

impl<St, S: BosStr> FileBuilder<St, S>
where
    St: file_state::State,
    St::Type: file_state::IsUnset,
{
    /// Set the `type` field (required)
    pub fn r#type(
        mut self,
        value: impl Into<S>,
    ) -> FileBuilder<file_state::SetType<St>, S> {
        self._fields.4 = Option::Some(value.into());
        FileBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> FileBuilder<St, S>
where
    St: file_state::State,
    St::Blob: file_state::IsSet,
    St::Type: file_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> File<S> {
        File {
            base64: self._fields.0,
            blob: self._fields.1.unwrap(),
            encoding: self._fields.2,
            mime_type: self._fields.3,
            r#type: 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>>) -> File<S> {
        File {
            base64: self._fields.0,
            blob: self._fields.1.unwrap(),
            encoding: self._fields.2,
            mime_type: self._fields.3,
            r#type: self._fields.4.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

pub mod fs_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 Root;
        type Site;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type CreatedAt = Unset;
        type Root = Unset;
        type Site = 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 Root = St::Root;
        type Site = St::Site;
    }
    ///State transition - sets the `root` field to Set
    pub struct SetRoot<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetRoot<St> {}
    impl<St: State> State for SetRoot<St> {
        type CreatedAt = St::CreatedAt;
        type Root = Set<members::root>;
        type Site = St::Site;
    }
    ///State transition - sets the `site` field to Set
    pub struct SetSite<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSite<St> {}
    impl<St: State> State for SetSite<St> {
        type CreatedAt = St::CreatedAt;
        type Root = St::Root;
        type Site = Set<members::site>;
    }
    /// 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 `root` field
        pub struct root(());
        ///Marker type for the `site` field
        pub struct site(());
    }
}

/// Builder for constructing an instance of this type.
pub struct FsBuilder<St: fs_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<Datetime>, Option<i64>, Option<fs::Directory<S>>, Option<S>),
    _type: PhantomData<fn() -> S>,
}

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

impl<S: BosStr> Fs<S> {
    /// Create a new builder for this type
    pub fn builder() -> FsBuilder<fs_state::Empty, S> {
        FsBuilder::builder()
    }
}

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

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

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

impl<St: fs_state::State, S: BosStr> FsBuilder<St, S> {
    /// Set the `fileCount` field (optional)
    pub fn file_count(mut self, value: impl Into<Option<i64>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `fileCount` field to an Option value (optional)
    pub fn maybe_file_count(mut self, value: Option<i64>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> FsBuilder<St, S>
where
    St: fs_state::State,
    St::Root: fs_state::IsUnset,
{
    /// Set the `root` field (required)
    pub fn root(
        mut self,
        value: impl Into<fs::Directory<S>>,
    ) -> FsBuilder<fs_state::SetRoot<St>, S> {
        self._fields.2 = Option::Some(value.into());
        FsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> FsBuilder<St, S>
where
    St: fs_state::State,
    St::Site: fs_state::IsUnset,
{
    /// Set the `site` field (required)
    pub fn site(mut self, value: impl Into<S>) -> FsBuilder<fs_state::SetSite<St>, S> {
        self._fields.3 = Option::Some(value.into());
        FsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> FsBuilder<St, S>
where
    St: fs_state::State,
    St::CreatedAt: fs_state::IsSet,
    St::Root: fs_state::IsSet,
    St::Site: fs_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Fs<S> {
        Fs {
            created_at: self._fields.0.unwrap(),
            file_count: self._fields.1,
            root: self._fields.2.unwrap(),
            site: self._fields.3.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>>) -> Fs<S> {
        Fs {
            created_at: self._fields.0.unwrap(),
            file_count: self._fields.1,
            root: self._fields.2.unwrap(),
            site: self._fields.3.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}

pub mod subfs_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 Subject;
        type Type;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Subject = Unset;
        type Type = Unset;
    }
    ///State transition - sets the `subject` field to Set
    pub struct SetSubject<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetSubject<St> {}
    impl<St: State> State for SetSubject<St> {
        type Subject = Set<members::subject>;
        type Type = St::Type;
    }
    ///State transition - sets the `type` field to Set
    pub struct SetType<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetType<St> {}
    impl<St: State> State for SetType<St> {
        type Subject = St::Subject;
        type Type = Set<members::r#type>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `subject` field
        pub struct subject(());
        ///Marker type for the `type` field
        pub struct r#type(());
    }
}

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

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

impl<S: BosStr> Subfs<S> {
    /// Create a new builder for this type
    pub fn builder() -> SubfsBuilder<subfs_state::Empty, S> {
        SubfsBuilder::builder()
    }
}

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

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

impl<St: subfs_state::State, S: BosStr> SubfsBuilder<St, S> {
    /// Set the `flat` field (optional)
    pub fn flat(mut self, value: impl Into<Option<bool>>) -> Self {
        self._fields.0 = value.into();
        self
    }
    /// Set the `flat` field to an Option value (optional)
    pub fn maybe_flat(mut self, value: Option<bool>) -> Self {
        self._fields.0 = value;
        self
    }
}

impl<St, S: BosStr> SubfsBuilder<St, S>
where
    St: subfs_state::State,
    St::Subject: subfs_state::IsUnset,
{
    /// Set the `subject` field (required)
    pub fn subject(
        mut self,
        value: impl Into<AtUri<S>>,
    ) -> SubfsBuilder<subfs_state::SetSubject<St>, S> {
        self._fields.1 = Option::Some(value.into());
        SubfsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SubfsBuilder<St, S>
where
    St: subfs_state::State,
    St::Type: subfs_state::IsUnset,
{
    /// Set the `type` field (required)
    pub fn r#type(
        mut self,
        value: impl Into<S>,
    ) -> SubfsBuilder<subfs_state::SetType<St>, S> {
        self._fields.2 = Option::Some(value.into());
        SubfsBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St, S: BosStr> SubfsBuilder<St, S>
where
    St: subfs_state::State,
    St::Subject: subfs_state::IsSet,
    St::Type: subfs_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> Subfs<S> {
        Subfs {
            flat: self._fields.0,
            subject: self._fields.1.unwrap(),
            r#type: 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>>) -> Subfs<S> {
        Subfs {
            flat: self._fields.0,
            subject: self._fields.1.unwrap(),
            r#type: self._fields.2.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}