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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::fs::File;
use std::io::Read;

use anyhow::Context;
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use simplelog::{ConfigBuilder, LevelFilter, TermLogger, TerminalMode};


#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
    Off,
    Error,
    Warn,
    Info,
    Debug,
    Trace
}


impl Default for LogLevel {
    fn default() -> Self {
        Self::Error
    }
}


pub trait Config: Default + Serialize + DeserializeOwned {
    fn get_log_level(&self) -> Option<LogLevel> {
        None
    }
}


#[derive(Debug, Default)]
pub struct Options {
    config_file: Option<String>,
    print_config: bool,
}


impl Options {
    pub fn parse_args<I>(
        mut arg_iter: I
    ) -> Result<Options, Option<String>>
        where I: Iterator<Item = String>
    {
        let mut options = Options::default();

        loop {
            if let Some(arg) = arg_iter.next(){
                match arg.as_str(){
                    "-c" | "--config-file" => {
                        if let Some(path) = arg_iter.next(){
                            options.config_file = Some(path);
                        } else {
                            return Err(
                                Some("No config file path given".to_string())
                            );
                        }
                    },
                    "-p" | "--print-config" => {
                        options.print_config = true;
                    },
                    "-h" | "--help" => {
                        return Err(None);
                    },
                    _ => {
                        return Err(Some("Unrecognized argument".to_string()));
                    }
                }
            } else {
                break;
            }
        }

        Ok(options)
    }
}


pub fn run_app_with_cli_and_config<T>(
    app_title: &str,
    // Function that takes config file and runs application
    app_fn: fn(T) -> anyhow::Result<()>,
    opts: Option<Options>,
) where T: Config {
    ::std::process::exit(match run_inner(app_title, app_fn, opts) {
        Ok(()) => 0,
        Err(err) => {
            eprintln!("Error: {:#}", err);

            1
        },
    })
}


fn run_inner<T>(
    app_title: &str,
    // Function that takes config file and runs application
    app_fn: fn(T) -> anyhow::Result<()>,
    // Possibly preparsed options
    options: Option<Options>,
) -> anyhow::Result<()> where T: Config {
    let options = if let Some(options) = options {
        options
    } else {
        let mut arg_iter = ::std::env::args();

        let app_path = arg_iter.next().unwrap();

        match Options::parse_args(arg_iter){
            Ok(options) => options,
            Err(opt_err) => {
                let gen_info = || format!(
                    "{}\n\nUsage: {} [OPTIONS]",
                    app_title,
                    app_path
                );

                print_help(gen_info, opt_err);

                return Ok(())
            }
        }
    };

    if options.print_config {
        print!("{}", default_config_as_toml::<T>());

        Ok(())
    } else {
        let config = if let Some(path) = options.config_file {
            config_from_toml_file(path)?
        } else {
            T::default()
        };

        if let Some(log_level) = config.get_log_level(){
            start_logger(log_level)?;
        }

        app_fn(config)
    }
}


pub fn print_help<F>(
    info_generator: F,
    opt_error: Option<String>
) where F: FnOnce() -> String {
    println!("{}", info_generator());

    println!("\nOptions:");
    println!("    -c, --config-file     Load config from this path");
    println!("    -p, --print-config    Print default config");
    println!("    -h, --help            Print this help message");

    if let Some(error) = opt_error {
        println!("\nError: {}.", error);
    }
}


fn config_from_toml_file<T>(path: String) -> anyhow::Result<T>
    where T: DeserializeOwned
{
    let mut file = File::open(path.clone()).with_context(||
        format!("Couldn't open config file {}", path.clone())
    )?;

    let mut data = String::new();

    file.read_to_string(&mut data).with_context(||
        format!("Couldn't read config file {}", path.clone())
    )?;

    toml::from_str(&data).with_context(||
        format!("Couldn't parse config file {}", path.clone())
    )
}


fn default_config_as_toml<T>() -> String
    where T: Default + Serialize
{
    toml::to_string_pretty(&T::default())
        .expect("Could not serialize default config to toml")
}


fn start_logger(log_level: LogLevel) -> ::anyhow::Result<()> {
    let level_filter = match log_level{
        LogLevel::Off => LevelFilter::Off,
        LogLevel::Error => LevelFilter::Error,
        LogLevel::Warn => LevelFilter::Warn,
        LogLevel::Info => LevelFilter::Info,
        LogLevel::Debug => LevelFilter::Debug,
        LogLevel::Trace => LevelFilter::Trace,
    };

    // Note: logger doesn't seem to pick up thread names. Not a huge loss.
    let simplelog_config = ConfigBuilder::new()
        .set_time_to_local(true)
        .set_location_level(LevelFilter::Off)
        .build();

    TermLogger::init(
        level_filter,
        simplelog_config,
        TerminalMode::Stderr
    ).context("Couldn't initialize logger")?;

    Ok(())
}