use serde::{Deserialize, Serialize};
use super::extensible::TmfType;
use super::macros::tmf_struct;
use super::reference::Ref;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Party;
impl TmfType for Party {
const TYPE_NAME: &'static str = "Party";
const REF_TYPE_NAME: &'static str = "PartyRef";
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PartyRole;
impl TmfType for PartyRole {
const TYPE_NAME: &'static str = "PartyRole";
const REF_TYPE_NAME: &'static str = "PartyRoleRef";
}
#[derive(Debug, Clone, PartialEq)]
pub enum PartyOrPartyRole {
Party(Ref<Party>),
Role(Ref<PartyRole>),
}
impl Serialize for PartyOrPartyRole {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::Party(r) => r.serialize(serializer),
Self::Role(r) => r.serialize(serializer),
}
}
}
impl<'de> Deserialize<'de> for PartyOrPartyRole {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let parsed = Ref::<Party>::deserialize(deserializer)?;
if parsed.type_name() == PartyRole::REF_TYPE_NAME {
Ok(Self::Role(parsed.retarget()))
} else {
Ok(Self::Party(parsed))
}
}
}
impl From<Ref<Party>> for PartyOrPartyRole {
fn from(reference: Ref<Party>) -> Self {
Self::Party(reference)
}
}
impl From<Ref<PartyRole>> for PartyOrPartyRole {
fn from(reference: Ref<PartyRole>) -> Self {
Self::Role(reference)
}
}
impl PartyOrPartyRole {
#[must_use]
pub fn id(&self) -> &str {
match self {
Self::Party(r) => &r.id,
Self::Role(r) => &r.id,
}
}
#[must_use]
pub fn type_name(&self) -> &str {
match self {
Self::Party(r) => r.type_name(),
Self::Role(r) => r.type_name(),
}
}
#[must_use]
pub fn referred_type(&self) -> Option<&str> {
match self {
Self::Party(r) => r.referred_type.as_deref(),
Self::Role(r) => r.referred_type.as_deref(),
}
}
}
tmf_struct! {
@name = "RelatedPartyRefOrPartyRoleRef";
pub struct RelatedParty {
role: String,
party_or_party_role: PartyOrPartyRole,
}
}
impl RelatedParty {
pub fn new(role: impl Into<String>, target: impl Into<PartyOrPartyRole>) -> Self {
Self::builder()
.role(role)
.party_or_party_role(target.into())
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn related_party_round_trips() {
let json = r#"{"role":"customer","partyOrPartyRole":{"id":"42","@referredType":"Individual","@type":"PartyRef"},"@type":"RelatedPartyRefOrPartyRoleRef"}"#;
let rp: RelatedParty = serde_json::from_str(json).unwrap();
assert_eq!(rp.role.as_deref(), Some("customer"));
let target = rp.party_or_party_role.as_ref().unwrap();
assert_eq!(target.id(), "42");
assert_eq!(target.referred_type(), Some("Individual"));
assert!(matches!(target, PartyOrPartyRole::Party(_)));
assert_eq!(serde_json::to_string(&rp).unwrap(), json);
}
#[test]
fn a_related_party_is_short_to_write_and_keeps_its_discriminator() {
let buyer = RelatedParty::new("customer", Ref::<Party>::new("4104").with_name("Ada"));
assert_eq!(
serde_json::to_value(&buyer).unwrap(),
serde_json::json!({
"role": "customer",
"partyOrPartyRole": {
"id": "4104",
"name": "Ada",
"@referredType": "Party",
"@type": "PartyRef",
},
"@type": "RelatedPartyRefOrPartyRoleRef",
})
);
let agent = RelatedParty::new("salesAgent", Ref::<PartyRole>::new("77"));
assert_eq!(
serde_json::to_value(&agent).unwrap()["partyOrPartyRole"]["@type"],
"PartyRoleRef"
);
}
#[test]
fn a_role_reference_dispatches_on_its_discriminator() {
let json = r#"{"id":"7","@type":"PartyRoleRef"}"#;
let target: PartyOrPartyRole = serde_json::from_str(json).unwrap();
assert!(matches!(target, PartyOrPartyRole::Role(_)));
assert_eq!(serde_json::to_string(&target).unwrap(), json);
}
}