summaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-11 23:43:55 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-11 23:57:02 -0500
commitb26d3c09eaec958c75931cc126367d4124cbffad (patch)
tree04eb1039b1f4646d84356a3b1779b24701609417 /src/config.rs
parenta20f3667a88affa0498e564cea17e9e795162bb8 (diff)
refactor: improve error handling and layout
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/config.rs b/src/config.rs
index 09ec997..412c5c1 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,7 +1,6 @@
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
-use unnamed_server::Error;
#[derive(Debug, Clone)]
pub struct Config {
@@ -24,7 +23,7 @@ pub struct ConfigBuilder {
}
impl ConfigBuilder {
- pub fn file(self) -> Result<Self, Error> {
+ pub fn file(self) -> Result<Self, toml::de::Error> {
let file = std::env::args()
.nth(1)
.unwrap_or("/etc/unnamed_server.toml".to_string());
@@ -57,18 +56,16 @@ impl ConfigBuilder {
}
}
- pub fn build(self) -> Result<Config, Error> {
+ pub fn build(self) -> Result<Config, String> {
+ let err = |s: &str| format!("Config error: missing value: {s}");
+
Ok(Config {
listen_addr: self
.listen_addr
.and_then(|s| s.parse().ok())
- .ok_or_else(|| Error::Config("listen_addr".to_string()))?,
- jwt_secret: self
- .jwt_secret
- .ok_or_else(|| Error::Config("jwt_secret".to_string()))?,
- database_url: self
- .database_url
- .ok_or_else(|| Error::Config("database_url".to_string()))?,
+ .ok_or_else(|| err("listen_addr"))?,
+ jwt_secret: self.jwt_secret.ok_or_else(|| err("jwt_secret"))?,
+ database_url: self.database_url.ok_or_else(|| err("database_url"))?,
})
}
}