use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct, tmf_value};
use crate::core::{
Account, Agreement, AgreementSpecification, AssociationSpecification, Attachment,
Characteristic, CharacteristicSpecification, Constraint, CreditProfile, Party, PaymentMethod,
PermissionSpecificationSet, Ref, RelatedParty, TimePeriod, Timestamp,
};
use super::ContactMedium;
tmf_struct! {
@name = "PartyRole", @ref = "PartyRoleRef";
pub struct PartyRole {
id: String,
href: String,
name: String,
description: String,
role: String,
status: String,
status_reason: String,
engaged_party: Ref<Party>,
party_role_specification: Ref<PartyRoleSpecification>,
account: Vec<Ref<Account>>,
agreement: Vec<Ref<Agreement>>,
payment_method: Vec<Ref<PaymentMethod>>,
contact_medium: Vec<ContactMedium>,
credit_profile: Vec<CreditProfile>,
characteristic: Vec<Characteristic>,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_entity!(PartyRole);
tmf_struct! {
@name = "PartyRole";
pub struct PartyRoleCreate {
@required {
engaged_party: Ref<Party>,
name: String,
}
id: String,
description: String,
role: String,
status: String,
status_reason: String,
party_role_specification: Ref<PartyRoleSpecification>,
account: Vec<Ref<Account>>,
agreement: Vec<Ref<Agreement>>,
payment_method: Vec<Ref<PaymentMethod>>,
contact_medium: Vec<ContactMedium>,
credit_profile: Vec<CreditProfile>,
characteristic: Vec<Characteristic>,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "PartyRole";
pub struct PartyRoleUpdate {
name: String,
description: String,
role: String,
status: String,
status_reason: String,
engaged_party: Ref<Party>,
party_role_specification: Ref<PartyRoleSpecification>,
account: Vec<Ref<Account>>,
agreement: Vec<Ref<Agreement>>,
payment_method: Vec<Ref<PaymentMethod>>,
contact_medium: Vec<ContactMedium>,
credit_profile: Vec<CreditProfile>,
characteristic: Vec<Characteristic>,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_patch_body!(PartyRoleUpdate);
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PartyRoleKind {
PartyRole,
Supplier,
Producer,
Consumer,
BusinessPartner,
Other(String),
}
impl PartyRoleKind {
#[must_use]
pub fn all() -> &'static [Self] {
&[
Self::PartyRole,
Self::Supplier,
Self::Producer,
Self::Consumer,
Self::BusinessPartner,
]
}
#[must_use]
pub fn from_type_name(name: &str) -> Self {
match name {
"Supplier" => Self::Supplier,
"Producer" => Self::Producer,
"Consumer" => Self::Consumer,
"BusinessPartner" => Self::BusinessPartner,
"PartyRole" => Self::PartyRole,
other => Self::Other(other.to_owned()),
}
}
#[must_use]
pub fn type_name(&self) -> &str {
match self {
Self::PartyRole => "PartyRole",
Self::Supplier => "Supplier",
Self::Producer => "Producer",
Self::Consumer => "Consumer",
Self::BusinessPartner => "BusinessPartner",
Self::Other(name) => name,
}
}
}
impl PartyRole {
#[must_use]
pub fn kind(&self) -> PartyRoleKind {
PartyRoleKind::from_type_name(self.type_name())
}
}
tmf_struct! {
@name = "PartyRoleSpecification", @ref = "PartyRoleSpecificationRef";
pub struct PartyRoleSpecification {
id: String,
href: String,
name: String,
description: String,
version: String,
is_bundle: bool,
last_update: Timestamp,
lifecycle_status: String,
status: String,
spec_characteristic: Vec<CharacteristicSpecification>,
entity_spec_relationship: Vec<EntitySpecificationRelationship>,
agreement_specification: Vec<Ref<AgreementSpecification>>,
permission_specification_set: Vec<Ref<PermissionSpecificationSet>>,
constraint: Vec<Ref<Constraint>>,
attachment: Vec<Attachment>,
target_entity_schema: TargetEntitySchema,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_entity!(PartyRoleSpecification);
tmf_struct! {
@name = "PartyRoleSpecification";
pub struct PartyRoleSpecificationCreate {
@required {
name: String,
}
id: String,
description: String,
version: String,
is_bundle: bool,
last_update: Timestamp,
lifecycle_status: String,
status: String,
spec_characteristic: Vec<CharacteristicSpecification>,
entity_spec_relationship: Vec<EntitySpecificationRelationship>,
agreement_specification: Vec<Ref<AgreementSpecification>>,
permission_specification_set: Vec<Ref<PermissionSpecificationSet>>,
constraint: Vec<Ref<Constraint>>,
attachment: Vec<Attachment>,
target_entity_schema: TargetEntitySchema,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_struct! {
@name = "PartyRoleSpecification";
pub struct PartyRoleSpecificationUpdate {
name: String,
description: String,
version: String,
is_bundle: bool,
last_update: Timestamp,
lifecycle_status: String,
status: String,
spec_characteristic: Vec<CharacteristicSpecification>,
entity_spec_relationship: Vec<EntitySpecificationRelationship>,
agreement_specification: Vec<Ref<AgreementSpecification>>,
permission_specification_set: Vec<Ref<PermissionSpecificationSet>>,
constraint: Vec<Ref<Constraint>>,
attachment: Vec<Attachment>,
target_entity_schema: TargetEntitySchema,
related_party: Vec<RelatedParty>,
valid_for: TimePeriod,
}
}
tmf_patch_body!(PartyRoleSpecificationUpdate);
tmf_value! {
pub struct EntitySpecificationRelationship {
href: String,
name: String,
relationship_type: String,
role: String,
association_spec: Ref<AssociationSpecification>,
valid_for: TimePeriod,
@renamed {
"@baseType" base_type: String,
"@schemaLocation" schema_location: String,
}
}
}
tmf_value! {
pub struct TargetEntitySchema {
@renamed {
"@schemaLocation" schema_location: String,
"@type" ty: String,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_subclass_survives_both_directions() {
for kind in PartyRoleKind::all() {
assert_eq!(
&PartyRoleKind::from_type_name(kind.type_name()),
kind,
"{} does not read back as itself",
kind.type_name()
);
}
}
#[test]
fn a_vendor_subclass_keeps_its_name_rather_than_becoming_the_base() {
let kind = PartyRoleKind::from_type_name("WholesalePartner");
assert_eq!(kind, PartyRoleKind::Other("WholesalePartner".to_owned()));
assert_eq!(kind.type_name(), "WholesalePartner");
}
#[test]
fn a_role_reports_the_subclass_its_payload_declared() {
let supplier: PartyRole = serde_json::from_str(r#"{"id":"7","@type":"Supplier"}"#).unwrap();
assert_eq!(supplier.kind(), PartyRoleKind::Supplier);
let bare: PartyRole = serde_json::from_str(r#"{"id":"7"}"#).unwrap();
assert_eq!(bare.kind(), PartyRoleKind::PartyRole);
}
}