use crate::wire::OperationCode;
use serde::{Deserialize, Serialize, Serializer};
use super::Operation;
#[derive(Clone, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ListOpsAndDepsRequest {
pub password: String,
}
impl std::fmt::Debug for ListOpsAndDepsRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ListOpsAndDepsRequest")
.field("password", &"[redacted]")
.finish()
}
}
impl Operation for ListOpsAndDepsRequest {
const CODE: OperationCode = OperationCode::ListOpsAndDeps;
const USES_PASSWORD_KEY: bool = true;
type Response = ListOpsAndDepsResponse;
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct ListOpsAndDepsResponse {
#[serde(rename = "c", default)]
pub operators: Vec<OperatorInfo>,
#[serde(rename = "d", default)]
pub departments: Vec<DepartmentInfo>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct OperatorInfo {
pub id: u32,
#[serde(default)]
pub name: String,
#[serde(default)]
pub deps: Vec<u32>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct DepartmentInfo {
pub id: u32,
#[serde(default)]
pub name: String,
#[serde(rename = "type", default = "default_unknown_taxation")]
#[cfg_attr(feature = "schema", schemars(with = "u32"))]
pub kind: TaxationKind,
}
const fn default_unknown_taxation() -> TaxationKind {
TaxationKind::Unknown(0)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TaxationKind {
VatTaxable,
NotVatTaxable,
TurnoverTax,
ProductionLicensee,
Patented,
FamilyBusiness,
MicroBusiness,
Unknown(u32),
}
impl<'de> Deserialize<'de> for TaxationKind {
fn deserialize<D: serde::Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
let code = u32::deserialize(de)?;
Ok(match code {
1 => Self::VatTaxable,
2 => Self::NotVatTaxable,
3 => Self::TurnoverTax,
4 => Self::ProductionLicensee,
5 => Self::Patented,
6 => Self::FamilyBusiness,
7 => Self::MicroBusiness,
other => Self::Unknown(other),
})
}
}
impl Serialize for TaxationKind {
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
let code = match *self {
Self::VatTaxable => 1,
Self::NotVatTaxable => 2,
Self::TurnoverTax => 3,
Self::ProductionLicensee => 4,
Self::Patented => 5,
Self::FamilyBusiness => 6,
Self::MicroBusiness => 7,
Self::Unknown(c) => c,
};
ser.serialize_u32(code)
}
}