nfe_parser/base/
totais.rs1use super::Error;
4use serde::{Deserialize, Deserializer, Serialize, Serializer};
5use std::str::FromStr;
6
7#[derive(Debug, PartialEq, Clone)]
9pub struct Totalizacao {
10 pub valor_base_calculo: f32,
12 pub valor_icms: f32,
14 pub valor_produtos: f32,
16 pub valor_frete: f32,
18 pub valor_seguro: f32,
20 pub valor_desconto: f32,
22 pub valor_outros: f32,
24 pub valor_pis: f32,
26 pub valor_cofins: f32,
28 pub valor_total: f32,
30 pub valor_aproximado_tributos: f32,
32}
33
34impl FromStr for Totalizacao {
35 type Err = Error;
36
37 fn from_str(s: &str) -> Result<Self, Self::Err> {
38 quick_xml::de::from_str(s).map_err(|e| e.into())
39 }
40}
41
42impl ToString for Totalizacao {
43 fn to_string(&self) -> String {
44 quick_xml::se::to_string(self).expect("Falha ao serializar a totalização")
45 }
46}
47
48impl Serialize for Totalizacao {
49 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50 where
51 S: Serializer,
52 {
53 let icms = IcmsTot {
54 valor_base_calculo: self.valor_base_calculo,
55 valor_icms: self.valor_icms,
56 valor_produtos: self.valor_produtos,
57 valor_frete: self.valor_frete,
58 valor_seguro: self.valor_seguro,
59 valor_desconto: self.valor_desconto,
60 valor_outros: self.valor_outros,
61 valor_pis: self.valor_pis,
62 valor_cofins: self.valor_cofins,
63 valor_total: self.valor_total,
64 valor_aproximado_tributos: self.valor_aproximado_tributos,
65 };
66
67 let total = TotalContainer { icms };
68
69 total.serialize(serializer)
70 }
71}
72
73impl<'de> Deserialize<'de> for Totalizacao {
74 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75 where
76 D: Deserializer<'de>,
77 {
78 let helper = TotalContainer::deserialize(deserializer)?;
79 Ok(Totalizacao {
80 valor_base_calculo: helper.icms.valor_base_calculo,
81 valor_icms: helper.icms.valor_icms,
82 valor_produtos: helper.icms.valor_produtos,
83 valor_frete: helper.icms.valor_frete,
84 valor_seguro: helper.icms.valor_seguro,
85 valor_desconto: helper.icms.valor_desconto,
86 valor_outros: helper.icms.valor_outros,
87 valor_pis: helper.icms.valor_pis,
88 valor_cofins: helper.icms.valor_cofins,
89 valor_total: helper.icms.valor_total,
90 valor_aproximado_tributos: helper.icms.valor_aproximado_tributos,
91 })
92 }
93}
94
95#[derive(Deserialize, Serialize)]
96#[serde(rename = "total")]
97struct TotalContainer {
98 #[serde(rename = "ICMSTot")]
99 icms: IcmsTot,
100}
101
102#[derive(Deserialize, Serialize)]
103struct IcmsTot {
104 #[serde(rename = "$unflatten=vBC")]
105 valor_base_calculo: f32,
106 #[serde(rename = "$unflatten=vICMS")]
107 valor_icms: f32,
108 #[serde(rename = "$unflatten=vProd")]
109 valor_produtos: f32,
110 #[serde(rename = "$unflatten=vFrete")]
111 valor_frete: f32,
112 #[serde(rename = "$unflatten=vSeg")]
113 valor_seguro: f32,
114 #[serde(rename = "$unflatten=vDesc")]
115 valor_desconto: f32,
116 #[serde(rename = "$unflatten=vOutro")]
117 valor_outros: f32,
118 #[serde(rename = "$unflatten=vPIS")]
119 valor_pis: f32,
120 #[serde(rename = "$unflatten=vCOFINS")]
121 valor_cofins: f32,
122 #[serde(rename = "$unflatten=vNF")]
123 valor_total: f32,
124 #[serde(rename = "$unflatten=vTotTrib")]
125 valor_aproximado_tributos: f32,
126}