-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_from_anyhow.rs
More file actions
42 lines (37 loc) · 1.39 KB
/
Copy pathmigrate_from_anyhow.rs
File metadata and controls
42 lines (37 loc) · 1.39 KB
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
//! Migration from anyhow to masterror - same ergonomics, better features.
use std::fs;
use masterror::{AppError, AppResult, ResultExt, ensure, fail, field};
fn main() {
println!("Migration from anyhow to masterror");
println!();
println!("Replace: anyhow::Result -> masterror::AppResult");
println!("Replace: bail!() -> fail!()");
println!("Same: ensure!() works identically");
println!("Same: .context() works identically");
println!("Plus: Structured metadata with field::*");
println!();
match read_config("/tmp/config.toml") {
Ok(content) => println!("Config: {content}"),
Err(e) => println!("Error: {e}")
}
}
fn read_config(path: &str) -> AppResult<String> {
// .context() works exactly like anyhow
let content = fs::read_to_string(path).context("Failed to read config file")?;
ensure!(
!content.is_empty(),
AppError::bad_request("Config file is empty")
.with_field(field::str("path", path.to_string()))
);
if content.starts_with("invalid") {
fail!(
AppError::bad_request("Invalid config format")
.with_field(field::str("path", path.to_string()))
.with_field(field::u64("size", content.len() as u64))
);
}
Ok(content)
}