// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: science.alt.dataset.resolveLabel
//
// 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_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};
use crate::science_alt::dataset::label::Label;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct ResolveLabel<'a> {
    #[serde(borrow)]
    pub handle: CowStr<'a>,
    #[serde(borrow)]
    pub name: CowStr<'a>,
    ///(max length: 50)
    #[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 ResolveLabelOutput<'a> {
    ///CID of the resolved dataset entry
    #[serde(borrow)]
    pub cid: CowStr<'a>,
    ///The label record that was resolved
    #[serde(borrow)]
    pub label: Label<'a>,
    ///AT-URI of the resolved dataset entry
    #[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 ResolveLabelError<'a> {
    /// No label found with the given name
    #[serde(rename = "LabelNotFound")]
    LabelNotFound(Option<CowStr<'a>>),
}

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

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

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

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

pub mod resolve_label_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 Handle;
        type Name;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Handle = Unset;
        type Name = Unset;
    }
    ///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 Handle = Set<members::handle>;
        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 Handle = S::Handle;
        type Name = Set<members::name>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `handle` field
        pub struct handle(());
        ///Marker type for the `name` field
        pub struct name(());
    }
}

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

impl<'a> ResolveLabel<'a> {
    /// Create a new builder for this type
    pub fn new() -> ResolveLabelBuilder<'a, resolve_label_state::Empty> {
        ResolveLabelBuilder::new()
    }
}

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

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

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

impl<'a, S: resolve_label_state::State> ResolveLabelBuilder<'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> ResolveLabelBuilder<'a, S>
where
    S: resolve_label_state::State,
    S::Handle: resolve_label_state::IsSet,
    S::Name: resolve_label_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> ResolveLabel<'a> {
        ResolveLabel {
            handle: self._fields.0.unwrap(),
            name: self._fields.1.unwrap(),
            version: self._fields.2,
        }
    }
}