// @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, BosStr, DefaultStr, FromStaticStr};
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::string::AtUri;
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, 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", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct ResolveLabel<S: BosStr = DefaultStr> {
    pub handle: S,
    pub name: S,
    /// (max length: 50)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<S>,
}


#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct ResolveLabelOutput<S: BosStr = DefaultStr> {
    ///CID of the resolved dataset entry
    pub cid: S,
    ///The label record that was resolved
    pub label: Label<S>,
    ///AT-URI of the resolved dataset entry
    pub uri: AtUri<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,
    thiserror::Error,
    miette::Diagnostic
)]

#[serde(tag = "error", content = "message")]
pub enum ResolveLabelError {
    /// No label found with the given name
    #[serde(rename = "LabelNotFound")]
    LabelNotFound(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

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::Other { error, message } => {
                write!(f, "{}", error)?;
                if let Some(msg) = message {
                    write!(f, ": {}", msg)?;
                }
                Ok(())
            }
        }
    }
}

/** Response marker for the `science.alt.dataset.resolveLabel` query.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `ResolveLabelOutput<S>` for this endpoint.*/
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<S: BosStr> = ResolveLabelOutput<S>;
    type Err = ResolveLabelError;
}

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

/** Endpoint marker for the `science.alt.dataset.resolveLabel` query.

Path: `/xrpc/science.alt.dataset.resolveLabel`. The request payload type is `ResolveLabel<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
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<S: BosStr> = ResolveLabel<S>;
    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<St: State = Empty>(PhantomData<fn() -> St>);
    impl<St: State> sealed::Sealed for SetHandle<St> {}
    impl<St: State> State for SetHandle<St> {
        type Handle = Set<members::handle>;
        type Name = St::Name;
    }
    ///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 Handle = St::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<St: resolve_label_state::State, S: BosStr = DefaultStr> {
    _state: PhantomData<fn() -> St>,
    _fields: (Option<S>, Option<S>, Option<S>),
    _type: PhantomData<fn() -> S>,
}

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

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

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

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

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

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

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

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