diff options
author | tilpner | 2023-10-15 17:22:07 +0200 |
---|---|---|
committer | tilpner | 2023-10-15 17:22:07 +0200 |
commit | 61083d614962dfeb69fdf767c5b0466b6efd413c (patch) | |
tree | 41643de13ad83da600f1a4aa814f04756a63d23f /src | |
parent | 0494c6341a9a34323bd68323a59c2152d4cc4a17 (diff) | |
download | rpb-s3-61083d614962dfeb69fdf767c5b0466b6efd413c.tar.gz rpb-s3-61083d614962dfeb69fdf767c5b0466b6efd413c.tar.xz rpb-s3-61083d614962dfeb69fdf767c5b0466b6efd413c.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs index 790feb2..d2a09ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use std::{ path::PathBuf }; -use config::Config; +use config::{Config, ConfigBuilder}; use structopt::StructOpt; use mime_guess::{ Mime, MimeGuess }; @@ -66,7 +66,7 @@ struct Opt { fn main() { let cfg = { let opt = Opt::from_args(); - let mut cfg = Config::default(); + let mut cfg = ConfigBuilder::<config::builder::DefaultState>::default(); let xdg = xdg::BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); let xdg_file = xdg.find_config_file("config.json"); @@ -75,11 +75,13 @@ fn main() { .or(xdg_file); if let Some(cfg_file) = cfg_file { let cfg_file = cfg_file.to_str().expect("Config file has invalid path"); - cfg.merge(config::File::new(cfg_file, config::FileFormat::Json)) - .expect("Failed to read settings from config file"); + cfg = cfg.add_source(config::File::new(cfg_file, config::FileFormat::Json)); } - cfg.merge(config::Environment::with_prefix("PB")).expect("Failed to read config from environment"); - cfg.merge(Config::try_from(&opt).unwrap()).expect("Failed to read config from CLI arguments"); + let mut cfg = cfg + .add_source(config::Environment::with_prefix("PB")) + .add_source(Config::try_from(&opt).unwrap()) + .build() + .unwrap(); if cfg.get_bool("private").unwrap_or(false) { cfg.set("id_length", 10).unwrap(); @@ -94,15 +96,15 @@ async fn async_main(cfg: Config) { let region = Region::Custom { endpoint: { // trailing / causes problems with rusoto - let mut s = cfg.get_str("endpoint").expect("No endpoint set"); + let mut s = cfg.get_string("endpoint").expect("No endpoint set"); if s.ends_with('/') { s.pop(); } s }, - name: cfg.get_str("region").expect("No region set") + name: cfg.get_string("region").expect("No region set") }; - let path = cfg.get_str("file"); - let lang = cfg.get_str("language").ok(); + let path = cfg.get_string("file"); + let lang = cfg.get_string("language").ok(); let code = cfg.get_bool("code").unwrap() || lang.is_some(); @@ -121,12 +123,12 @@ async fn async_main(cfg: Config) { let dispatcher = request::HttpClient::new().expect("Unable to create rusoto http client"); let credentials = StaticProvider::new_minimal( - cfg.get_str("access_key_id").expect("Access key not set"), - cfg.get_str("secret_access_key").expect("Secret access key not set")); + cfg.get_string("access_key_id").expect("Access key not set"), + cfg.get_string("secret_access_key").expect("Secret access key not set")); let client = S3Client::new_with(dispatcher, credentials, region); - let bucket = cfg.get_str("bucket").expect("Bucket not set"); - let name = cfg.get_str("name").ok() + let bucket = cfg.get_string("bucket").expect("Bucket not set"); + let name = cfg.get_string("name").ok() .unwrap_or_else(|| { let len = cfg.get_int("id_length").unwrap() as u32; (0..).map(|_| random_id(len)) @@ -135,7 +137,7 @@ async fn async_main(cfg: Config) { }); let mime = if code { String::from("text/html; charset=utf-8") } else { - cfg.get_str("mime").ok() + cfg.get_string("mime").ok() .or_else(|| path.ok() .and_then(|path| MimeGuess::from_path(path).first()) .as_ref() @@ -176,10 +178,10 @@ fn highlight(cfg: &Config, mut input: Box<dyn BufRead + Send>) -> impl Iterator< .unwrap()); let syntax_set = syntax_set.build(); - let syntax = - if let Ok(lang) = cfg.get_str("language") { + let syntax = + if let Ok(lang) = cfg.get_string("language") { syntax_set.find_syntax_by_token(&lang).expect("Failed to load syntax") - } else if let Ok(path) = cfg.get_str("file") { + } else if let Ok(path) = cfg.get_string("file") { syntax_set.find_syntax_for_file(path) .unwrap_or_default().expect("Failed to load syntax from path") } else { @@ -204,7 +206,7 @@ fn highlight(cfg: &Config, mut input: Box<dyn BufRead + Send>) -> impl Iterator< for (i, line) in LinesWithEndings::from(&input_str).enumerate() { let regions = highlighter.highlight(line, &syntax_set); formatted_line.clear(); - append_highlighted_html_for_styled_line(®ions[..], IncludeBackground::IfDifferent(bg), &mut formatted_line); + append_highlighted_html_for_styled_line(®ions[..], IncludeBackground::IfDifferent(bg), &mut formatted_line).unwrap(); if formatted_line.ends_with('\n') { formatted_line.pop(); } let _ = write!(&mut html, "<a id=\"L{}\"></a>{}", i, formatted_line); } @@ -215,7 +217,7 @@ fn highlight(cfg: &Config, mut input: Box<dyn BufRead + Send>) -> impl Iterator< template.add_formatter("raw", tinytemplate::format_unescaped); let rendered = template.render("code", &Template { - title: cfg.get_str("name").unwrap_or_else(|_| String::new()), + title: cfg.get_string("name").unwrap_or_else(|_| String::new()), code: String::from_utf8(html).expect("Invalid UTF8") }).unwrap(); |