1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::DictionaryStore;
use byteorder::{ByteOrder, LittleEndian};
use jpreprocess_core::{error::JPreprocessErrorKind, JPreprocessResult};

impl<'a> DictionaryStore<'a> for lindera_core::dictionary::Dictionary {
    fn get_bytes(&'a self, id: u32) -> JPreprocessResult<&'a [u8]> {
        get_bytes(id, &self.words_idx_data, &self.words_data)
    }
    fn identifier(&self) -> Option<String> {
        get_metadata(&self.words_idx_data, &self.words_data)
    }
}

impl<'a> DictionaryStore<'a> for lindera_core::dictionary::UserDictionary {
    fn get_bytes(&'a self, id: u32) -> JPreprocessResult<&'a [u8]> {
        get_bytes(id, &self.words_idx_data, &self.words_data)
    }
    fn identifier(&self) -> Option<String> {
        get_metadata(&self.words_idx_data, &self.words_data)
    }
}

fn get_metadata(words_idx_data: &[u8], words_data: &[u8]) -> Option<String> {
    let metadata_end = LittleEndian::read_u32(&words_idx_data[0..4]) as usize;
    if metadata_end == 0 {
        return None;
    }

    String::from_utf8(words_data[0..metadata_end].to_vec()).ok()
}

fn get_bytes<'a>(
    id: u32,
    words_idx_data: &'a [u8],
    words_data: &'a [u8],
) -> JPreprocessResult<&'a [u8]> {
    let start_point = 4 * id as usize;
    if words_idx_data.len() < start_point + 4 {
        return Err(JPreprocessErrorKind::WordNotFoundError
            .with_error(anyhow::anyhow!("Word with id {:?} does not exist.", id)));
    }

    let idx = LittleEndian::read_u32(&words_idx_data[start_point..start_point + 4]) as usize;
    let idx_next = if words_idx_data.len() < start_point + 8 {
        words_data.len()
    } else {
        LittleEndian::read_u32(&words_idx_data[start_point + 4..start_point + 8]) as usize
    };

    Ok(&words_data[idx..idx_next])
}