// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: science.alt.dataset.resolveSchema
//
// 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;
use jacquard_common::types::string::AtUri;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct ResolveSchema<'a> {
    #[serde(borrow)]
    pub handle: CowStr<'a>,
    #[serde(borrow)]
    pub schema_id: CowStr<'a>,
    ///(max length: 20)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub version: Option<CowStr<'a>>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct ResolveSchemaOutput<'a> {
    ///CID of the resolved schema record
    #[serde(borrow)]
    pub cid: CowStr<'a>,
    ///The full schema record
    #[serde(borrow)]
    pub record: Data<'a>,
    ///AT-URI of the resolved schema record
    #[serde(borrow)]
    pub uri: AtUri<'a>,
}


#[open_union]
#[derive(
    Serialize,
    Deserialize,
    Debug,
    Clone,
    PartialEq,
    Eq,
    thiserror::Error,
    miette::Diagnostic,
    IntoStatic
)]

#[serde(tag = "error", content = "message")]
#[serde(bound(deserialize = "'de: 'a"))]
pub enum ResolveSchemaError<'a> {
    /// No schema found with the given NSID
    #[serde(rename = "SchemaNotFound")]
    SchemaNotFound(Option<CowStr<'a>>),
}

impl core::fmt::Display for ResolveSchemaError<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::SchemaNotFound(msg) => {
                write!(f, "SchemaNotFound")?;
                if let Some(msg) = msg {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
            Self::Unknown(err) => write!(f, "Unknown error: {:?}", err),
        }
    }
}

/// Response type for science.alt.dataset.resolveSchema
pub struct ResolveSchemaResponse;
impl jacquard_common::xrpc::XrpcResp for ResolveSchemaResponse {
    const NSID: &'static str = "science.alt.dataset.resolveSchema";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = ResolveSchemaOutput<'de>;
    type Err<'de> = ResolveSchemaError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for ResolveSchema<'a> {
    const NSID: &'static str = "science.alt.dataset.resolveSchema";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Response = ResolveSchemaResponse;
}

/// Endpoint type for science.alt.dataset.resolveSchema
pub struct ResolveSchemaRequest;
impl jacquard_common::xrpc::XrpcEndpoint for ResolveSchemaRequest {
    const PATH: &'static str = "/xrpc/science.alt.dataset.resolveSchema";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Query;
    type Request<'de> = ResolveSchema<'de>;
    type Response = ResolveSchemaResponse;
}

pub mod resolve_schema_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 SchemaId;
        type Handle;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type SchemaId = Unset;
        type Handle = Unset;
    }
    ///State transition - sets the `schema_id` field to Set
    pub struct SetSchemaId<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetSchemaId<S> {}
    impl<S: State> State for SetSchemaId<S> {
        type SchemaId = Set<members::schema_id>;
        type Handle = S::Handle;
    }
    ///State transition - sets the `handle` field to Set
    pub struct SetHandle<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetHandle<S> {}
    impl<S: State> State for SetHandle<S> {
        type SchemaId = S::SchemaId;
        type Handle = Set<members::handle>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `schema_id` field
        pub struct schema_id(());
        ///Marker type for the `handle` field
        pub struct handle(());
    }
}

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

impl<'a> ResolveSchema<'a> {
    /// Create a new builder for this type
    pub fn new() -> ResolveSchemaBuilder<'a, resolve_schema_state::Empty> {
        ResolveSchemaBuilder::new()
    }
}

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

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

impl<'a, S> ResolveSchemaBuilder<'a, S>
where
    S: resolve_schema_state::State,
    S::SchemaId: resolve_schema_state::IsUnset,
{
    /// Set the `schemaId` field (required)
    pub fn schema_id(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> ResolveSchemaBuilder<'a, resolve_schema_state::SetSchemaId<S>> {
        self._fields.1 = Option::Some(value.into());
        ResolveSchemaBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: resolve_schema_state::State> ResolveSchemaBuilder<'a, S> {
    /// Set the `version` field (optional)
    pub fn version(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `version` field to an Option value (optional)
    pub fn maybe_version(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S> ResolveSchemaBuilder<'a, S>
where
    S: resolve_schema_state::State,
    S::SchemaId: resolve_schema_state::IsSet,
    S::Handle: resolve_schema_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> ResolveSchema<'a> {
        ResolveSchema {
            handle: self._fields.0.unwrap(),
            schema_id: self._fields.1.unwrap(),
            version: self._fields.2,
        }
    }
}