Async file read

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

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
}
}