refact: removed figment dependency

This commit is contained in:
meeg_leeto 2022-04-15 01:23:55 +01:00
parent 7220663240
commit b53646cb64
3 changed files with 40 additions and 88 deletions

81
Cargo.lock generated
View File

@ -57,15 +57,6 @@ dependencies = [
"syn",
]
[[package]]
name = "atomic"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c"
dependencies = [
"autocfg",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -250,20 +241,6 @@ dependencies = [
"instant",
]
[[package]]
name = "figment"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790b4292c72618abbab50f787a477014fe15634f96291de45672ce46afe122df"
dependencies = [
"atomic",
"pear",
"serde",
"serde_json",
"uncased",
"version_check",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -490,12 +467,6 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "inlinable_string"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb"
[[package]]
name = "instant"
version = "0.1.12"
@ -581,7 +552,6 @@ version = "0.1.0"
dependencies = [
"argh",
"base64",
"figment",
"redis",
"serde",
"serde_json",
@ -769,29 +739,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "pear"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15e44241c5e4c868e3eaa78b7c1848cadd6344ed4f54d029832d32b415a58702"
dependencies = [
"inlinable_string",
"pear_codegen",
"yansi",
]
[[package]]
name = "pear_codegen"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82a5ca643c2303ecb740d506539deba189e16f2754040a42901cd8105d0282d0"
dependencies = [
"proc-macro2",
"proc-macro2-diagnostics",
"quote",
"syn",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -865,19 +812,6 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "proc-macro2-diagnostics"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf29726d67464d49fa6224a1d07936a8c08bb3fba727c7493f6cf1616fdaada"
dependencies = [
"proc-macro2",
"quote",
"syn",
"version_check",
"yansi",
]
[[package]]
name = "quick-error"
version = "1.2.3"
@ -1363,15 +1297,6 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "uncased"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baeed7327e25054889b9bd4f975f32e5f4c5d434042d59ab6cd4142c0a76ed0"
dependencies = [
"version_check",
]
[[package]]
name = "unicase"
version = "2.6.0"
@ -1593,9 +1518,3 @@ name = "windows_x86_64_msvc"
version = "0.32.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "504a2476202769977a040c6364301a3f65d0cc9e3fb08600b2bda150a0488316"
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View File

@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
argh = "0.1.7"
base64 = "0.13.0"
figment = { version = "0.10.6", features = ["json", "env"] }
redis = { version = "~0.21.5", features = ["tokio-comp"] }
serde = { version = "~1.0.136", features = ["derive"] }
serde_json = "1.0.79"

View File

@ -1,5 +1,5 @@
use argh::FromArgs;
use figment::{providers::Format, Figment};
use core::panic;
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeSet, io::BufRead, net::IpAddr, path::PathBuf, str::FromStr, sync::Arc,
@ -257,11 +257,45 @@ fn shorten(
#[tokio::main]
async fn serve() {
// Read configuration
let config_file = std::env::var("LONK_CONFIG").unwrap_or("lonk.json".to_string());
let config: Config = Figment::new()
.merge(figment::providers::Json::file(&config_file))
.extract()
.expect("Could not parse configuration file.");
let config: Config = {
let config_file_name = std::env::var("LONK_CONFIG").unwrap_or("lonk.json".to_string());
let config_file = std::fs::File::open(config_file_name.clone()).unwrap_or_else(|err| {
match err.kind() {
std::io::ErrorKind::NotFound => {
panic!("Configuration file {} does not exist.", config_file_name)
}
std::io::ErrorKind::PermissionDenied => {
panic!("Read permission to {} was denied.", config_file_name)
}
_ => panic!(
"Error when trying to read configuration file {}: {}",
config_file_name, err
),
};
});
let config_buf = std::io::BufReader::new(config_file);
serde_json::from_reader(config_buf).unwrap_or_else(|err| match err.classify() {
serde_json::error::Category::Io => panic!("IO error when reading configuration file."),
serde_json::error::Category::Syntax => panic!(
"Configuration file is syntactically incorrect.
See {}:line {}, column {}.",
&config_file_name,
err.line(),
err.column()
),
serde_json::error::Category::Data => panic!(
"Error deserializing configuration file; expected different data type.
See {}:line {}, column {}.",
&config_file_name,
err.line(),
err.column()
),
serde_json::error::Category::Eof => {
panic!("Unexpected end of file when reading configuration file.")
}
})
};
// Create slug factory
let slug_factory = Arc::new(SlugFactory::from_rules(config.slug_rules));