#![doc(
html_logo_url = "https://raw.githubusercontent.com/bgpkit/assets/main/logos/icon-transparent.png",
html_favicon_url = "https://raw.githubusercontent.com/bgpkit/assets/main/logos/favicon.ico"
)]
mod builder;
mod client;
mod compression;
mod error;
mod progress;
pub use builder::OneIoBuilder;
pub use client::OneIo;
pub use error::OneIoError;
#[cfg(feature = "async")]
pub mod async_reader;
#[cfg(feature = "rustls")]
pub mod crypto;
#[cfg(feature = "digest")]
pub mod digest;
#[cfg(any(feature = "http", feature = "ftp"))]
pub(crate) mod remote;
#[cfg(feature = "s3")]
pub mod s3;
#[cfg(feature = "s3")]
pub use s3::*;
#[cfg(feature = "digest")]
pub use digest::*;
use std::fs::File;
use std::io::{BufWriter, Read, Write};
pub(crate) fn get_protocol(path: &str) -> Option<&str> {
path.split_once("://").map(|(protocol, _)| protocol)
}
pub(crate) fn file_extension(path: &str) -> &str {
let path = path.split('?').next().unwrap_or(path);
let path = path.split('#').next().unwrap_or(path);
path.rsplit('.').next().unwrap_or("")
}
pub(crate) fn get_writer_raw_impl(path: &str) -> Result<BufWriter<File>, OneIoError> {
let path = std::path::Path::new(path);
if let Some(prefix) = path.parent() {
std::fs::create_dir_all(prefix)?;
}
let output_file = BufWriter::new(File::create(path)?);
Ok(output_file)
}
#[allow(dead_code)]
pub(crate) fn get_reader_raw_impl(path: &str) -> Result<Box<dyn Read + Send>, OneIoError> {
let file = File::open(path)?;
Ok(Box::new(std::io::BufReader::new(file)))
}
pub fn get_reader(path: &str) -> Result<Box<dyn Read + Send>, OneIoError> {
builder::default_oneio()?.get_reader(path)
}
pub fn get_writer(path: &str) -> Result<Box<dyn Write>, OneIoError> {
builder::default_oneio()?.get_writer(path)
}
pub fn exists(path: &str) -> Result<bool, OneIoError> {
builder::default_oneio()?.exists(path)
}
pub fn read_to_string(path: &str) -> Result<String, OneIoError> {
builder::default_oneio()?.read_to_string(path)
}
#[cfg(feature = "json")]
pub fn read_json_struct<T: serde::de::DeserializeOwned>(path: &str) -> Result<T, OneIoError> {
builder::default_oneio()?.read_json_struct(path)
}
pub fn read_lines(
path: &str,
) -> Result<std::io::Lines<std::io::BufReader<Box<dyn Read + Send>>>, OneIoError> {
builder::default_oneio()?.read_lines(path)
}
pub fn download(remote: &str, local: &str) -> Result<(), OneIoError> {
builder::default_oneio()?.download(remote, local)
}
pub fn get_cache_reader(
path: &str,
cache_dir: &str,
cache_file_name: Option<String>,
force_cache: bool,
) -> Result<Box<dyn Read + Send>, OneIoError> {
builder::default_oneio()?.get_cache_reader(path, cache_dir, cache_file_name, force_cache)
}
#[cfg(feature = "async")]
pub async fn get_reader_async(
path: &str,
) -> Result<Box<dyn tokio::io::AsyncRead + Send + Unpin>, OneIoError> {
async_reader::get_reader_async(path).await
}
#[cfg(feature = "async")]
pub async fn read_to_string_async(path: &str) -> Result<String, OneIoError> {
async_reader::read_to_string_async(path).await
}
#[cfg(feature = "async")]
pub async fn download_async(url: &str, path: &str) -> Result<(), OneIoError> {
async_reader::download_async(url, path).await
}