sudachi/plugin/
mod.rs

1/*
2 * Copyright (c) 2021 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
53impl From<LLError> for PluginError {
54    fn from(e: LLError) -> Self {
55        PluginError::Libloading {
56            source: e,
57            message: String::new(),
58        }
59    }
60}
61
62pub(crate) struct Plugins {
63    pub(crate) connect_cost: PluginContainer<dyn EditConnectionCostPlugin>,
64    pub(crate) input_text: PluginContainer<dyn InputTextPlugin>,
65    pub(crate) oov: PluginContainer<dyn OovProviderPlugin>,
66    pub(crate) path_rewrite: PluginContainer<dyn PathRewritePlugin>,
67}
68
69impl Plugins {
70    pub(crate) fn load<'a, 'b>(
71        cfg: &'a Config,
72        grammar: &'a mut Grammar<'b>,
73    ) -> SudachiResult<Plugins>
74    where
75        'b: 'a,
76    {
77        let plugins = Plugins {
78            connect_cost: load_plugins_of(cfg, grammar)
79                .map_err(|e| e.with_context("connect_cost"))?,
80            input_text: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("input_text"))?,
81            oov: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("oov"))?,
82            path_rewrite: load_plugins_of(cfg, grammar)
83                .map_err(|e| e.with_context("path_rewrite"))?,
84        };
85        Ok(plugins)
86    }
87}