mod.rs 3.41 KB
Newer Older
Éloïs's avatar
Éloïs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//  Copyright (C) 2018  The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Define durs-core cli subcommands options.

pub mod dbex;
inso's avatar
inso committed
19
pub mod keys;
Éloïs's avatar
Éloïs committed
20 21 22 23
pub mod modules;
pub mod reset;
pub mod start;

24 25 26
use crate::errors::DursCoreError;
use crate::{durs_exec_core_cmd, DursCore};
pub use dbex::*;
27
pub use duniter_network::cli::sync::SyncOpt;
28 29
use durs_conf::DuRsConf;
pub use keys::KeysOpt;
Éloïs's avatar
Éloïs committed
30
use log::Level;
31 32 33
pub use modules::*;
pub use reset::*;
pub use start::*;
34
use std::path::PathBuf;
Éloïs's avatar
Éloïs committed
35

36 37
/// Durs core options
pub struct DursCoreOptions {
38
    /// Keypairs file path
39 40 41
    pub keypairs_file: Option<PathBuf>,
    /// Set log level.
    pub logs_level: Option<Level>,
Éloïs's avatar
Éloïs committed
42
    /// Print logs in standard output
43
    pub log_stdout: bool,
44
    /// Set a custom user profile name
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 88 89 90
    pub profile_name: Option<String>,
    /// Path where user profiles are persisted
    pub profiles_path: Option<PathBuf>,
}

/// Durs executable command
pub trait DursExecutableCoreCommand {
    /// Execute Durs command
    fn execute(self, durs_core: DursCore<DuRsConf>) -> Result<(), DursCoreError>;
}

/// Executable module command
pub trait ExecutableModuleCommand {
    /// Execute module command
    fn execute_module_command(self, options: DursCoreOptions) -> Result<(), DursCoreError>;
}

/// Durs command with options
pub struct DursCommand<T: ExecutableModuleCommand> {
    /// Durs core options
    pub options: DursCoreOptions,
    /// Durs command
    pub command: DursCommandEnum<T>,
}

/// Durs command
pub enum DursCommandEnum<T: ExecutableModuleCommand> {
    /// Core command
    Core(DursCoreCommand),
    /// Other command
    Other(T),
}

impl<T: ExecutableModuleCommand> DursCommand<T> {
    /// Execute Durs command
    pub fn execute<PlugFunc>(self, plug_modules: PlugFunc) -> Result<(), DursCoreError>
    where
        PlugFunc: FnMut(&mut DursCore<DuRsConf>) -> Result<(), DursCoreError>,
    {
        match self.command {
            DursCommandEnum::Core(core_cmd) => {
                durs_exec_core_cmd!(core_cmd, self.options, plug_modules,)
            }
            DursCommandEnum::Other(cmd) => cmd.execute_module_command(self.options),
        }
    }
Éloïs's avatar
Éloïs committed
91 92 93 94
}

#[derive(StructOpt, Debug)]
/// Core cli subcommands
95
pub enum DursCoreCommand {
96
    /// Enable a module
Éloïs's avatar
Éloïs committed
97
    EnableOpt(EnableOpt),
98
    /// Disable a module
Éloïs's avatar
Éloïs committed
99
    DisableOpt(DisableOpt),
100
    /// List available modules
Éloïs's avatar
Éloïs committed
101
    ListModulesOpt(ListModulesOpt),
102
    /// Start node
Éloïs's avatar
Éloïs committed
103
    StartOpt(StartOpt),
104
    /// Synchronize
Éloïs's avatar
Éloïs committed
105
    SyncOpt(SyncOpt),
106
    /// Reset data or conf or all
Éloïs's avatar
Éloïs committed
107
    ResetOpt(ResetOpt),
108
    /// Database explorer
Éloïs's avatar
Éloïs committed
109
    DbExOpt(DbExOpt),
110
    /// Keys operations
inso's avatar
inso committed
111
    KeysOpt(KeysOpt),
Éloïs's avatar
Éloïs committed
112 113 114 115 116 117 118 119 120 121 122
}

/// InvalidInput
#[derive(Debug, Copy, Clone)]
pub struct InvalidInput(&'static str);

impl ToString for InvalidInput {
    fn to_string(&self) -> String {
        String::from(self.0)
    }
}