From b35f7a51799ce723b9930d9f7d7e05ff2d6a051f Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sat, 9 Oct 2021 16:37:10 +0200 Subject: [PATCH] convenient "data_as_str" getter (v0.1.4) --- Cargo.toml | 2 +- README.md | 3 +-- examples/minimal.rs | 3 +-- src/archive.rs | 14 +++++++++----- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b38376c..374f292 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ as GNU Longname. The maximum supported file name length is 100 characters includ The maximum supported file size is 8GiB. Also, directories are not supported yet but only flat collections of files. """ -version = "0.1.3" +version = "0.1.4" edition = "2018" keywords = ["tar", "tarball", "archive"] categories = ["data-structures", "no-std", "parser-implementations"] diff --git a/README.md b/README.md index bde7264..b07390a 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,7 @@ fn main() { let entries = archive.entries().collect::>(); println!("{:#?}", entries); println!("content of last file:"); - let last_file_content = unsafe { core::str::from_utf8_unchecked(entries[2].data()) }; - println!("{:#?}", last_file_content); + println!("{:#?}", entries[2].data_as_str().expect("Invalid UTF-8") ); } ``` diff --git a/examples/minimal.rs b/examples/minimal.rs index 628790b..d77fc0c 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -35,6 +35,5 @@ fn main() { let entries = archive.entries().collect::>(); println!("{:#?}", entries); println!("content of last file:"); - let last_file_content = unsafe { core::str::from_utf8_unchecked(entries[2].data()) }; - println!("{:#?}", last_file_content); + println!("{:#?}", entries[2].data_as_str().expect("Invalid UTF-8") ); } diff --git a/src/archive.rs b/src/archive.rs index 224280c..e288bca 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -27,7 +27,7 @@ use crate::header::PosixHeader; use crate::{TypeFlag, BLOCKSIZE}; use arrayvec::ArrayString; use core::fmt::{Debug, Formatter}; -use core::str::FromStr; +use core::str::{FromStr, Utf8Error}; /// Describes an entry in an archive. /// Currently only supports files but no directories. @@ -57,6 +57,11 @@ impl<'a> ArchiveEntry<'a> { self.data } + /// Data of the file as string slice, if data is valid UTF-8. + pub fn data_as_str(&self) -> Result<&'a str, Utf8Error> { + core::str::from_utf8(self.data) + } + /// Filesize in bytes. pub const fn size(&self) -> usize { self.size @@ -183,7 +188,6 @@ impl<'a> Iterator for ArchiveIterator<'a> { #[cfg(test)] mod tests { use super::*; - use core::str; use std::vec::Vec; #[test] @@ -233,7 +237,7 @@ mod tests { assert_eq!(entries[0].size(), 513); assert_eq!(entries[0].data().len(), 513); assert_eq!( - unsafe { str::from_utf8_unchecked(entries[0].data) }, + entries[0].data_as_str().expect("Invalid UTF-8"), include_str!("../tests/bye_world_513b.txt") ); @@ -241,7 +245,7 @@ mod tests { assert_eq!(entries[1].size(), 513); assert_eq!(entries[1].data().len(), 513); assert_eq!( - unsafe { str::from_utf8_unchecked(entries[1].data) }, + entries[1].data_as_str().expect("Invalid UTF-8"), include_str!("../tests/hello_world_513b.txt") ); @@ -249,7 +253,7 @@ mod tests { assert_eq!(entries[2].size(), 12); assert_eq!(entries[2].data().len(), 12); assert_eq!( - unsafe { str::from_utf8_unchecked(entries[2].data) }, + entries[2].data_as_str().expect("Invalid UTF-8"), "Hello World\n", "file content must match" );