2023-07-21 12:43:39 +00:00
|
|
|
use std::{fs, io, path, pin};
|
2023-06-14 18:22:10 +00:00
|
|
|
|
|
|
|
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
|
|
|
|
match fs::File::open(path) {
|
|
|
|
Ok(file) => Ok(Some(file)),
|
|
|
|
Err(err) => match err.kind() {
|
|
|
|
io::ErrorKind::NotFound => Ok(None),
|
|
|
|
_ => Err(err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 18:56:14 +00:00
|
|
|
|
2023-08-21 17:00:32 +00:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum ParseFileError<ParseError> {
|
|
|
|
#[error("io error: {0}")]
|
|
|
|
IO(io::Error),
|
|
|
|
|
|
|
|
#[error("parse error")]
|
|
|
|
FromStr(ParseError),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_file<T: std::str::FromStr>(
|
|
|
|
path: &path::Path,
|
|
|
|
) -> Result<Option<T>, ParseFileError<T::Err>> {
|
|
|
|
match fs::read_to_string(path) {
|
2024-01-10 09:42:48 +00:00
|
|
|
Ok(s) => Ok(Some(s.parse().map_err(ParseFileError::FromStr)?)),
|
2023-08-21 17:00:32 +00:00
|
|
|
Err(err) => match err.kind() {
|
|
|
|
io::ErrorKind::NotFound => Ok(None),
|
|
|
|
_ => Err(ParseFileError::IO(err)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:46:54 +00:00
|
|
|
pub type BoxByteStream = futures::stream::BoxStream<'static, io::Result<bytes::Bytes>>;
|
2023-07-08 13:19:31 +00:00
|
|
|
|
|
|
|
pub type BoxFuture<'a, O> = pin::Pin<Box<dyn futures::Future<Output = O> + Send + 'a>>;
|