// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.safelink.updateRule
//
// 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::Did;
use jacquard_derive::{IntoStatic, lexicon, open_union};
use serde::{Serialize, Deserialize};
use crate::tools_ozone::safelink::ActionType;
use crate::tools_ozone::safelink::Event;
use crate::tools_ozone::safelink::PatternType;
use crate::tools_ozone::safelink::ReasonType;

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRule<'a> {
    #[serde(borrow)]
    pub action: ActionType<'a>,
    ///Optional comment about the update
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub comment: Option<CowStr<'a>>,
    ///Optional DID to credit as the creator. Only respected for admin_token authentication.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub created_by: Option<Did<'a>>,
    #[serde(borrow)]
    pub pattern: PatternType<'a>,
    #[serde(borrow)]
    pub reason: ReasonType<'a>,
    ///The URL or domain to update the rule for
    #[serde(borrow)]
    pub url: CowStr<'a>,
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRuleOutput<'a> {
    #[serde(flatten)]
    #[serde(borrow)]
    pub value: Event<'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 UpdateRuleError<'a> {
    /// No active rule found for this URL/domain
    #[serde(rename = "RuleNotFound")]
    RuleNotFound(Option<CowStr<'a>>),
}

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

/// Response type for tools.ozone.safelink.updateRule
pub struct UpdateRuleResponse;
impl jacquard_common::xrpc::XrpcResp for UpdateRuleResponse {
    const NSID: &'static str = "tools.ozone.safelink.updateRule";
    const ENCODING: &'static str = "application/json";
    type Output<'de> = UpdateRuleOutput<'de>;
    type Err<'de> = UpdateRuleError<'de>;
}

impl<'a> jacquard_common::xrpc::XrpcRequest for UpdateRule<'a> {
    const NSID: &'static str = "tools.ozone.safelink.updateRule";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Response = UpdateRuleResponse;
}

/// Endpoint type for tools.ozone.safelink.updateRule
pub struct UpdateRuleRequest;
impl jacquard_common::xrpc::XrpcEndpoint for UpdateRuleRequest {
    const PATH: &'static str = "/xrpc/tools.ozone.safelink.updateRule";
    const METHOD: jacquard_common::xrpc::XrpcMethod = jacquard_common::xrpc::XrpcMethod::Procedure(
        "application/json",
    );
    type Request<'de> = UpdateRule<'de>;
    type Response = UpdateRuleResponse;
}

pub mod update_rule_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 Pattern;
        type Reason;
        type Url;
        type Action;
    }
    /// Empty state - all required fields are unset
    pub struct Empty(());
    impl sealed::Sealed for Empty {}
    impl State for Empty {
        type Pattern = Unset;
        type Reason = Unset;
        type Url = Unset;
        type Action = Unset;
    }
    ///State transition - sets the `pattern` field to Set
    pub struct SetPattern<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetPattern<S> {}
    impl<S: State> State for SetPattern<S> {
        type Pattern = Set<members::pattern>;
        type Reason = S::Reason;
        type Url = S::Url;
        type Action = S::Action;
    }
    ///State transition - sets the `reason` field to Set
    pub struct SetReason<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetReason<S> {}
    impl<S: State> State for SetReason<S> {
        type Pattern = S::Pattern;
        type Reason = Set<members::reason>;
        type Url = S::Url;
        type Action = S::Action;
    }
    ///State transition - sets the `url` field to Set
    pub struct SetUrl<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetUrl<S> {}
    impl<S: State> State for SetUrl<S> {
        type Pattern = S::Pattern;
        type Reason = S::Reason;
        type Url = Set<members::url>;
        type Action = S::Action;
    }
    ///State transition - sets the `action` field to Set
    pub struct SetAction<S: State = Empty>(PhantomData<fn() -> S>);
    impl<S: State> sealed::Sealed for SetAction<S> {}
    impl<S: State> State for SetAction<S> {
        type Pattern = S::Pattern;
        type Reason = S::Reason;
        type Url = S::Url;
        type Action = Set<members::action>;
    }
    /// Marker types for field names
    #[allow(non_camel_case_types)]
    pub mod members {
        ///Marker type for the `pattern` field
        pub struct pattern(());
        ///Marker type for the `reason` field
        pub struct reason(());
        ///Marker type for the `url` field
        pub struct url(());
        ///Marker type for the `action` field
        pub struct action(());
    }
}

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

impl<'a> UpdateRule<'a> {
    /// Create a new builder for this type
    pub fn new() -> UpdateRuleBuilder<'a, update_rule_state::Empty> {
        UpdateRuleBuilder::new()
    }
}

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

impl<'a, S> UpdateRuleBuilder<'a, S>
where
    S: update_rule_state::State,
    S::Action: update_rule_state::IsUnset,
{
    /// Set the `action` field (required)
    pub fn action(
        mut self,
        value: impl Into<ActionType<'a>>,
    ) -> UpdateRuleBuilder<'a, update_rule_state::SetAction<S>> {
        self._fields.0 = Option::Some(value.into());
        UpdateRuleBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S: update_rule_state::State> UpdateRuleBuilder<'a, S> {
    /// Set the `comment` field (optional)
    pub fn comment(mut self, value: impl Into<Option<CowStr<'a>>>) -> Self {
        self._fields.1 = value.into();
        self
    }
    /// Set the `comment` field to an Option value (optional)
    pub fn maybe_comment(mut self, value: Option<CowStr<'a>>) -> Self {
        self._fields.1 = value;
        self
    }
}

impl<'a, S: update_rule_state::State> UpdateRuleBuilder<'a, S> {
    /// Set the `createdBy` field (optional)
    pub fn created_by(mut self, value: impl Into<Option<Did<'a>>>) -> Self {
        self._fields.2 = value.into();
        self
    }
    /// Set the `createdBy` field to an Option value (optional)
    pub fn maybe_created_by(mut self, value: Option<Did<'a>>) -> Self {
        self._fields.2 = value;
        self
    }
}

impl<'a, S> UpdateRuleBuilder<'a, S>
where
    S: update_rule_state::State,
    S::Pattern: update_rule_state::IsUnset,
{
    /// Set the `pattern` field (required)
    pub fn pattern(
        mut self,
        value: impl Into<PatternType<'a>>,
    ) -> UpdateRuleBuilder<'a, update_rule_state::SetPattern<S>> {
        self._fields.3 = Option::Some(value.into());
        UpdateRuleBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateRuleBuilder<'a, S>
where
    S: update_rule_state::State,
    S::Reason: update_rule_state::IsUnset,
{
    /// Set the `reason` field (required)
    pub fn reason(
        mut self,
        value: impl Into<ReasonType<'a>>,
    ) -> UpdateRuleBuilder<'a, update_rule_state::SetReason<S>> {
        self._fields.4 = Option::Some(value.into());
        UpdateRuleBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateRuleBuilder<'a, S>
where
    S: update_rule_state::State,
    S::Url: update_rule_state::IsUnset,
{
    /// Set the `url` field (required)
    pub fn url(
        mut self,
        value: impl Into<CowStr<'a>>,
    ) -> UpdateRuleBuilder<'a, update_rule_state::SetUrl<S>> {
        self._fields.5 = Option::Some(value.into());
        UpdateRuleBuilder {
            _state: PhantomData,
            _fields: self._fields,
            _lifetime: PhantomData,
        }
    }
}

impl<'a, S> UpdateRuleBuilder<'a, S>
where
    S: update_rule_state::State,
    S::Pattern: update_rule_state::IsSet,
    S::Reason: update_rule_state::IsSet,
    S::Url: update_rule_state::IsSet,
    S::Action: update_rule_state::IsSet,
{
    /// Build the final struct
    pub fn build(self) -> UpdateRule<'a> {
        UpdateRule {
            action: self._fields.0.unwrap(),
            comment: self._fields.1,
            created_by: self._fields.2,
            pattern: self._fields.3.unwrap(),
            reason: self._fields.4.unwrap(),
            url: self._fields.5.unwrap(),
            extra_data: Default::default(),
        }
    }
    /// Build the final struct with custom extra_data
    pub fn build_with_data(
        self,
        extra_data: BTreeMap<
            jacquard_common::deps::smol_str::SmolStr,
            jacquard_common::types::value::Data<'a>,
        >,
    ) -> UpdateRule<'a> {
        UpdateRule {
            action: self._fields.0.unwrap(),
            comment: self._fields.1,
            created_by: self._fields.2,
            pattern: self._fields.3.unwrap(),
            reason: self._fields.4.unwrap(),
            url: self._fields.5.unwrap(),
            extra_data: Some(extra_data),
        }
    }
}