Replace more unwrap() with ? operator

This commit is contained in:
sxyazi 2024-03-31 21:36:58 +08:00
parent 82a3a57671
commit f32d830316
No known key found for this signature in database

View file

@ -1,6 +1,6 @@
use std::{mem, path::{Path, PathBuf}}; use std::{mem, path::{Path, PathBuf}};
use anyhow::Context; use anyhow::{anyhow, Context, Result};
use toml::{Table, Value}; use toml::{Table, Value};
use crate::theme::Flavor; use crate::theme::Flavor;
@ -8,15 +8,15 @@ use crate::theme::Flavor;
pub(crate) struct Preset; pub(crate) struct Preset;
impl Preset { impl Preset {
pub(crate) fn yazi(p: &Path) -> anyhow::Result<String> { pub(crate) fn yazi(p: &Path) -> Result<String> {
Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml")) Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml"))
} }
pub(crate) fn keymap(p: &Path) -> anyhow::Result<String> { pub(crate) fn keymap(p: &Path) -> Result<String> {
Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml")) Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml"))
} }
pub(crate) fn theme(p: &Path) -> anyhow::Result<String> { pub(crate) fn theme(p: &Path) -> Result<String> {
let Ok(user) = std::fs::read_to_string(p.join("theme.toml")) else { let Ok(user) = std::fs::read_to_string(p.join("theme.toml")) else {
return Ok(include_str!("../preset/theme.toml").to_owned()); return Ok(include_str!("../preset/theme.toml").to_owned());
}; };
@ -24,10 +24,9 @@ impl Preset {
return Self::merge_str(&user, include_str!("../preset/theme.toml")); return Self::merge_str(&user, include_str!("../preset/theme.toml"));
}; };
let p = p.join(format!("flavors/{}.yazi/flavor.toml", use_)); let p = p.join(format!("flavors/{use_}.yazi/flavor.toml"));
let flavor = std::fs::read_to_string(&p) let flavor =
.with_context(|| format!("Failed to load flavor {:?}", p)) std::fs::read_to_string(&p).with_context(|| anyhow!("Failed to load flavor {p:?}"))?;
.unwrap();
Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml"))?) Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml"))?)
} }
@ -38,21 +37,21 @@ impl Preset {
} }
#[inline] #[inline]
pub(crate) fn merge_str(user: &str, base: &str) -> anyhow::Result<String> { pub(crate) fn merge_str(user: &str, base: &str) -> Result<String> {
let mut t = user.parse()?; let mut t = user.parse()?;
Self::merge(&mut t, base.parse().unwrap(), 2); Self::merge(&mut t, base.parse()?, 2);
Ok(t.to_string()) Ok(t.to_string())
} }
#[inline] #[inline]
fn merge_path(user: PathBuf, base: &str) -> anyhow::Result<String> { fn merge_path(user: PathBuf, base: &str) -> Result<String> {
let s = std::fs::read_to_string(&user).unwrap_or_default(); let s = std::fs::read_to_string(&user).unwrap_or_default();
if s.is_empty() { if s.is_empty() {
return Ok(base.to_string()); return Ok(base.to_string());
} }
Ok(Self::merge_str(&s, base).context(format!("Loading {user:?}"))?) Self::merge_str(&s, base).with_context(|| anyhow!("Loading {user:?}"))
} }
fn merge(a: &mut Table, b: Table, max: u8) { fn merge(a: &mut Table, b: Table, max: u8) {