1use std::path::Path;
2
3use crate::pre::*;
4
5#[inline(always)]
7pub fn read(path: impl AsRef<Path>) -> crate::Result<Vec<u8>> {
8 read_impl(path.as_ref())
9}
10fn read_impl(path: &Path) -> crate::Result<Vec<u8>> {
11 crate::trace!("read '{}'", path.display());
12 crate::check!(
13 std::fs::read(path),
14 "failed to read file '{}' as bytes",
15 path.display()
16 )
17}
18
19#[cfg(feature = "coroutine")]
21#[inline(always)]
22pub async fn co_read(path: impl AsRef<Path>) -> crate::Result<Vec<u8>> {
23 co_read_impl(path.as_ref()).await
24}
25#[cfg(feature = "coroutine")]
26async fn co_read_impl(path: &Path) -> crate::Result<Vec<u8>> {
27 crate::trace!("co_read '{}'", path.display());
28 crate::check!(
29 tokio::fs::read(path).await,
30 "failed to read file '{}' as bytes",
31 path.display()
32 )
33}
34
35#[inline(always)]
37pub fn read_string(path: impl AsRef<Path>) -> crate::Result<String> {
38 read_string_impl(path.as_ref())
39}
40fn read_string_impl(path: &Path) -> crate::Result<String> {
41 crate::trace!("read_string '{}'", path.display());
42 crate::check!(
43 std::fs::read_to_string(path),
44 "failed to read file '{}' as string",
45 path.display()
46 )
47}
48
49#[cfg(feature = "coroutine")]
51#[inline(always)]
52pub async fn co_read_string(path: impl AsRef<Path>) -> crate::Result<String> {
53 co_read_string_impl(path.as_ref()).await
54}
55#[cfg(feature = "coroutine")]
56async fn co_read_string_impl(path: &Path) -> crate::Result<String> {
57 crate::trace!("co_read_string '{}'", path.display());
58 crate::check!(
59 tokio::fs::read_to_string(path).await,
60 "failed to read file '{}' as string",
61 path.display()
62 )
63}
64
65pub type Reader = std::io::BufReader<std::fs::File>;
67
68#[inline(always)]
73pub fn reader(path: impl AsRef<Path>) -> crate::Result<Reader> {
74 reader_impl(path.as_ref())
75}
76fn reader_impl(path: &Path) -> crate::Result<Reader> {
77 crate::trace!("reader '{}'", path.display());
78 let file = crate::check!(
79 std::fs::File::open(path),
80 "failed to open file '{}'",
81 path.display()
82 )?;
83 Ok(std::io::BufReader::new(file))
84}
85
86#[cfg(feature = "coroutine")]
88pub type CoReader = tokio::io::BufReader<tokio::fs::File>;
89
90#[cfg(feature = "coroutine")]
95#[inline(always)]
96pub async fn co_reader(path: impl AsRef<Path>) -> crate::Result<CoReader> {
97 co_reader_impl(path.as_ref()).await
98}
99#[cfg(feature = "coroutine")]
100async fn co_reader_impl(path: &Path) -> crate::Result<CoReader> {
101 crate::trace!("co_reader '{}'", path.display());
102 let file = crate::check!(
103 tokio::fs::File::open(path).await,
104 "failed to open file '{}'",
105 path.display()
106 )?;
107 Ok(tokio::io::BufReader::new(file))
108}