From fb0551dfcd05a506e4bd86bcd364134841bd4cf4 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Thu, 16 Sep 2021 20:22:26 +0200 Subject: Save code and get better formatters via `derive_more` and `strum_macros` --- Cargo.toml | 2 ++ src/attribute.rs | 3 +- src/structured_values/file_name.rs | 2 +- src/time.rs | 23 +++++---------- src/types.rs | 60 +++++++++++++++++++++----------------- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 047e301..957eba8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,11 @@ binread = { version = "2.1.1", features = ["const_generics"], default-features = byteorder = { version = "1.4.3", default-features = false } bitflags = "1.2.1" chrono = { version = "0.4.19", optional = true } +derive_more = "0.99.16" displaydoc = { version = "0.2.1", default-features = false } enumn = "0.1.3" memoffset = "0.6.4" +strum_macros = "0.21.1" [features] default = ["std"] diff --git a/src/attribute.rs b/src/attribute.rs index 5aa3dd9..55cd8e6 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -19,6 +19,7 @@ use core::mem; use core::ops::Range; use enumn::N; use memoffset::offset_of; +use strum_macros::Display; /// On-disk structure of the generic header of an NTFS attribute. #[repr(C, packed)] @@ -93,7 +94,7 @@ struct NtfsNonResidentAttributeHeader { initialized_size: u64, } -#[derive(Clone, Copy, Debug, Eq, N, PartialEq)] +#[derive(Clone, Copy, Debug, Display, Eq, N, PartialEq)] #[repr(u32)] pub enum NtfsAttributeType { StandardInformation = 0x10, diff --git a/src/structured_values/file_name.rs b/src/structured_values/file_name.rs index 6ec45b4..19a1e08 100644 --- a/src/structured_values/file_name.rs +++ b/src/structured_values/file_name.rs @@ -209,7 +209,7 @@ mod tests { .unwrap(); let creation_time = file_name.creation_time(); - assert!(*creation_time > NT_TIMESTAMP_2021_01_01); + assert!(creation_time.nt_timestamp() > NT_TIMESTAMP_2021_01_01); assert_eq!(creation_time, file_name.modification_time()); assert_eq!(creation_time, file_name.mft_record_modification_time()); assert_eq!(creation_time, file_name.access_time()); diff --git a/src/time.rs b/src/time.rs index 325d4ee..5e1d46c 100644 --- a/src/time.rs +++ b/src/time.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later use binread::BinRead; -use core::ops::Deref; +use derive_more::From; #[cfg(any(feature = "chrono", feature = "std"))] use core::convert::TryFrom; @@ -32,20 +32,13 @@ const INTERVALS_PER_SECOND: u64 = 10_000_000; #[cfg(feature = "chrono")] const INTERVALS_PER_DAY: u64 = 24 * 60 * 60 * INTERVALS_PER_SECOND; -#[derive(BinRead, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive(BinRead, Clone, Copy, Debug, Eq, From, Ord, PartialEq, PartialOrd)] pub struct NtfsTime(u64); -impl Deref for NtfsTime { - type Target = u64; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl From for NtfsTime { - fn from(value: u64) -> Self { - Self(value) +impl NtfsTime { + /// Returns the stored NT timestamp (number of 100-nanosecond intervals since January 1, 1601). + pub fn nt_timestamp(&self) -> u64 { + self.0 } } @@ -87,7 +80,7 @@ impl TryFrom> for NtfsTime { #[cfg(feature = "chrono")] impl From for DateTime { fn from(nt: NtfsTime) -> DateTime { - let mut remainder = *nt; + let mut remainder = nt.nt_timestamp(); let nano = (remainder % INTERVALS_PER_SECOND) as u32 * 100; remainder /= INTERVALS_PER_SECOND; @@ -163,6 +156,6 @@ pub(crate) mod tests { fn test_systemtime() { let st = SystemTime::now(); let nt = NtfsTime::try_from(st).unwrap(); - assert!(*nt > NT_TIMESTAMP_2021_01_01); + assert!(nt.nt_timestamp() > NT_TIMESTAMP_2021_01_01); } } diff --git a/src/types.rs b/src/types.rs index d327484..563f353 100644 --- a/src/types.rs +++ b/src/types.rs @@ -4,9 +4,24 @@ use crate::error::{NtfsError, Result}; use crate::ntfs::Ntfs; use binread::BinRead; -use core::fmt; +use derive_more::{Binary, Display, From, LowerHex, Octal, UpperHex}; -#[derive(BinRead, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive( + Binary, + BinRead, + Clone, + Copy, + Debug, + Display, + Eq, + From, + LowerHex, + Octal, + Ord, + PartialEq, + PartialOrd, + UpperHex, +)] pub struct Lcn(u64); impl Lcn { @@ -27,19 +42,22 @@ impl Lcn { } } -impl fmt::Display for Lcn { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl From for Lcn { - fn from(value: u64) -> Self { - Self(value) - } -} - -#[derive(BinRead, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive( + Binary, + BinRead, + Clone, + Copy, + Debug, + Display, + Eq, + From, + LowerHex, + Octal, + Ord, + PartialEq, + PartialOrd, + UpperHex, +)] pub struct Vcn(i64); impl Vcn { @@ -49,15 +67,3 @@ impl Vcn { .ok_or(NtfsError::VcnTooBig { vcn: *self }) } } - -impl fmt::Display for Vcn { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl From for Vcn { - fn from(value: i64) -> Self { - Self(value) - } -} -- cgit v1.2.3