Skip to main content

kenall_rs/
ui.rs

1//* Handling the result output
2
3use ansi_term::Colour::Green;
4use cli_table::{format::Justify, print_stdout, Cell, CellStruct, Style, Table};
5
6use crate::PostalCodeResponse;
7
8pub trait Ui {
9    fn display_address(&self);
10    fn display_corporate(&self);
11    fn justify_content(data: &str, position: Justify) -> CellStruct;
12}
13
14impl Ui for PostalCodeResponse {
15    fn display_address(&self) {
16        let rawdata = &self.data[0];
17        let address_table = vec![
18            vec![
19                "Postal code".cell().justify(Justify::Center),
20                Self::justify_content(&rawdata.postal_code, Justify::Right),
21            ],
22            vec![
23                "Prefecture".cell().justify(Justify::Center),
24                Self::justify_content(&rawdata.prefecture, Justify::Right),
25            ],
26            vec![
27                "City".cell().justify(Justify::Center),
28                Self::justify_content(&rawdata.city, Justify::Right),
29            ],
30            vec![
31                "Town".cell().justify(Justify::Center),
32                Self::justify_content(&rawdata.town, Justify::Right),
33            ],
34            vec![
35                "Town raw".cell().justify(Justify::Center),
36                Self::justify_content(&rawdata.town_raw, Justify::Right),
37            ],
38            vec![
39                "Koaza".cell().justify(Justify::Center),
40                Self::justify_content(&rawdata.koaza, Justify::Right),
41            ],
42            vec![
43                "Kyoto street".cell().justify(Justify::Center),
44                Self::justify_content(&rawdata.kyoto_street, Justify::Right),
45            ],
46            vec![
47                "Building".cell().justify(Justify::Center),
48                Self::justify_content(&rawdata.building, Justify::Right),
49            ],
50            vec![
51                "Floor".cell().justify(Justify::Center),
52                Self::justify_content(&rawdata.floor, Justify::Right),
53            ],
54            // Katakana below
55            vec![
56                "Pref kana".cell().justify(Justify::Center),
57                Self::justify_content(&rawdata.prefecture_kana, Justify::Right),
58            ],
59            vec![
60                "City kana".cell().justify(Justify::Center),
61                Self::justify_content(&rawdata.city_kana, Justify::Right),
62            ],
63            vec![
64                "Town kana".cell().justify(Justify::Center),
65                Self::justify_content(&rawdata.town_kana, Justify::Right),
66            ],
67            vec![
68                "Town kana raw".cell().justify(Justify::Center),
69                Self::justify_content(&rawdata.town_kana_raw, Justify::Right),
70            ],
71        ]
72        .table()
73        .title(vec![
74            "Name".cell().bold(true),
75            "Data".cell().bold(true).justify(Justify::Center),
76        ])
77        .bold(true);
78
79        let _ = print_stdout(address_table);
80        Self::display_corporate(&self);
81    }
82
83    fn display_corporate(&self) {
84        let corporation = &self.data[0].corporation;
85        match corporation {
86            Some(cooporate) => {
87                let cooporate_table = vec![
88                    vec![
89                        "Name".cell().justify(Justify::Center),
90                        Self::justify_content(&cooporate.name, Justify::Right),
91                    ],
92                    vec![
93                        "Name kana".cell().justify(Justify::Center),
94                        Self::justify_content(&cooporate.name_kana, Justify::Right),
95                    ],
96                    vec![
97                        "Block log".cell().justify(Justify::Center),
98                        Self::justify_content(&cooporate.block_lot, Justify::Right),
99                    ],
100                    vec![
101                        "Post office".cell().justify(Justify::Center),
102                        Self::justify_content(&cooporate.post_office, Justify::Right),
103                    ],
104                ];
105                let _ = print_stdout(cooporate_table);
106            }
107            None => {
108                let _ = print_stdout(
109                    vec![vec!["No Cooporate Data".cell().justify(Justify::Left)]]
110                        .table()
111                        .bold(true),
112                );
113            }
114        };
115    }
116
117    fn justify_content(data: &str, position: Justify) -> CellStruct {
118        Green.paint(data).cell().justify(position)
119    }
120}