Skip to main content

country_based_config/
country_based_config.rs

1//! An example with country based config to use the correct date formatting for users from different countries.
2//! Run with: `cargo run --example country_based_config`
3
4use partial_date::models::{self, ComponentOrder, Config, DateComponent};
5
6fn main() {
7    println!("Example: Country-Based Config");
8    println!("============================\n");
9
10    let south_african_user_1 = User {
11        name: "Albie Sachs".to_string(),
12        date_input: "13/10/94".to_string(),
13        country: Country::SouthAfrica,
14    };
15
16    let south_african_user_2 = User {
17        name: "Robert Sobukwe".to_string(),
18        date_input: "02/06".to_string(),
19        country: Country::SouthAfrica,
20    };
21
22    let liberian_user = User {
23        name: "Leymah Gbowee".to_string(),
24        date_input: "02/12/24".to_string(),
25        country: Country::Liberia,
26    };
27
28    let liberian_user_2 = User {
29        name: "George Weah".to_string(),
30        date_input: "02/06".to_string(),
31        country: Country::Liberia,
32    };
33
34    let all_users = vec![
35        south_african_user_1,
36        south_african_user_2,
37        liberian_user,
38        liberian_user_2,
39    ];
40
41    for user in all_users {
42        // Matching country to config allows us to cleanly parse the different
43        // users with their appropriate date configs
44        let config = user.country.get_config();
45
46        let input = models::Input {
47            utterance: user.date_input.clone(),
48            config: Some(config),
49        };
50
51        let result = partial_date::extract::extract(input);
52
53        println!(
54            "User: {} from {:?} input: '{}'",
55            user.name, user.country, user.date_input
56        );
57        println!("  Parsed Day:   {:?}", result.day.value);
58        println!("  Parsed Month: {:?}", result.month.number);
59        println!("  Parsed Year:  {:?}\n", result.year.value);
60    }
61}
62
63// A simple user struct to hold the date input and country information
64struct User {
65    name: String,
66    date_input: String,
67    country: Country,
68}
69
70// Enum to represent different countries with different date formats
71#[derive(Debug)]
72pub enum Country {
73    SouthAfrica,
74    Liberia,
75}
76
77// Implement a method to get the appropriate config for each country
78impl Country {
79    pub fn get_config(&self) -> Config {
80        match self {
81            Country::SouthAfrica => Config {
82                component_order: ComponentOrder {
83                    first: DateComponent::Day,
84                    second: DateComponent::Month,
85                    third: DateComponent::Year,
86                },
87                ..Default::default()
88            },
89            Country::Liberia => Config {
90                component_order: ComponentOrder {
91                    first: DateComponent::Month,
92                    second: DateComponent::Day,
93                    third: DateComponent::Year,
94                },
95                ..Default::default()
96            },
97        }
98    }
99}