konduto/types/vehicle/
owner.rs

1use crate::types::validation_errors::ValidationError;
2use serde::{Deserialize, Serialize};
3
4use super::{OwnerName, OwnerTaxId};
5
6/// Dados do proprietário do veículo
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct VehicleOwner {
9    /// Documento do proprietário (obrigatório)
10    pub tax_id: OwnerTaxId,
11
12    /// Nome do proprietário (recomendado)
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub name: Option<OwnerName>,
15}
16
17impl VehicleOwner {
18    /// Cria um builder para construir o proprietário
19    pub fn builder() -> VehicleOwnerBuilder {
20        VehicleOwnerBuilder::default()
21    }
22}
23
24/// Builder para criar proprietários
25#[derive(Default)]
26pub struct VehicleOwnerBuilder {
27    tax_id: Option<OwnerTaxId>,
28    name: Option<OwnerName>,
29}
30
31impl VehicleOwnerBuilder {
32    pub fn tax_id(mut self, tax_id: OwnerTaxId) -> Self {
33        self.tax_id = Some(tax_id);
34        self
35    }
36
37    pub fn tax_id_str(mut self, tax_id: impl Into<String>) -> Result<Self, ValidationError> {
38        self.tax_id = Some(OwnerTaxId::try_new(tax_id)?);
39        Ok(self)
40    }
41
42    pub fn name(mut self, name: OwnerName) -> Self {
43        self.name = Some(name);
44        self
45    }
46
47    pub fn name_str(mut self, name: impl Into<String>) -> Result<Self, ValidationError> {
48        self.name = Some(OwnerName::try_new(name)?);
49        Ok(self)
50    }
51
52    pub fn build(self) -> Result<VehicleOwner, ValidationError> {
53        Ok(VehicleOwner {
54            tax_id: self
55                .tax_id
56                .ok_or_else(|| ValidationError::EmptyField("owner_tax_id".to_string()))?,
57            name: self.name,
58        })
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_vehicle_owner_builder() {
68        let owner = VehicleOwner::builder()
69            .tax_id_str("540.830.640-21")
70            .unwrap()
71            .name_str("Cicero")
72            .unwrap()
73            .build()
74            .unwrap();
75
76        assert_eq!(owner.tax_id.as_str(), "540.830.640-21");
77        assert_eq!(owner.name.as_ref().unwrap().as_str(), "Cicero");
78    }
79
80    #[test]
81    fn test_vehicle_owner_minimal() {
82        let owner = VehicleOwner::builder()
83            .tax_id_str("123.456.789-00")
84            .unwrap()
85            .build()
86            .unwrap();
87
88        assert_eq!(owner.tax_id.as_str(), "123.456.789-00");
89        assert!(owner.name.is_none());
90    }
91
92    #[test]
93    fn test_vehicle_owner_missing_tax_id() {
94        let result = VehicleOwner::builder().name_str("Test").unwrap().build();
95
96        assert!(result.is_err());
97    }
98
99    #[test]
100    fn test_vehicle_owner_serialization() {
101        let owner = VehicleOwner::builder()
102            .tax_id_str("111.222.333-44")
103            .unwrap()
104            .name_str("João")
105            .unwrap()
106            .build()
107            .unwrap();
108
109        let json = serde_json::to_string(&owner).unwrap();
110        assert!(json.contains("111.222.333-44"));
111        assert!(json.contains("João"));
112    }
113}
114