redispatch_xml/types/attr_v.rs
1//! `AttrV` and `AttrVWithScheme` -- serde wrappers for EDIFACT/CIM XML simpleContent elements with attributes.
2use std::fmt;
3
4use serde::{Deserialize, Serialize};
5
6/// Generic wrapper for the ENTSO-E/BDEW attr-v pattern:
7///
8/// ```xml
9/// <ElementName v="value"/>
10/// ```
11///
12/// The `v` attribute holds the actual value. This pattern appears in all
13/// ENTSO-E-derived and most BDEW XML documents (ActivationDocument,
14/// PlannedResourceScheduleDocument, AcknowledgementDocument,
15/// NetworkConstraintDocument, Kostenblatt).
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub struct AttrV<T> {
18 /// The element value, stored in the `v` XML attribute.
19 #[serde(rename = "@v")]
20 pub v: T,
21}
22
23impl<T> AttrV<T> {
24 /// Create a new `AttrV` wrapper.
25 pub fn new(v: T) -> Self {
26 Self { v }
27 }
28}
29
30impl<T: Clone> AttrV<T> {
31 /// Unwrap and clone the inner value.
32 pub fn value(&self) -> T {
33 self.v.clone()
34 }
35}
36
37impl<T> From<T> for AttrV<T> {
38 fn from(v: T) -> Self {
39 Self { v }
40 }
41}
42
43impl<T: fmt::Display> fmt::Display for AttrV<T> {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 self.v.fmt(f)
46 }
47}
48
49/// Wrapper for elements whose value is in a `v` attribute **and** that have
50/// an additional `codingScheme` attribute:
51///
52/// ```xml
53/// <SenderIdentification v="4045399000008" codingScheme="A10"/>
54/// ```
55///
56/// Used for market participant IDs (`SenderIdentification`,
57/// `ReceiverIdentification`, `ResourceProvider`) and control zone references
58/// (`ConnectingArea`, `AcquiringArea`) in the attr-v document family.
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60pub struct AttrVWithScheme<T, S = crate::types::CodingScheme> {
61 /// The element value.
62 #[serde(rename = "@v")]
63 pub v: T,
64 /// The coding scheme qualifier.
65 #[serde(rename = "@codingScheme")]
66 pub coding_scheme: S,
67}
68
69impl<T, S> AttrVWithScheme<T, S> {
70 /// Create a new `AttrVWithScheme` wrapper.
71 pub fn new(v: T, coding_scheme: S) -> Self {
72 Self { v, coding_scheme }
73 }
74}
75
76/// Wrapper for IEC 62325 simpleContent elements: text content with an
77/// additional `codingScheme` attribute:
78///
79/// ```xml
80/// <mRID codingScheme="A10">4045399000008</mRID>
81/// ```
82///
83/// Used in Kaskade, Unavailability_MarketDocument, and
84/// StatusRequest_MarketDocument.
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86pub struct SimpleContent<T, S = crate::types::CodingScheme> {
87 /// Text content of the element.
88 #[serde(rename = "$text")]
89 pub value: T,
90 /// Coding scheme qualifier attribute.
91 #[serde(rename = "@codingScheme")]
92 pub coding_scheme: S,
93}
94
95impl<T, S> SimpleContent<T, S> {
96 /// Create a new `SimpleContent` wrapper.
97 pub fn new(value: T, coding_scheme: S) -> Self {
98 Self {
99 value,
100 coding_scheme,
101 }
102 }
103}