Skip to content

Commit f022f88

Browse files
committed
Revert "Remove unnecessary rspack_fs Error"
This reverts commit 46d63ff.
1 parent 68612d8 commit f022f88

File tree

12 files changed

+191
-108
lines changed

12 files changed

+191
-108
lines changed

crates/rspack_core/src/compiler/mod.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::{atomic::AtomicU32, Arc};
66

77
use futures::future::join_all;
88
use rspack_cacheable::cacheable;
9-
use rspack_error::{miette::IntoDiagnostic, Result};
9+
use rspack_error::Result;
1010
use rspack_fs::{IntermediateFileSystem, NativeFileSystem, ReadableFileSystem, WritableFileSystem};
1111
use rspack_hook::define_hook;
1212
use rspack_paths::{Utf8Path, Utf8PathBuf};
@@ -436,8 +436,7 @@ impl Compiler {
436436
.parent()
437437
.unwrap_or_else(|| panic!("The parent of {file_path} can't found")),
438438
)
439-
.await
440-
.into_diagnostic()?;
439+
.await?;
441440

442441
let content = source.buffer();
443442

@@ -481,11 +480,7 @@ impl Compiler {
481480
};
482481

483482
if need_write {
484-
self
485-
.output_filesystem
486-
.write(&file_path, &content)
487-
.await
488-
.into_diagnostic()?;
483+
self.output_filesystem.write(&file_path, &content).await?;
489484
self.compilation.emitted_assets.insert(filename.to_string());
490485
}
491486

@@ -518,8 +513,7 @@ impl Compiler {
518513
self
519514
.output_filesystem
520515
.remove_dir_all(&self.options.output.path)
521-
.await
522-
.into_diagnostic()?;
516+
.await?;
523517
}
524518
CleanOptions::KeepPath(p) => {
525519
let path = self.options.output.path.join(p);
@@ -528,8 +522,7 @@ impl Compiler {
528522
&self.options.output.path,
529523
KeepPattern::Path(&path),
530524
)
531-
.await
532-
.into_diagnostic()?;
525+
.await?;
533526
}
534527
CleanOptions::KeepRegex(r) => {
535528
let keep_pattern = KeepPattern::Regex(r);
@@ -538,8 +531,7 @@ impl Compiler {
538531
&self.options.output.path,
539532
keep_pattern,
540533
)
541-
.await
542-
.into_diagnostic()?;
534+
.await?;
543535
}
544536
CleanOptions::KeepFunc(f) => {
545537
let keep_pattern = KeepPattern::Func(f);
@@ -548,8 +540,7 @@ impl Compiler {
548540
&self.options.output.path,
549541
keep_pattern,
550542
)
551-
.await
552-
.into_diagnostic()?;
543+
.await?;
553544
}
554545
_ => {}
555546
}

crates/rspack_core/src/resolver/boxfs.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
sync::Arc,
44
};
55

6-
use rspack_fs::ReadableFileSystem;
6+
use rspack_fs::{Error, FsResultToIoResultExt, ReadableFileSystem};
77
use rspack_paths::AssertUtf8;
88
use rspack_resolver::{FileMetadata, FileSystem as ResolverFileSystem};
99

@@ -18,32 +18,40 @@ impl BoxFS {
1818
#[async_trait::async_trait]
1919
impl ResolverFileSystem for BoxFS {
2020
async fn read(&self, path: &std::path::Path) -> io::Result<Vec<u8>> {
21-
self.0.read(path.assert_utf8()).await
21+
self.0.read(path.assert_utf8()).await.to_io_result()
2222
}
2323
async fn read_to_string(&self, path: &std::path::Path) -> std::io::Result<String> {
24-
let x = self.0.read(path.assert_utf8()).await?;
25-
String::from_utf8(x).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
24+
match self.0.read(path.assert_utf8()).await {
25+
Ok(x) => String::from_utf8(x).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err)),
26+
Err(Error::Io(e)) => Err(e),
27+
}
2628
}
2729
async fn metadata(&self, path: &std::path::Path) -> io::Result<FileMetadata> {
28-
let meta = self.0.metadata(path.assert_utf8()).await?;
29-
Ok(FileMetadata {
30-
is_dir: meta.is_directory,
31-
is_file: meta.is_file,
32-
is_symlink: meta.is_symlink,
33-
})
30+
match self.0.metadata(path.assert_utf8()).await {
31+
Ok(meta) => Ok(FileMetadata {
32+
is_dir: meta.is_directory,
33+
is_file: meta.is_file,
34+
is_symlink: meta.is_symlink,
35+
}),
36+
Err(Error::Io(e)) => Err(e),
37+
}
3438
}
3539

3640
async fn symlink_metadata(&self, path: &std::path::Path) -> io::Result<FileMetadata> {
37-
let meta = self.0.symlink_metadata(path.assert_utf8()).await?;
38-
Ok(FileMetadata {
39-
is_dir: meta.is_directory,
40-
is_file: meta.is_file,
41-
is_symlink: meta.is_symlink,
42-
})
41+
match self.0.symlink_metadata(path.assert_utf8()).await {
42+
Ok(meta) => Ok(FileMetadata {
43+
is_dir: meta.is_directory,
44+
is_file: meta.is_file,
45+
is_symlink: meta.is_symlink,
46+
}),
47+
Err(Error::Io(e)) => Err(e),
48+
}
4349
}
4450

4551
async fn canonicalize(&self, path: &std::path::Path) -> io::Result<std::path::PathBuf> {
46-
let path = self.0.canonicalize(path.assert_utf8()).await?;
47-
Ok(path.into())
52+
match self.0.canonicalize(path.assert_utf8()).await {
53+
Ok(path) => Ok(path.into()),
54+
Err(Error::Io(e)) => Err(e),
55+
}
4856
}
4957
}

crates/rspack_core/src/utils/fs_trim.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cow_utils::CowUtils;
2-
use rspack_fs::{Result as FsResult, RspackResultToFsResultExt, WritableFileSystem};
2+
use rspack_error::Result;
3+
use rspack_fs::{Result as FsResult, WritableFileSystem};
34
use rspack_paths::Utf8Path;
45
use rspack_regex::RspackRegex;
56

@@ -12,13 +13,11 @@ pub enum KeepPattern<'a> {
1213
}
1314

1415
impl<'a> KeepPattern<'a> {
15-
pub async fn try_match(&self, path: &'a Utf8Path) -> FsResult<bool> {
16+
pub async fn try_match(&self, path: &'a Utf8Path) -> Result<bool> {
1617
match self {
1718
KeepPattern::Path(p) => Ok(path.starts_with(p)),
1819
KeepPattern::Regex(r) => Ok(r.test(path.as_str().cow_replace("\\", "/").as_ref())),
19-
KeepPattern::Func(f) => f(path.as_str().cow_replace("\\", "/").to_string())
20-
.await
21-
.to_fs_result(),
20+
KeepPattern::Func(f) => f(path.as_str().cow_replace("\\", "/").to_string()).await,
2221
}
2322
}
2423
}

crates/rspack_fs/src/error.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,87 @@
1-
pub type Error = std::io::Error;
1+
use std::fmt::Display;
2+
3+
use rspack_error::{
4+
miette::{self, Diagnostic},
5+
thiserror::{self, Error},
6+
};
7+
8+
#[derive(Debug, Error, Diagnostic)]
9+
#[error("Rspack FS Error: {0}")]
10+
struct FsError(#[source] std::io::Error);
11+
12+
#[derive(Debug)]
13+
pub enum Error {
14+
/// Generic I/O error
15+
Io(std::io::Error),
16+
}
17+
18+
impl Error {
19+
pub fn new(kind: std::io::ErrorKind, message: &str) -> Self {
20+
Error::Io(std::io::Error::new(kind, message))
21+
}
22+
}
23+
24+
impl From<std::io::Error> for Error {
25+
fn from(value: std::io::Error) -> Self {
26+
Self::Io(value)
27+
}
28+
}
29+
30+
impl From<Error> for rspack_error::Error {
31+
fn from(value: Error) -> Self {
32+
match value {
33+
Error::Io(err) => FsError(err).into(),
34+
}
35+
}
36+
}
37+
38+
impl From<rspack_error::Error> for Error {
39+
fn from(e: rspack_error::Error) -> Self {
40+
Error::Io(std::io::Error::other(e.to_string()))
41+
}
42+
}
43+
44+
impl Display for Error {
45+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46+
match self {
47+
Error::Io(err) => write!(f, "IO error: {err}"),
48+
}
49+
}
50+
}
251

352
pub type Result<T> = std::result::Result<T, Error>;
453

554
pub trait RspackResultToFsResultExt<T> {
655
fn to_fs_result(self) -> Result<T>;
756
}
857

9-
impl<T> RspackResultToFsResultExt<T> for rspack_error::Result<T> {
58+
impl<T, E: ToString> RspackResultToFsResultExt<T> for std::result::Result<T, E> {
1059
fn to_fs_result(self) -> Result<T> {
11-
self.map_err(|e| Error::other(e.to_string()))
60+
match self {
61+
Ok(t) => Ok(t),
62+
Err(e) => Err(Error::Io(std::io::Error::other(e.to_string()))),
63+
}
64+
}
65+
}
66+
67+
pub trait IoResultToFsResultExt<T> {
68+
fn to_fs_result(self) -> Result<T>;
69+
}
70+
71+
impl<T> IoResultToFsResultExt<T> for std::io::Result<T> {
72+
fn to_fs_result(self) -> Result<T> {
73+
self.map_err(Error::from)
74+
}
75+
}
76+
77+
pub trait FsResultToIoResultExt<T> {
78+
fn to_io_result(self) -> std::io::Result<T>;
79+
}
80+
81+
impl<T> FsResultToIoResultExt<T> for Result<T> {
82+
fn to_io_result(self) -> std::io::Result<T> {
83+
self.map_err(|e| match e {
84+
Error::Io(err) => err,
85+
})
1286
}
1387
}

crates/rspack_fs/src/file_metadata.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fs::Metadata, io::ErrorKind};
22

3-
use crate::{Error, Result};
3+
use crate::{Error, IoResultToFsResultExt, Result};
44

55
#[derive(Debug, Clone)]
66
pub struct FileMetadata {
@@ -17,7 +17,8 @@ impl TryFrom<Metadata> for FileMetadata {
1717

1818
fn try_from(metadata: Metadata) -> Result<Self> {
1919
let mtime_ms = metadata
20-
.modified()?
20+
.modified()
21+
.to_fs_result()?
2122
.duration_since(std::time::UNIX_EPOCH)
2223
.expect("mtime is before unix epoch")
2324
.as_millis() as u64;
@@ -37,7 +38,8 @@ impl TryFrom<Metadata> for FileMetadata {
3738
}
3839
};
3940
let atime_ms = metadata
40-
.accessed()?
41+
.accessed()
42+
.to_fs_result()?
4143
.duration_since(std::time::UNIX_EPOCH)
4244
.expect("atime is before unix epoch")
4345
.as_millis() as u64;

crates/rspack_fs/src/intermediate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Debug;
33
use rspack_paths::Utf8Path;
44

55
use super::Result;
6-
use crate::WritableFileSystem;
6+
use crate::{Error, WritableFileSystem};
77

88
pub trait IntermediateFileSystem:
99
WritableFileSystem + IntermediateFileSystemExtras + Debug + Send + Sync
@@ -22,7 +22,7 @@ pub trait ReadStream: Debug + Sync + Send {
2222
async fn read_line(&mut self) -> Result<String> {
2323
match String::from_utf8(self.read_until(b'\n').await?) {
2424
Ok(s) => Ok(s),
25-
Err(_) => Err(std::io::Error::other("invalid utf8 line")),
25+
Err(_) => Err(Error::Io(std::io::Error::other("invalid utf8 line"))),
2626
}
2727
}
2828
async fn read(&mut self, length: usize) -> Result<Vec<u8>>;

crates/rspack_fs/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ mod memory_fs;
2020
pub use memory_fs::{MemoryFileSystem, MemoryReadStream, MemoryWriteStream};
2121

2222
mod error;
23-
pub use error::{Error, Result, RspackResultToFsResultExt};
23+
pub use error::{
24+
Error, FsResultToIoResultExt, IoResultToFsResultExt, Result, RspackResultToFsResultExt,
25+
};

crates/rspack_fs/src/memory_fs.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use std::{
22
collections::{HashMap, HashSet},
3-
io::{BufRead, Cursor, ErrorKind, Read, Seek},
3+
io::{BufRead, Cursor, Read, Seek},
44
sync::{Arc, Mutex},
55
time::{SystemTime, UNIX_EPOCH},
66
};
77

88
use rspack_paths::{AssertUtf8, Utf8Path, Utf8PathBuf};
99

1010
use crate::{
11-
Error, FileMetadata, IntermediateFileSystem, IntermediateFileSystemExtras, ReadStream,
12-
ReadableFileSystem, Result, WritableFileSystem, WriteStream,
11+
Error, FileMetadata, IntermediateFileSystem, IntermediateFileSystemExtras, IoResultToFsResultExt,
12+
ReadStream, ReadableFileSystem, Result, WritableFileSystem, WriteStream,
1313
};
1414

1515
fn current_time() -> u64 {
@@ -20,7 +20,7 @@ fn current_time() -> u64 {
2020
}
2121

2222
fn new_error(msg: &str) -> Error {
23-
Error::other(msg)
23+
Error::Io(std::io::Error::other(msg))
2424
}
2525

2626
#[derive(Debug)]
@@ -240,7 +240,10 @@ impl ReadableFileSystem for MemoryFileSystem {
240240
let files = self.files.lock().expect("should get lock");
241241
match files.get(path) {
242242
Some(FileType::File { content, .. }) => Ok(content.clone()),
243-
_ => Err(Error::new(ErrorKind::NotFound, "file not exist")),
243+
_ => Err(Error::Io(std::io::Error::new(
244+
std::io::ErrorKind::NotFound,
245+
"file not exist",
246+
))),
244247
}
245248
}
246249

@@ -252,7 +255,10 @@ impl ReadableFileSystem for MemoryFileSystem {
252255
let files = self.files.lock().expect("should get lock");
253256
match files.get(path) {
254257
Some(ft) => Ok(ft.metadata().clone()),
255-
None => Err(Error::new(ErrorKind::NotFound, "file not exist")),
258+
None => Err(Error::Io(std::io::Error::new(
259+
std::io::ErrorKind::NotFound,
260+
"file not exist",
261+
))),
256262
}
257263
}
258264

@@ -308,25 +314,25 @@ impl MemoryReadStream {
308314
impl ReadStream for MemoryReadStream {
309315
async fn read(&mut self, length: usize) -> Result<Vec<u8>> {
310316
let mut buf = vec![0u8; length];
311-
self.0.read_exact(&mut buf)?;
317+
self.0.read_exact(&mut buf).to_fs_result()?;
312318
Ok(buf)
313319
}
314320

315321
async fn read_until(&mut self, byte: u8) -> Result<Vec<u8>> {
316322
let mut buf = vec![];
317-
self.0.read_until(byte, &mut buf)?;
323+
self.0.read_until(byte, &mut buf).to_fs_result()?;
318324
if buf.last().is_some_and(|b| b == &byte) {
319325
buf.pop();
320326
}
321327
Ok(buf)
322328
}
323329
async fn read_to_end(&mut self) -> Result<Vec<u8>> {
324330
let mut buf = vec![];
325-
self.0.read_to_end(&mut buf)?;
331+
self.0.read_to_end(&mut buf).to_fs_result()?;
326332
Ok(buf)
327333
}
328334
async fn skip(&mut self, offset: usize) -> Result<()> {
329-
self.0.seek_relative(offset as i64)
335+
self.0.seek_relative(offset as i64).to_fs_result()
330336
}
331337
async fn close(&mut self) -> Result<()> {
332338
Ok(())

0 commit comments

Comments
 (0)