Skip to main content

sudachi/
plugin.rs

1/*
2 * Copyright (c) 2021-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 libloading::Error as LLError;
18use thiserror::Error;
19
20use crate::config::Config;
21use crate::dic::grammar::Grammar;
22use crate::plugin::connect_cost::EditConnectionCostPlugin;
23use crate::plugin::input_text::InputTextPlugin;
24use crate::plugin::loader::{load_plugins_of, PluginContainer};
25use crate::plugin::oov::OovProviderPlugin;
26use crate::plugin::path_rewrite::PathRewritePlugin;
27use crate::prelude::*;
28
29pub use self::loader::PluginCategory;
30
31pub mod connect_cost;
32pub mod dso;
33pub mod input_text;
34mod loader;
35pub mod oov;
36pub mod path_rewrite;
37
38#[derive(Error, Debug)]
39pub enum PluginError {
40    #[error("IO Error: {0}")]
41    Io(#[from] std::io::Error),
42
43    #[error("Libloading Error: {message} ; {source}")]
44    Libloading { source: LLError, message: String },
45
46    #[error("Serde error: {0}")]
47    SerdeError(#[from] serde_json::Error),
48
49    #[error("Invalid data format: {0}")]
50    InvalidDataFormat(String),
51
52    #[error("Invalid data format at line {line}: {message}")]
53    InvalidDataFormatWithLine { line: usize, message: String },
54}
55
56impl From<LLError> for PluginError {
57    fn from(e: LLError) -> Self {
58        PluginError::Libloading {
59            source: e,
60            message: String::new(),
61        }
62    }
63}
64
65pub(crate) struct Plugins {
66    pub(crate) connect_cost: PluginContainer<dyn EditConnectionCostPlugin>,
67    pub(crate) input_text: PluginContainer<dyn InputTextPlugin>,
68    pub(crate) oov: PluginContainer<dyn OovProviderPlugin>,
69    pub(crate) path_rewrite: PluginContainer<dyn PathRewritePlugin>,
70}
71
72impl Plugins {
73    pub(crate) fn load<'a, 'b>(
74        cfg: &'a Config,
75        grammar: &'a mut Grammar<'b>,
76    ) -> SudachiResult<Plugins>
77    where
78        'b: 'a,
79    {
80        let plugins = Plugins {
81            connect_cost: load_plugins_of(cfg, grammar)
82                .map_err(|e| e.with_context("connect_cost"))?,
83            input_text: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("input_text"))?,
84            oov: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("oov"))?,
85            path_rewrite: load_plugins_of(cfg, grammar)
86                .map_err(|e| e.with_context("path_rewrite"))?,
87        };
88        Ok(plugins)
89    }
90}