Skip to content

Commit 039c4e8

Browse files
committed
Move from displaydoc to thiserror, enabling core::error::Error.
1 parent 56e38f6 commit 039c4e8

File tree

4 files changed

+69
-60
lines changed

4 files changed

+69
-60
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ categories = ["filesystem", "no-std", "os::windows-apis", "parser-implementation
1717
arrayvec = { version = "0.7.2", default-features = false }
1818
bitflags = "2.3.1"
1919
derive_more = { version = "2.1.1", default-features = false, features = ["display", "from"] }
20-
displaydoc = { version = "0.2.3", default-features = false }
2120
enumn = "0.1.3"
2221
memoffset = "0.9.0"
2322
nt-string = { version = "0.1.1", features = ["alloc"], default-features = false }
2423
strum_macros = "0.27.0"
24+
thiserror = { version = "2.0.17", default-features = false }
2525
time = { version = "0.3.9", features = ["large-dates", "macros"], default-features = false, optional = true }
2626
zerocopy = { version = "0.8.31", features = ["derive"] }
2727

@@ -31,7 +31,7 @@ time = { version = "0.3.9", features = ["formatting", "large-dates", "macros"],
3131

3232
[features]
3333
default = ["std"]
34-
std = ["arrayvec/std", "nt-string/std", "time?/std", "zerocopy/std"]
34+
std = ["arrayvec/std", "nt-string/std", "thiserror/std", "time?/std", "zerocopy/std"]
3535

3636
[[example]]
3737
name = "ntfs-shell"

src/error.rs

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use core::ops::Range;
55

6-
use displaydoc::Display;
6+
use thiserror::Error;
77

88
use crate::attribute::NtfsAttributeType;
99
use crate::io;
@@ -14,217 +14,225 @@ use crate::types::{Lcn, Vcn};
1414
pub type Result<T, E = NtfsError> = core::result::Result<T, E>;
1515

1616
/// Central error type of ntfs.
17-
#[derive(Debug, Display)]
17+
#[derive(Debug, Error)]
1818
#[non_exhaustive]
1919
pub enum NtfsError {
20-
/// The NTFS file at byte position {position:#x} has no attribute of type {ty:?}, but it was expected
20+
#[error("The NTFS file at byte position {position:#x} has no attribute of type {ty:?}, but it was expected")]
2121
AttributeNotFound {
2222
position: NtfsPosition,
2323
ty: NtfsAttributeType,
2424
},
25-
/// The NTFS Attribute at byte position {position:#x} should have type {expected:?}, but it actually has type {actual:?}
25+
#[error("The NTFS Attribute at byte position {position:#x} should have type {expected:?}, but it actually has type {actual:?}")]
2626
AttributeOfDifferentType {
2727
position: NtfsPosition,
2828
expected: NtfsAttributeType,
2929
actual: NtfsAttributeType,
3030
},
31-
/// The given buffer should have at least {expected} bytes, but it only has {actual} bytes
31+
#[error(
32+
"The given buffer should have at least {expected} bytes, but it only has {actual} bytes"
33+
)]
3234
BufferTooSmall { expected: usize, actual: usize },
33-
/// The NTFS Attribute at byte position {position:#x} has a length of {expected} bytes, but only {actual} bytes are left in the record
35+
#[error("The NTFS Attribute at byte position {position:#x} has a length of {expected} bytes, but only {actual} bytes are left in the record")]
3436
InvalidAttributeLength {
3537
position: NtfsPosition,
3638
expected: usize,
3739
actual: usize,
3840
},
39-
/// The NTFS Attribute at byte position {position:#x} indicates a name length up to offset {expected}, but the attribute only has a size of {actual} bytes
41+
#[error("The NTFS Attribute at byte position {position:#x} indicates a name length up to offset {expected}, but the attribute only has a size of {actual} bytes")]
4042
InvalidAttributeNameLength {
4143
position: NtfsPosition,
4244
expected: usize,
4345
actual: u32,
4446
},
45-
/// The NTFS Attribute at byte position {position:#x} indicates that its name starts at offset {expected}, but the attribute only has a size of {actual} bytes
47+
#[error("The NTFS Attribute at byte position {position:#x} indicates that its name starts at offset {expected}, but the attribute only has a size of {actual} bytes")]
4648
InvalidAttributeNameOffset {
4749
position: NtfsPosition,
4850
expected: u16,
4951
actual: u32,
5052
},
51-
/// The NTFS Data Run header at byte position {position:#x} indicates a maximum byte count of {expected}, but {actual} is the limit
53+
#[error("The NTFS Data Run header at byte position {position:#x} indicates a maximum byte count of {expected}, but {actual} is the limit")]
5254
InvalidByteCountInDataRunHeader {
5355
position: NtfsPosition,
5456
expected: u8,
5557
actual: u8,
5658
},
57-
/// The cluster count {cluster_count} read from the NTFS Data Run header at byte position {position:#x} is invalid
59+
#[error("The cluster count {cluster_count} read from the NTFS Data Run header at byte position {position:#x} is invalid")]
5860
InvalidClusterCountInDataRunHeader {
5961
position: NtfsPosition,
6062
cluster_count: u64,
6163
},
62-
/// The NTFS File Record at byte position {position:#x} indicates an allocated size of {expected} bytes, but the record only has a size of {actual} bytes
64+
#[error("The NTFS File Record at byte position {position:#x} indicates an allocated size of {expected} bytes, but the record only has a size of {actual} bytes")]
6365
InvalidFileAllocatedSize {
6466
position: NtfsPosition,
6567
expected: u32,
6668
actual: u32,
6769
},
68-
/// The requested NTFS File Record Number {file_record_number} is invalid
70+
#[error("The requested NTFS File Record Number {file_record_number} is invalid")]
6971
InvalidFileRecordNumber { file_record_number: u64 },
70-
/// The NTFS File Record at byte position {position:#x} should have signature {expected:?}, but it has signature {actual:?}
72+
#[error("The NTFS File Record at byte position {position:#x} should have signature {expected:?}, but it has signature {actual:?}")]
7173
InvalidFileSignature {
7274
position: NtfsPosition,
7375
expected: &'static [u8],
7476
actual: [u8; 4],
7577
},
76-
/// The NTFS File Record at byte position {position:#x} indicates a used size of {expected} bytes, but only {actual} bytes are allocated
78+
#[error("The NTFS File Record at byte position {position:#x} indicates a used size of {expected} bytes, but only {actual} bytes are allocated")]
7779
InvalidFileUsedSize {
7880
position: NtfsPosition,
7981
expected: u32,
8082
actual: u32,
8183
},
82-
/// The NTFS Index Record at byte position {position:#x} indicates an allocated size of {expected} bytes, but the record only has a size of {actual} bytes
84+
#[error("The NTFS Index Record at byte position {position:#x} indicates an allocated size of {expected} bytes, but the record only has a size of {actual} bytes")]
8385
InvalidIndexAllocatedSize {
8486
position: NtfsPosition,
8587
expected: u32,
8688
actual: u32,
8789
},
88-
/// The NTFS Index Entry at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes
90+
#[error("The NTFS Index Entry at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes")]
8991
InvalidIndexEntryDataRange {
9092
position: NtfsPosition,
9193
range: Range<usize>,
9294
size: u16,
9395
},
94-
/// The NTFS Index Entry at byte position {position:#x} reports a size of {expected} bytes, but it only has {actual} bytes
96+
#[error("The NTFS Index Entry at byte position {position:#x} reports a size of {expected} bytes, but it only has {actual} bytes")]
9597
InvalidIndexEntrySize {
9698
position: NtfsPosition,
9799
expected: u16,
98100
actual: u16,
99101
},
100-
/// The NTFS index root at byte position {position:#x} indicates that its entries start at offset {expected}, but the index root only has a size of {actual} bytes
102+
#[error("The NTFS index root at byte position {position:#x} indicates that its entries start at offset {expected}, but the index root only has a size of {actual} bytes")]
101103
InvalidIndexRootEntriesOffset {
102104
position: NtfsPosition,
103105
expected: usize,
104106
actual: usize,
105107
},
106-
/// The NTFS index root at byte position {position:#x} indicates a used size up to offset {expected}, but the index root only has a size of {actual} bytes
108+
#[error("The NTFS index root at byte position {position:#x} indicates a used size up to offset {expected}, but the index root only has a size of {actual} bytes")]
107109
InvalidIndexRootUsedSize {
108110
position: NtfsPosition,
109111
expected: usize,
110112
actual: usize,
111113
},
112-
/// The NTFS Index Record at byte position {position:#x} should have signature {expected:?}, but it has signature {actual:?}
114+
#[error("The NTFS Index Record at byte position {position:#x} should have signature {expected:?}, but it has signature {actual:?}")]
113115
InvalidIndexSignature {
114116
position: NtfsPosition,
115117
expected: &'static [u8],
116118
actual: [u8; 4],
117119
},
118-
/// The NTFS Index Record at byte position {position:#x} indicates a used size of {expected} bytes, but only {actual} bytes are allocated
120+
#[error("The NTFS Index Record at byte position {position:#x} indicates a used size of {expected} bytes, but only {actual} bytes are allocated")]
119121
InvalidIndexUsedSize {
120122
position: NtfsPosition,
121123
expected: u32,
122124
actual: u32,
123125
},
124-
/// The MFT LCN in the BIOS Parameter Block of the NTFS filesystem is invalid.
126+
#[error("The MFT LCN in the BIOS Parameter Block of the NTFS filesystem is invalid.")]
125127
InvalidMftLcn,
126-
/// The NTFS Non Resident Value Data at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes
128+
#[error("The NTFS Non Resident Value Data at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes")]
127129
InvalidNonResidentValueDataRange {
128130
position: NtfsPosition,
129131
range: Range<usize>,
130132
size: usize,
131133
},
132-
/// The resident NTFS Attribute at byte position {position:#x} indicates a value length of {length} starting at offset {offset}, but the attribute only has a size of {actual} bytes
134+
#[error("The resident NTFS Attribute at byte position {position:#x} indicates a value length of {length} starting at offset {offset}, but the attribute only has a size of {actual} bytes")]
133135
InvalidResidentAttributeValueLength {
134136
position: NtfsPosition,
135137
length: u32,
136138
offset: u16,
137139
actual: u32,
138140
},
139-
/// The resident NTFS Attribute at byte position {position:#x} indicates that its value starts at offset {expected}, but the attribute only has a size of {actual} bytes
141+
#[error("The resident NTFS Attribute at byte position {position:#x} indicates that its value starts at offset {expected}, but the attribute only has a size of {actual} bytes")]
140142
InvalidResidentAttributeValueOffset {
141143
position: NtfsPosition,
142144
expected: u16,
143145
actual: u32,
144146
},
145-
/// A record size field in the BIOS Parameter Block denotes {size_info}, which is invalid considering the cluster size of {cluster_size} bytes
147+
#[error("A record size field in the BIOS Parameter Block denotes {size_info}, which is invalid considering the cluster size of {cluster_size} bytes")]
146148
InvalidRecordSizeInfo { size_info: i8, cluster_size: u32 },
147-
/// The sectors per cluster field in the BIOS Parameter Block denotes {sectors_per_cluster:#04x}, which is invalid
149+
#[error("The sectors per cluster field in the BIOS Parameter Block denotes {sectors_per_cluster:#04x}, which is invalid")]
148150
InvalidSectorsPerCluster { sectors_per_cluster: u8 },
149-
/// The NTFS structured value at byte position {position:#x} of type {ty:?} has {actual} bytes where {expected} bytes were expected
151+
#[error("The NTFS structured value at byte position {position:#x} of type {ty:?} has {actual} bytes where {expected} bytes were expected")]
150152
InvalidStructuredValueSize {
151153
position: NtfsPosition,
152154
ty: NtfsAttributeType,
153155
expected: u64,
154156
actual: u64,
155157
},
156-
/// The given time can't be represented as an NtfsTime
158+
#[error("The given time can't be represented as an NtfsTime")]
157159
InvalidTime,
158-
/// The 2-byte signature field at byte position {position:#x} should contain {expected:?}, but it contains {actual:?}
160+
#[error("The 2-byte signature field at byte position {position:#x} should contain {expected:?}, but it contains {actual:?}")]
159161
InvalidTwoByteSignature {
160162
position: NtfsPosition,
161163
expected: &'static [u8],
162164
actual: [u8; 2],
163165
},
164-
/// The Upcase Table should have a size of {expected} bytes, but it has {actual} bytes
166+
#[error("The Upcase Table should have a size of {expected} bytes, but it has {actual} bytes")]
165167
InvalidUpcaseTableSize { expected: u64, actual: u64 },
166-
/// The NTFS Update Sequence Count of the record at byte position {position:#x} has the invalid value {update_sequence_count}
168+
#[error("The NTFS Update Sequence Count of the record at byte position {position:#x} has the invalid value {update_sequence_count}")]
167169
InvalidUpdateSequenceCount {
168170
position: NtfsPosition,
169171
update_sequence_count: u16,
170172
},
171-
/// The NTFS Update Sequence Number of the record at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes
173+
#[error("The NTFS Update Sequence Number of the record at byte position {position:#x} references a data field in the range {range:?}, but the entry only has a size of {size} bytes")]
172174
InvalidUpdateSequenceNumberRange {
173175
position: NtfsPosition,
174176
range: Range<usize>,
175177
size: usize,
176178
},
177-
/// The VCN {vcn} read from the NTFS Data Run header at byte position {position:#x} cannot be added to the LCN {previous_lcn} calculated from previous data runs
179+
#[error("The VCN {vcn} read from the NTFS Data Run header at byte position {position:#x} cannot be added to the LCN {previous_lcn} calculated from previous data runs")]
178180
InvalidVcnInDataRunHeader {
179181
position: NtfsPosition,
180182
vcn: Vcn,
181183
previous_lcn: Lcn,
182184
},
183-
/// I/O error: {0:?}
185+
#[error("I/O error: {0:?}")]
184186
Io(io::Error),
185-
/// The Logical Cluster Number (LCN) {lcn} is too big to be multiplied by the cluster size
187+
#[error(
188+
"The Logical Cluster Number (LCN) {lcn} is too big to be multiplied by the cluster size"
189+
)]
186190
LcnTooBig { lcn: Lcn },
187-
/// The index root at byte position {position:#x} is a large index, but no matching index allocation attribute was provided
191+
#[error("The index root at byte position {position:#x} is a large index, but no matching index allocation attribute was provided")]
188192
MissingIndexAllocation { position: NtfsPosition },
189-
/// The NTFS file at byte position {position:#x} is not a directory
193+
#[error("The NTFS file at byte position {position:#x} is not a directory")]
190194
NotADirectory { position: NtfsPosition },
191-
/// The total sector count {total_sectors} is too big to be multiplied by the sector size
195+
#[error(
196+
"The total sector count {total_sectors} is too big to be multiplied by the sector size"
197+
)]
192198
TotalSectorsTooBig { total_sectors: u64 },
193-
/// The NTFS Attribute at byte position {position:#x} should not belong to an Attribute List, but it does
199+
#[error("The NTFS Attribute at byte position {position:#x} should not belong to an Attribute List, but it does")]
194200
UnexpectedAttributeListAttribute { position: NtfsPosition },
195-
/// The NTFS Attribute at byte position {position:#x} should be resident, but it is non-resident
201+
#[error("The NTFS Attribute at byte position {position:#x} should be resident, but it is non-resident")]
196202
UnexpectedNonResidentAttribute { position: NtfsPosition },
197-
/// The NTFS Attribute at byte position {position:#x} should be non-resident, but it is resident
203+
#[error("The NTFS Attribute at byte position {position:#x} should be non-resident, but it is resident")]
198204
UnexpectedResidentAttribute { position: NtfsPosition },
199-
/// The type of the NTFS Attribute at byte position {position:#x} is {actual:#010x}, which is not supported
205+
#[error("The type of the NTFS Attribute at byte position {position:#x} is {actual:#010x}, which is not supported")]
200206
UnsupportedAttributeType { position: NtfsPosition, actual: u32 },
201-
/// The cluster size is {actual} bytes, but it needs to be between {min} and {max}
207+
#[error("The cluster size is {actual} bytes, but it needs to be between {min} and {max}")]
202208
UnsupportedClusterSize { min: u32, max: u32, actual: u32 },
203-
/// The namespace of the NTFS file name starting at byte position {position:#x} is {actual}, which is not supported
209+
#[error("The namespace of the NTFS file name starting at byte position {position:#x} is {actual}, which is not supported")]
204210
UnsupportedFileNamespace { position: NtfsPosition, actual: u8 },
205-
/// The sector size is {actual} bytes, but it needs to be between {min} and {max}
211+
#[error("The sector size is {actual} bytes, but it needs to be between {min} and {max}")]
206212
UnsupportedSectorSize { min: u16, max: u16, actual: u16 },
207-
/// The Update Sequence Array (USA) of the record at byte position {position:#x} has entries for {array_count} blocks of 512 bytes, but the record is only {record_size} bytes long
213+
#[error("The Update Sequence Array (USA) of the record at byte position {position:#x} has entries for {array_count} blocks of 512 bytes, but the record is only {record_size} bytes long")]
208214
UpdateSequenceArrayExceedsRecordSize {
209215
position: NtfsPosition,
210216
array_count: u16,
211217
record_size: usize,
212218
},
213-
/// Sector corruption: The 2 bytes at byte position {position:#x} should match the Update Sequence Number (USN) {expected:?}, but they are {actual:?}
219+
#[error("Sector corruption: The 2 bytes at byte position {position:#x} should match the Update Sequence Number (USN) {expected:?}, but they are {actual:?}")]
214220
UpdateSequenceNumberMismatch {
215221
position: NtfsPosition,
216222
expected: [u8; 2],
217223
actual: [u8; 2],
218224
},
219-
/// The index allocation at byte position {position:#x} references a Virtual Cluster Number (VCN) {expected}, but a record with VCN {actual} is found at that offset
225+
#[error("The index allocation at byte position {position:#x} references a Virtual Cluster Number (VCN) {expected}, but a record with VCN {actual} is found at that offset")]
220226
VcnMismatchInIndexAllocation {
221227
position: NtfsPosition,
222228
expected: Vcn,
223229
actual: Vcn,
224230
},
225-
/// The index allocation at byte position {position:#x} references a Virtual Cluster Number (VCN) {vcn}, but this VCN exceeds the boundaries of the filesystem
231+
#[error("The index allocation at byte position {position:#x} references a Virtual Cluster Number (VCN) {vcn}, but this VCN exceeds the boundaries of the filesystem")]
226232
VcnOutOfBoundsInIndexAllocation { position: NtfsPosition, vcn: Vcn },
227-
/// The Virtual Cluster Number (VCN) {vcn} is too big to be multiplied by the cluster size
233+
#[error(
234+
"The Virtual Cluster Number (VCN) {vcn} is too big to be multiplied by the cluster size"
235+
)]
228236
VcnTooBig { vcn: Vcn },
229237
}
230238

@@ -245,7 +253,3 @@ impl From<NtfsError> for io::Error {
245253
}
246254
}
247255
}
248-
249-
#[cfg(feature = "std")]
250-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
251-
impl std::error::Error for NtfsError {}

src/io/no_std/error.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
//! Mostly imported from https://github.com/rust-lang/rust/blob/561364e4d5ccc506f610208a4989e91fdbdc8ca7/library/std/src/io/error.rs
55
6-
use displaydoc::Display;
6+
use core::fmt;
77

88
/// A specialized [`Result`] type for I/O operations.
99
pub type Result<T> = core::result::Result<T, Error>;
@@ -33,7 +33,7 @@ impl Error {
3333
/// Simplified version of [`std::io::ErrorKind`] for `no_std` environments.
3434
///
3535
/// See its documentation for more details.
36-
#[derive(Clone, Copy, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
36+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
3737
#[non_exhaustive]
3838
pub enum ErrorKind {
3939
/// A parameter was incorrect.
@@ -45,3 +45,9 @@ pub enum ErrorKind {
4545
/// A custom error that does not fall under any other I/O error kind.
4646
Other,
4747
}
48+
49+
impl fmt::Display for ErrorKind {
50+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51+
fmt::Debug::fmt(self, f)
52+
}
53+
}

src/io/no_std/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
mod error;
32
mod read;
43
mod seek;

0 commit comments

Comments
 (0)