convenient "data_as_str" getter (v0.1.4)

no-rustdoc v0.1.4
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
collections of files.
"""
version = "0.1.3"
version = "0.1.4"
edition = "2018"
keywords = ["tar", "tarball", "archive"]
categories = ["data-structures", "no-std", "parser-implementations"]

View File

@ -33,8 +33,7 @@ fn main() {
let entries = archive.entries().collect::<Vec<_>>();
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") );
}
```

View File

@ -35,6 +35,5 @@ fn main() {
let entries = archive.entries().collect::<Vec<_>>();
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") );
}

View File

@ -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"
);