konduto/types/travel/
loyalty_program.rs1use crate::types::validation_errors::ValidationError;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct LoyaltyProgram {
7 pub program: String,
9
10 pub category: String,
12}
13
14impl LoyaltyProgram {
15 pub fn new(program: impl Into<String>, category: impl Into<String>) -> Self {
17 Self {
18 program: program.into(),
19 category: category.into(),
20 }
21 }
22
23 pub fn builder() -> LoyaltyProgramBuilder {
25 LoyaltyProgramBuilder::default()
26 }
27}
28
29#[derive(Default)]
31pub struct LoyaltyProgramBuilder {
32 program: Option<String>,
33 category: Option<String>,
34}
35
36impl LoyaltyProgramBuilder {
37 pub fn program(mut self, program: impl Into<String>) -> Self {
38 self.program = Some(program.into());
39 self
40 }
41
42 pub fn category(mut self, category: impl Into<String>) -> Self {
43 self.category = Some(category.into());
44 self
45 }
46
47 pub fn build(self) -> Result<LoyaltyProgram, ValidationError> {
48 let program = self
49 .program
50 .ok_or_else(|| ValidationError::EmptyField("loyalty_program".to_string()))?;
51
52 let category = self
53 .category
54 .ok_or_else(|| ValidationError::EmptyField("loyalty_category".to_string()))?;
55
56 if program.trim().is_empty() {
57 return Err(ValidationError::EmptyField("loyalty_program".to_string()));
58 }
59
60 if category.trim().is_empty() {
61 return Err(ValidationError::EmptyField("loyalty_category".to_string()));
62 }
63
64 Ok(LoyaltyProgram { program, category })
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_loyalty_program_new() {
74 let loyalty = LoyaltyProgram::new("smiles", "gold");
75 assert_eq!(loyalty.program, "smiles");
76 assert_eq!(loyalty.category, "gold");
77 }
78
79 #[test]
80 fn test_loyalty_program_builder() {
81 let loyalty = LoyaltyProgram::builder()
82 .program("multiplus")
83 .category("silver")
84 .build()
85 .unwrap();
86
87 assert_eq!(loyalty.program, "multiplus");
88 assert_eq!(loyalty.category, "silver");
89 }
90
91 #[test]
92 fn test_loyalty_program_serialization() {
93 let loyalty = LoyaltyProgram::new("smiles", "gold");
94 let json = serde_json::to_string(&loyalty).unwrap();
95 assert!(json.contains("smiles"));
96 assert!(json.contains("gold"));
97 }
98}
99