sudachi/plugin/
mod.rs

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2021 Works Applications Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use libloading::Error as LLError;
use thiserror::Error;

use crate::config::Config;
use crate::dic::grammar::Grammar;
use crate::plugin::connect_cost::EditConnectionCostPlugin;
use crate::plugin::input_text::InputTextPlugin;
use crate::plugin::loader::{load_plugins_of, PluginContainer};
use crate::plugin::oov::OovProviderPlugin;
use crate::plugin::path_rewrite::PathRewritePlugin;
use crate::prelude::*;

pub use self::loader::PluginCategory;

pub mod connect_cost;
pub mod dso;
pub mod input_text;
mod loader;
pub mod oov;
pub mod path_rewrite;

#[derive(Error, Debug)]
pub enum PluginError {
    #[error("IO Error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Libloading Error: {message} ; {source}")]
    Libloading { source: LLError, message: String },

    #[error("Serde error: {0}")]
    SerdeError(#[from] serde_json::Error),

    #[error("Invalid data format: {0}")]
    InvalidDataFormat(String),
}

impl From<LLError> for PluginError {
    fn from(e: LLError) -> Self {
        PluginError::Libloading {
            source: e,
            message: String::new(),
        }
    }
}

pub(crate) struct Plugins {
    pub(crate) connect_cost: PluginContainer<dyn EditConnectionCostPlugin>,
    pub(crate) input_text: PluginContainer<dyn InputTextPlugin>,
    pub(crate) oov: PluginContainer<dyn OovProviderPlugin>,
    pub(crate) path_rewrite: PluginContainer<dyn PathRewritePlugin>,
}

impl Plugins {
    pub(crate) fn load<'a, 'b>(
        cfg: &'a Config,
        grammar: &'a mut Grammar<'b>,
    ) -> SudachiResult<Plugins>
    where
        'b: 'a,
    {
        let plugins = Plugins {
            connect_cost: load_plugins_of(cfg, grammar)
                .map_err(|e| e.with_context("connect_cost"))?,
            input_text: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("input_text"))?,
            oov: load_plugins_of(cfg, grammar).map_err(|e| e.with_context("oov"))?,
            path_rewrite: load_plugins_of(cfg, grammar)
                .map_err(|e| e.with_context("path_rewrite"))?,
        };
        Ok(plugins)
    }
}