1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::str::FromStr;
#[derive(Debug, Serialize, Deserialize)]
pub struct QualifiedLawyerInfoData {
#[serde(
rename = "{urn:ietf:params:xml:ns:qualifiedLawyer-1.0}qualifiedLawyer:accreditationId"
)]
pub accreditation_id: String,
#[serde(
rename = "{urn:ietf:params:xml:ns:qualifiedLawyer-1.0}qualifiedLawyer:accreditationBody"
)]
pub accreditation_body: String,
#[serde(
rename = "{urn:ietf:params:xml:ns:qualifiedLawyer-1.0}qualifiedLawyer:accreditationYear",
deserialize_with = "deserialize_date_year",
serialize_with = "serialize_date_year"
)]
pub accreditation_year: i32,
#[serde(rename = "{urn:ietf:params:xml:ns:qualifiedLawyer-1.0}qualifiedLawyer:jurisdictionCC")]
pub jurisdiction_country: String,
#[serde(
rename = "{urn:ietf:params:xml:ns:qualifiedLawyer-1.0}qualifiedLawyer:jurisdictionSP",
skip_serializing_if = "Option::is_none",
default
)]
pub jurisdiction_province: Option<String>,
}
struct DateYearVisitor;
impl<'de> serde::de::Visitor<'de> for DateYearVisitor {
type Value = i32;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a formatted date string")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if value.ends_with(&"Z") {
let len = value.len();
if len < 2 {
return Err(E::custom("Invalid year"));
}
match i32::from_str(&value[..len - 1]) {
Ok(v) => Ok(v),
Err(e) => Err(E::custom(e)),
}
} else {
Err(E::custom("Invalid year"))
}
}
}
fn deserialize_date_year<'de, D>(d: D) -> Result<i32, D::Error>
where
D: serde::de::Deserializer<'de>,
{
d.deserialize_str(DateYearVisitor)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn serialize_date_year<S>(d: &i32, s: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
s.serialize_str(&format!("{}Z", d))
}