refactor: unify error handling with anyhow and add From conversions

Phase 5: Replace all Box<dyn Error> return types with anyhow::Result<T>
throughout the codebase. Replace string-based Err("msg".into()) and
format!().into() patterns with bail!() and anyhow!() macros. Fix
dirs::home_dir().unwrap() in settings.rs to use a fallback path instead
of panicking when HOME is unset. Remove stray use std::error::Error
imports no longer needed.

Phase 6: Add From<&User> for CacheUser in models/user.rs and
From<&Laboratory>/From<&Laboratories> for CacheLaboratory/CacheLabsWrapper
in models/laboratory.rs. Simplify commands/login.rs to use .into()
conversions, removing the redundant to_cache_user() and to_cache_labs()
helper functions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-04-20 14:19:10 +09:00
co-authored by Copilot
parent e8fd359f54
commit 769a5a68e2
30 changed files with 889 additions and 872 deletions
+42
View File
@@ -0,0 +1,42 @@
use serde::Deserialize;
/// Access and refresh token pair stored in the login cache file.
#[derive(Deserialize, Clone)]
pub struct CacheToken {
pub access: String,
pub refresh: String,
}
/// Minimal user fields stored in the cache, matching Python's `User` dataclass.
#[derive(Deserialize, Clone)]
pub struct CacheUser {
pub id: u32,
pub username: String,
pub laboratory_ids: Vec<u32>,
pub is_reviewer: bool,
}
/// All four laboratory fields needed for digest computation.
#[derive(Deserialize, Clone)]
pub struct CacheLaboratory {
pub id: u32,
pub name: String,
#[serde(default)]
pub pi_name: String,
#[serde(default)]
pub full_name: String,
}
/// Wrapper matching Python's `Laboratories` serialization: `{"items": [...]}`.
#[derive(Deserialize, Clone, Default)]
pub struct CacheLabsWrapper {
pub items: Vec<CacheLaboratory>,
}
/// Full login cache, corresponding to the `<remote>.json` file written by `login`.
#[derive(Deserialize, Clone)]
pub struct Cache {
pub user: Option<CacheUser>,
pub token: CacheToken,
pub laboratories: CacheLabsWrapper,
}