// @generated by jacquard-lexicon. DO NOT EDIT.
//
// Lexicon: tools.ozone.safelink.queryRules
//
// 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};
use serde::{Serialize, Deserialize};
use crate::tools_ozone::safelink::UrlRule;

#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Default)]
#[serde(rename_all = "camelCase")]
pub struct QueryRules<'a> {
    ///Filter by action types
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub actions: Option<Vec<CowStr<'a>>>,
    ///Filter by rule creator
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub created_by: Option<Did<'a>>,
    ///Cursor for pagination
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cursor: Option<CowStr<'a>>,
    ///Maximum number of results to return  Defaults to `50`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "_default_query_rules_limit")]
    pub limit: Option<i64>,
    ///Filter by pattern type
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub pattern_type: Option<CowStr<'a>>,
    ///Filter by reason type
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub reason: Option<CowStr<'a>>,
    ///Sort direction
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub sort_direction: Option<QueryRulesSortDirection<'a>>,
    ///Filter by specific URLs or domains
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub urls: Option<Vec<CowStr<'a>>>,
}

/// Sort direction

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum QueryRulesSortDirection<'a> {
    Asc,
    Desc,
    Other(CowStr<'a>),
}

impl<'a> QueryRulesSortDirection<'a> {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
            Self::Other(s) => s.as_ref(),
        }
    }
}

impl<'a> From<&'a str> for QueryRulesSortDirection<'a> {
    fn from(s: &'a str) -> Self {
        match s {
            "asc" => Self::Asc,
            "desc" => Self::Desc,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> From<String> for QueryRulesSortDirection<'a> {
    fn from(s: String) -> Self {
        match s.as_str() {
            "asc" => Self::Asc,
            "desc" => Self::Desc,
            _ => Self::Other(CowStr::from(s)),
        }
    }
}

impl<'a> core::fmt::Display for QueryRulesSortDirection<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl<'a> AsRef<str> for QueryRulesSortDirection<'a> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<'a> serde::Serialize for QueryRulesSortDirection<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de, 'a> serde::Deserialize<'de> for QueryRulesSortDirection<'a>
where
    'de: 'a,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = <&'de str>::deserialize(deserializer)?;
        Ok(Self::from(s))
    }
}

impl<'a> Default for QueryRulesSortDirection<'a> {
    fn default() -> Self {
        Self::Other(Default::default())
    }
}

impl jacquard_common::IntoStatic for QueryRulesSortDirection<'_> {
    type Output = QueryRulesSortDirection<'static>;
    fn into_static(self) -> Self::Output {
        match self {
            QueryRulesSortDirection::Asc => QueryRulesSortDirection::Asc,
            QueryRulesSortDirection::Desc => QueryRulesSortDirection::Desc,
            QueryRulesSortDirection::Other(v) => {
                QueryRulesSortDirection::Other(v.into_static())
            }
        }
    }
}


#[lexicon]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(rename_all = "camelCase")]
pub struct QueryRulesOutput<'a> {
    ///Next cursor for pagination. Only present if there are more results.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cursor: Option<CowStr<'a>>,
    #[serde(borrow)]
    pub rules: Vec<UrlRule<'a>>,
}

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

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

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

fn _default_query_rules_limit() -> Option<i64> {
    Some(50i64)
}