From 66e4131bd3fe628c8bb068c0d5bbfc078ce93b57 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Wed, 7 Aug 2024 11:35:47 +0200 Subject: [PATCH] Use Utf8PathBuf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 1 + src/config.rs | 18 +++++++----------- src/main.rs | 6 +++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c041a5c..ccf7b0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ xmpp = "0.5" hmac = "0.12" sha2 = "0.10" hex = "0.4" +camino = { version = "1.1", features = ["serde"] } [patch.crates-io] forgejo-hooks = { path = "forgejo-hooks" } diff --git a/src/config.rs b/src/config.rs index 99114e5..ccb8c7e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,8 +15,8 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::net::{IpAddr, Ipv6Addr, SocketAddr}; -use std::path::{Path, PathBuf}; +use camino::{Utf8Path, Utf8PathBuf}; use jid::BareJid; use log::debug; use serde::{Deserialize, Serialize}; @@ -57,7 +57,7 @@ impl Config { SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000) } - pub fn from_file(file: PathBuf) -> Result { + pub fn from_file(file: Utf8PathBuf) -> Result { if file.try_exists().is_err() { let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file)); return Err(Error::Io(err)); @@ -68,20 +68,16 @@ impl Config { Ok(toml::from_str(&buf)?) } - pub fn from_arg(file: Option<&PathBuf>) -> Result { + pub fn from_arg(file: Option<&Utf8PathBuf>) -> Result { let path = if let Some(path) = file { // Provided by --config flag - if !path.starts_with("/") { - std::env::current_dir()?.join(path) - } else { - path.to_path_buf() - } + path.canonicalize_utf8()? } else { - let confdir: PathBuf = match std::env::var("XDG_CONFIG_HOME") { - Ok(ref dir) => Path::new(dir).to_path_buf(), + let confdir: Utf8PathBuf = match std::env::var("XDG_CONFIG_HOME") { + Ok(ref dir) => Utf8Path::new(dir).to_path_buf(), Err(_) => { let home = std::env::var("HOME")?; - Path::new(home.as_str()).join(".config") + Utf8Path::new(home.as_str()).join(".config") } }; diff --git a/src/main.rs b/src/main.rs index 3075efe..cbc261d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,9 +28,9 @@ use crate::error::Error; use crate::hook::Hook; use crate::web::hooks; -use std::path::PathBuf; use std::sync::{Arc, Mutex}; +use camino::Utf8PathBuf; use clap::{command, value_parser, Arg}; use hyper::{server::conn::http1, service::service_fn}; use hyper_util::rt::tokio::{TokioIo, TokioTimer}; @@ -46,11 +46,11 @@ async fn main() -> Result { .short('c') .long("config") .required(false) - .value_parser(value_parser!(PathBuf)), + .value_parser(value_parser!(Utf8PathBuf)), ) .get_matches(); - let config = Config::from_arg(matches.get_one::("config"))?; + let config = Config::from_arg(matches.get_one::("config"))?; let (value_tx, mut value_rx) = mpsc::unbounded_channel::();