Skip to main content

sudachi/analysis/
mode.rs

1/*
2 *  Copyright (c) 2026 Works Applications Co., Ltd.
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *   Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17use std::fmt::{Display, Formatter};
18use std::str::FromStr;
19
20/// Unit to split text
21///
22/// Some examples:
23/// ```text
24/// A:選挙/管理/委員/会
25/// B:選挙/管理/委員会
26/// C:選挙管理委員会
27///
28/// A:客室/乗務/員
29/// B:客室/乗務員
30/// C:客室乗務員
31///
32/// A:労働/者/協同/組合
33/// B:労働者/協同/組合
34/// C:労働者協同組合
35///
36/// A:機能/性/食品
37/// B:機能性/食品
38/// C:機能性食品
39/// ```
40///
41/// See [Sudachi documentation](https://github.com/WorksApplications/Sudachi#the-modes-of-splitting)
42/// for more details
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum Mode {
45    /// Short
46    A,
47
48    /// Middle (similar to "word")
49    B,
50
51    /// Named Entity
52    C,
53}
54
55impl FromStr for Mode {
56    type Err = &'static str;
57
58    fn from_str(s: &str) -> Result<Self, Self::Err> {
59        match s {
60            "A" | "a" => Ok(Mode::A),
61            "B" | "b" => Ok(Mode::B),
62            "C" | "c" => Ok(Mode::C),
63            _ => Err("Mode must be one of \"A\", \"B\", or \"C\" (in lower or upper case)."),
64        }
65    }
66}
67
68impl Display for Mode {
69    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70        let repr = match self {
71            Mode::A => "A",
72            Mode::B => "B",
73            Mode::C => "C",
74        };
75        f.write_str(repr)
76    }
77}