Async file read

This commit is contained in:
xmppftw 2024-08-07 11:48:55 +02:00 committed by Maxime “pep” Buquet
parent 66e4131bd3
commit 6285c0b1a4
3 changed files with 7 additions and 8 deletions

View file

@ -16,7 +16,7 @@ http-body-util = "0.1"
bytes = "1.6"
jid = { version = "0.10", features = [ "serde" ] }
log = "0.4"
tokio = { version = "1", default-features = false, features = [ "rt", "net", "sync" ] }
tokio = { version = "1", default-features = false, features = [ "rt", "net", "sync", "fs" ] }
pretty_env_logger = "0.5"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"

View file

@ -57,18 +57,17 @@ impl Config {
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000)
}
pub fn from_file(file: Utf8PathBuf) -> Result<Config, Error> {
if file.try_exists().is_err() {
pub async fn from_file(file: Utf8PathBuf) -> Result<Config, Error> {
if tokio::fs::try_exists(&file).await.is_err() {
let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file));
return Err(Error::Io(err));
}
// TODO: tokio::fs
let buf = std::fs::read_to_string(file)?;
let buf = tokio::fs::read_to_string(file).await?;
Ok(toml::from_str(&buf)?)
}
pub fn from_arg(file: Option<&Utf8PathBuf>) -> Result<Config, Error> {
pub async fn from_arg(file: Option<&Utf8PathBuf>) -> Result<Config, Error> {
let path = if let Some(path) = file {
// Provided by --config flag
path.canonicalize_utf8()?
@ -86,6 +85,6 @@ impl Config {
debug!("Using configuration file: {:?}", path);
Self::from_file(path)
Self::from_file(path).await
}
}

View file

@ -50,7 +50,7 @@ async fn main() -> Result<!, Error> {
)
.get_matches();
let config = Config::from_arg(matches.get_one::<Utf8PathBuf>("config"))?;
let config = Config::from_arg(matches.get_one::<Utf8PathBuf>("config")).await?;
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<Hook>();