Skip to main content

sudachi/dic/
pos.rs

1/*
2 * Copyright (c) 2025-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 nom::number::complete::le_i16;
18
19use crate::error::SudachiResult;
20
21use super::read::utf16_string::utf16_string;
22
23pub const POS_DEPTH: usize = 6;
24
25/// A part of speech
26///
27/// Its length must be `POS_DEPTH`
28#[allow(clippy::upper_case_acronyms)]
29type POS = Vec<String>;
30
31#[derive(Clone, Debug, Default)]
32pub struct PosList(Vec<POS>);
33
34impl From<PosList> for Vec<POS> {
35    fn from(val: PosList) -> Self {
36        val.0
37    }
38}
39
40impl std::ops::Deref for PosList {
41    type Target = Vec<POS>;
42    fn deref(&self) -> &Vec<POS> {
43        &self.0
44    }
45}
46
47impl std::ops::DerefMut for PosList {
48    fn deref_mut(&mut self) -> &mut Vec<POS> {
49        &mut self.0
50    }
51}
52
53impl IntoIterator for PosList {
54    type Item = POS;
55    type IntoIter = std::vec::IntoIter<Self::Item>;
56    fn into_iter(self) -> Self::IntoIter {
57        self.0.into_iter()
58    }
59}
60
61impl PosList {
62    pub fn from_bytes(buf: &[u8]) -> SudachiResult<Self> {
63        let (rest, num_pos) = le_i16(buf)?;
64        let (_rest, pos_list) =
65            nom::multi::count(nom::multi::count(utf16_string, POS_DEPTH), num_pos as usize)(rest)?;
66        Ok(PosList(pos_list))
67    }
68}