jpreprocess_core/
word_line.rs

1use std::fmt::Display;
2
3/// A struct to represent a line in a word dictionary file.
4///
5/// <div class="warning">
6///
7/// This struct is experimental and may change in the future.
8///
9/// </div>
10#[derive(Clone, PartialEq, Debug)]
11pub struct WordDetailsLine {
12    pub pos: String,
13    pub pos_group1: String,
14    pub pos_group2: String,
15    pub pos_group3: String,
16    pub ctype: String,
17    pub cform: String,
18    pub orig: String,
19    pub read: String,
20    pub pron: String,
21    pub acc_morasize: String,
22    pub chain_rule: String,
23    pub chain_flag: String,
24}
25
26impl Default for WordDetailsLine {
27    fn default() -> Self {
28        Self {
29            pos: "名詞".to_string(),
30            pos_group1: "*".to_string(),
31            pos_group2: "*".to_string(),
32            pos_group3: "*".to_string(),
33            ctype: "*".to_string(),
34            cform: "*".to_string(),
35            orig: "*".to_string(),
36            read: "*".to_string(),
37            pron: "*".to_string(),
38            acc_morasize: "*/*".to_string(),
39            chain_rule: "*".to_string(),
40            chain_flag: "*".to_string(),
41        }
42    }
43}
44
45impl WordDetailsLine {
46    pub fn from_strs(details: &[&str]) -> Self {
47        assert_eq!(details.len(), 12, "line must have exactly 12 columns");
48
49        Self {
50            pos: details[0].to_string(),
51            pos_group1: details[1].to_string(),
52            pos_group2: details[2].to_string(),
53            pos_group3: details[3].to_string(),
54            ctype: details[4].to_string(),
55            cform: details[5].to_string(),
56            orig: details[6].to_string(),
57            read: details[7].to_string(),
58            pron: details[8].to_string(),
59            acc_morasize: details[9].to_string(),
60            chain_rule: details[10].to_string(),
61            chain_flag: details[11].to_string(),
62        }
63    }
64
65    pub fn to_str_vec(&self, orig: String) -> [String; 12] {
66        [
67            self.pos.clone(),
68            self.pos_group1.clone(),
69            self.pos_group2.clone(),
70            self.pos_group3.clone(),
71            self.ctype.clone(),
72            self.cform.clone(),
73            orig,
74            self.read.clone(),
75            self.pron.clone(),
76            self.acc_morasize.clone(),
77            self.chain_rule.clone(),
78            self.chain_flag.clone(),
79        ]
80    }
81}
82
83impl Display for WordDetailsLine {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(
86            f,
87            "{},{},{},{},{},{},{},{},{},{},{},{}",
88            self.pos,
89            self.pos_group1,
90            self.pos_group2,
91            self.pos_group3,
92            self.ctype,
93            self.cform,
94            self.orig,
95            self.read,
96            self.pron,
97            self.acc_morasize,
98            self.chain_rule,
99            self.chain_flag
100        )
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use crate::word_details::WordDetails;
107
108    use super::WordDetailsLine;
109
110    #[test]
111    fn default_same_with_details() {
112        let default = WordDetails::default();
113        let details = WordDetailsLine::default().try_into().unwrap();
114        assert_eq!(default, details);
115    }
116}