// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: place.stream.multistream.putTarget
//
// 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::{RecordKey, Rkey};
use jacquard_common::types::value::Data;
use jacquard_derive::{IntoStatic, open_union};
use serde::{Serialize, Deserialize};
use crate::place_stream::multistream::TargetView;
use crate::place_stream::multistream::target::Target;

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase", bound(deserialize = "S: Deserialize<'de> + BosStr"))]
pub struct PutTarget<S: BosStr = DefaultStr> {
    pub multistream_target: Target<S>,
    ///The Record Key.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rkey: Option<RecordKey<Rkey<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 PutTargetOutput<S: BosStr = DefaultStr> {
    #[serde(flatten)]
    pub value: TargetView<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 PutTargetError {
    /// The provided target URL is invalid or unreachable.
    #[serde(rename = "InvalidTargetUrl")]
    InvalidTargetUrl(Option<SmolStr>),
    /// Catch-all for unknown error codes.
    #[serde(untagged)]
    Other { error: SmolStr, message: Option<SmolStr> },
}

impl core::fmt::Display for PutTargetError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidTargetUrl(msg) => {
                write!(f, "InvalidTargetUrl")?;
                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 `place.stream.multistream.putTarget` procedure.

Implements `jacquard_common::xrpc::XrpcResp`; successful bodies decode as `Self::Output<S>`, which is `PutTargetOutput<S>` for this endpoint.*/
pub struct PutTargetResponse;
impl jacquard_common::xrpc::XrpcResp for PutTargetResponse {
    const NSID: &'static str = "place.stream.multistream.putTarget";
    const ENCODING: &'static str = "application/json";
    type Output<S: BosStr> = PutTargetOutput<S>;
    type Err = PutTargetError;
}

impl<S: BosStr> jacquard_common::xrpc::XrpcRequest for PutTarget<S> {
    const NSID: &'static str = "place.stream.multistream.putTarget";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = PutTargetResponse;
}

/** Endpoint marker for the `place.stream.multistream.putTarget` procedure.

Path: `/xrpc/place.stream.multistream.putTarget`. The request payload type is `PutTarget<S>`; send that request with `jacquard::Client` or use this marker through lower-level `XrpcEndpoint` APIs.*/
pub struct PutTargetRequest;
impl jacquard_common::xrpc::XrpcEndpoint for PutTargetRequest {
    const PATH: &'static str = "/xrpc/place.stream.multistream.putTarget";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<S: BosStr> = PutTarget<S>;
    type Response = PutTargetResponse;
}

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

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

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

impl<S: BosStr> PutTarget<S> {
    /// Create a new builder for this type
    pub fn builder() -> PutTargetBuilder<put_target_state::Empty, S> {
        PutTargetBuilder::builder()
    }
}

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

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

impl<St, S: BosStr> PutTargetBuilder<St, S>
where
    St: put_target_state::State,
    St::MultistreamTarget: put_target_state::IsUnset,
{
    /// Set the `multistreamTarget` field (required)
    pub fn multistream_target(
        mut self,
        value: impl Into<Target<S>>,
    ) -> PutTargetBuilder<put_target_state::SetMultistreamTarget<St>, S> {
        self._fields.0 = Option::Some(value.into());
        PutTargetBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _type: PhantomData,
        }
    }
}

impl<St: put_target_state::State, S: BosStr> PutTargetBuilder<St, S> {
    /// Set the `rkey` field (optional)
    pub fn rkey(mut self, value: impl Into<Option<RecordKey<Rkey<S>>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `rkey` field to an Option value (optional)
    pub fn maybe_rkey(mut self, value: Option<RecordKey<Rkey<S>>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<St, S: BosStr> PutTargetBuilder<St, S>
where
    St: put_target_state::State,
    St::MultistreamTarget: put_target_state::IsSet,
{
    /// Build the final struct.
    pub fn build(self) -> PutTarget<S> {
        PutTarget {
            multistream_target: self._fields.0.unwrap(),
            rkey: self._fields.1,
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data.
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<SmolStr, Data<S>>,
    ) -> PutTarget<S> {
        PutTarget {
            multistream_target: self._fields.0.unwrap(),
            rkey: self._fields.1,
            extra_data: Some(extra_data),
        }
    }
}