Skip to content

Commit 8c1a37a

Browse files
committed
feat: better toml error messages
1 parent 6d5c66f commit 8c1a37a

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ serde = { version = "1.0.228", features = ["derive"] }
1717
ping = "0.7.1-beta.1"
1818
chrono = "0.4.43"
1919
json = "0.12.4"
20+
eyre = "0.6.12"

src/app/config.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::connection::{Connection, json::JsonConn};
2+
use eyre::Context;
23
use serde::Deserialize;
34
use std::{io::Read, path::PathBuf};
45

@@ -8,38 +9,26 @@ struct ConfigFile {
89
json: Vec<JsonConn>,
910
}
1011

11-
pub fn read_config() -> Vec<Connection> {
12-
let home = std::env::var("HOME").unwrap();
12+
pub fn read_config() -> eyre::Result<Vec<Connection>> {
13+
let home = std::env::var("HOM").wrap_err("Failed to read HOME environment variable")?;
1314

1415
// Full path to the toml config file.
1516
let toml_path = PathBuf::from(home)
1617
.join(".config")
1718
.join("lantern")
1819
.join("config.toml");
1920

20-
let Ok(mut f) = std::fs::File::open(&toml_path) else {
21-
eprintln!(
22-
"ERROR: failed to read configuration file: {}",
23-
toml_path.display()
24-
);
25-
std::process::exit(1);
26-
};
21+
let mut f = std::fs::File::open(&toml_path)?;
2722

2823
let mut buf = String::new();
2924

30-
if f.read_to_string(&mut buf).is_err() {
31-
eprintln!(
32-
"ERROR: failed to read configuration file: {}",
33-
toml_path.display()
34-
);
35-
std::process::exit(1);
36-
}
25+
f.read_to_string(&mut buf)?;
3726

38-
let c: ConfigFile = toml::from_str(&buf).unwrap();
27+
let c: ConfigFile = toml::from_str(&buf)?;
3928

40-
[
29+
Ok([
4130
c.connection,
4231
c.json.into_iter().map(std::convert::Into::into).collect(),
4332
]
44-
.concat()
33+
.concat())
4534
}

src/app/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ pub struct App {
3434
}
3535

3636
impl App {
37-
pub fn generate() -> Self {
37+
pub fn generate() -> eyre::Result<Self> {
3838
let matches: clap::ArgMatches = generate_matches();
3939

40-
let conns = config::read_config();
40+
let conns = config::read_config()?;
4141

4242
let output_fmt = match matches.get_one::<OutputFmt>("output_fmt") {
4343
Some(&fmt) => fmt,
@@ -46,12 +46,12 @@ impl App {
4646

4747
let interval = matches.get_one::<u32>("interval").unwrap_or(&15);
4848

49-
Self {
49+
Ok(Self {
5050
connections: Arc::new(Mutex::new(conns)),
5151
output_fmt,
5252
interval: *interval,
5353
..Default::default()
54-
}
54+
})
5555
}
5656

5757
pub fn next_tab(&mut self) {

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use std::{
2525
mod app;
2626
mod ui;
2727

28-
fn main() -> io::Result<()> {
29-
let mut app = App::generate();
28+
fn main() -> eyre::Result<()> {
29+
let mut app = App::generate()?;
3030

3131
// Set up terminal.
3232
enable_raw_mode()?;

0 commit comments

Comments
 (0)