Async file read
This commit is contained in:
parent
66e4131bd3
commit
6285c0b1a4
3 changed files with 7 additions and 8 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>();
|
||||
|
||||
|
|
Loading…
Reference in a new issue