From 270127a5b852e27d88ace5be0c49ab04368a1f1b Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Fri, 17 Sep 2021 19:29:44 +0200 Subject: Remove an `Option` around the return value of `NtfsAttribute::name`. There is no difference between an empty attribute name and no attribute name at all. --- src/attribute.rs | 8 ++++---- src/file.rs | 35 ++++++++--------------------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/src/attribute.rs b/src/attribute.rs index 55cd8e6..17eb855 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -157,18 +157,18 @@ impl<'n, 'f> NtfsAttribute<'n, 'f> { /// /// Note that most NTFS attributes have no name and are distinguished by their types. /// Use [`NtfsAttribute::ty`] to get the attribute type. - pub fn name(&self) -> Option>> { + pub fn name(&self) -> Result> { if self.name_offset() == 0 || self.name_length() == 0 { - return None; + return Ok(NtfsString(&[])); } - iter_try!(self.validate_name_sizes()); + self.validate_name_sizes()?; let start = self.offset + self.name_offset() as usize; let end = start + self.name_length(); let string = NtfsString(&self.file.record_data()[start..end]); - Some(Ok(string)) + Ok(string) } fn name_offset(&self) -> u16 { diff --git a/src/file.rs b/src/file.rs index f2dff53..097eff0 100644 --- a/src/file.rs +++ b/src/file.rs @@ -113,17 +113,13 @@ impl<'n> NtfsFile<'n> { /// Convenience function to get a $DATA attribute of this file. /// - /// As NTFS supports multiple data streams per file, you can optionally specify a data stream - /// name and NTFS will look up the corresponding $DATA attribute. - /// If you specify `None` for `data_stream_name`, the default unnamed $DATA attribute will be looked - /// up (commonly known as the "file data"). + /// As NTFS supports multiple data streams per file, you can specify the name of the $DATA attribute + /// to look up. + /// Passing an empty string here looks up the default unnamed $DATA attribute (commonly known as the "file data"). /// /// If you need more control over which $DATA attribute is available and picked up, /// you can use [`NtfsFile::attributes`] to iterate over all attributes of this file. - pub fn data<'f>( - &'f self, - data_stream_name: Option<&str>, - ) -> Option>> { + pub fn data<'f>(&'f self, data_stream_name: &str) -> Option>> { // Create an iterator that emits all $DATA attributes. let iter = self.attributes().filter(|attribute| { // TODO: Replace by attribute.ty().contains() once https://github.com/rust-lang/rust/issues/62358 has landed. @@ -134,25 +130,10 @@ impl<'n> NtfsFile<'n> { }); for attribute in iter { - match (attribute.name(), data_stream_name) { - (None, None) => { - // We found the unnamed $DATA attribute and are looking for the unnamed $DATA attribute. - return Some(Ok(attribute)); - } - (Some(Ok(name)), Some(data_stream_name)) => { - // We found a named $DATA attribute and are looking for a named $DATA attribute. - if data_stream_name == name { - return Some(Ok(attribute)); - } - } - (Some(Err(e)), _) => { - // We hit an error while fetching the $DATA attribute name. - return Some(Err(e)); - } - _ => { - // In any other case, we didn't find what we are looking for. - continue; - } + let name = iter_try!(attribute.name()); + + if data_stream_name == name { + return Some(Ok(attribute)); } } -- cgit v1.2.3