convenient "data_as_str" getter (v0.1.4)

This commit is contained in:
Philipp Schuster 2021-10-09 16:37:10 +02:00
parent a8c44fb83d
commit b35f7a5179
4 changed files with 12 additions and 10 deletions

View File

@ -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 The maximum supported file size is 8GiB. Also, directories are not supported yet but only flat
collections of files. collections of files.
""" """
version = "0.1.3" version = "0.1.4"
edition = "2018" edition = "2018"
keywords = ["tar", "tarball", "archive"] keywords = ["tar", "tarball", "archive"]
categories = ["data-structures", "no-std", "parser-implementations"] categories = ["data-structures", "no-std", "parser-implementations"]

View File

@ -33,8 +33,7 @@ fn main() {
let entries = archive.entries().collect::<Vec<_>>(); let entries = archive.entries().collect::<Vec<_>>();
println!("{:#?}", entries); println!("{:#?}", entries);
println!("content of last file:"); println!("content of last file:");
let last_file_content = unsafe { core::str::from_utf8_unchecked(entries[2].data()) }; println!("{:#?}", entries[2].data_as_str().expect("Invalid UTF-8") );
println!("{:#?}", last_file_content);
} }
``` ```

View File

@ -35,6 +35,5 @@ fn main() {
let entries = archive.entries().collect::<Vec<_>>(); let entries = archive.entries().collect::<Vec<_>>();
println!("{:#?}", entries); println!("{:#?}", entries);
println!("content of last file:"); println!("content of last file:");
let last_file_content = unsafe { core::str::from_utf8_unchecked(entries[2].data()) }; println!("{:#?}", entries[2].data_as_str().expect("Invalid UTF-8") );
println!("{:#?}", last_file_content);
} }

View File

@ -27,7 +27,7 @@ use crate::header::PosixHeader;
use crate::{TypeFlag, BLOCKSIZE}; use crate::{TypeFlag, BLOCKSIZE};
use arrayvec::ArrayString; use arrayvec::ArrayString;
use core::fmt::{Debug, Formatter}; use core::fmt::{Debug, Formatter};
use core::str::FromStr; use core::str::{FromStr, Utf8Error};
/// Describes an entry in an archive. /// Describes an entry in an archive.
/// Currently only supports files but no directories. /// Currently only supports files but no directories.
@ -57,6 +57,11 @@ impl<'a> ArchiveEntry<'a> {
self.data 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. /// Filesize in bytes.
pub const fn size(&self) -> usize { pub const fn size(&self) -> usize {
self.size self.size
@ -183,7 +188,6 @@ impl<'a> Iterator for ArchiveIterator<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use core::str;
use std::vec::Vec; use std::vec::Vec;
#[test] #[test]
@ -233,7 +237,7 @@ mod tests {
assert_eq!(entries[0].size(), 513); assert_eq!(entries[0].size(), 513);
assert_eq!(entries[0].data().len(), 513); assert_eq!(entries[0].data().len(), 513);
assert_eq!( 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") include_str!("../tests/bye_world_513b.txt")
); );
@ -241,7 +245,7 @@ mod tests {
assert_eq!(entries[1].size(), 513); assert_eq!(entries[1].size(), 513);
assert_eq!(entries[1].data().len(), 513); assert_eq!(entries[1].data().len(), 513);
assert_eq!( 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") include_str!("../tests/hello_world_513b.txt")
); );
@ -249,7 +253,7 @@ mod tests {
assert_eq!(entries[2].size(), 12); assert_eq!(entries[2].size(), 12);
assert_eq!(entries[2].data().len(), 12); assert_eq!(entries[2].data().len(), 12);
assert_eq!( assert_eq!(
unsafe { str::from_utf8_unchecked(entries[2].data) }, entries[2].data_as_str().expect("Invalid UTF-8"),
"Hello World\n", "Hello World\n",
"file content must match" "file content must match"
); );