Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Add EntryReference #235

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/src/archive/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ mod meta;
mod name;
mod options;
mod read;
mod reference;
mod write;

pub use self::{builder::*, header::*, meta::*, name::*, options::*};
pub use self::{builder::*, header::*, meta::*, name::*, options::*, reference::*};
use self::{private::*, read::*, write::*};
use crate::{
chunk::{
Expand Down
87 changes: 87 additions & 0 deletions lib/src/archive/entry/reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::util::try_to_string;
use std::borrow::Cow;
use std::path::{Component, Path};

/// A UTF-8 encoded entry reference.
///
/// ## Examples
/// ```
/// use libpna::EntryReference;
///
/// assert_eq!("uer/bin", EntryReference::try_from("uer/bin").unwrap().as_str());
/// assert_eq!("/user/bin", EntryReference::try_from("/user/bin").unwrap().as_str());
/// assert_eq!("/user/bin", EntryReference::try_from("/user/bin/").unwrap().as_str());
/// assert_eq!("../user/bin", EntryReference::try_from("../user/bin/").unwrap().as_str());
/// assert_eq!("/", EntryReference::try_from("/").unwrap().as_str());
/// ```
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct EntryReference(String);

impl EntryReference {
fn new(path: &Path) -> Result<Self, ()> {
let has_root = path.has_root();
let mut components = path.components();
if has_root {
components.next();
};
let p = components
.map(|it| match it {
Component::Prefix(p) => try_to_string(p.as_os_str()),
Component::RootDir => unreachable!(),
Component::CurDir => Ok(Cow::from(".")),
Component::ParentDir => Ok(Cow::from("..")),
Component::Normal(n) => try_to_string(n),
})
.collect::<Result<Vec<_>, _>>()
.map_err(|_| ())?;
let mut s = p.join("/");
if has_root {
s.insert(0, '/');
};
Ok(Self(s))
}

/// Extracts a string slice containing the entire [EntryReference].
/// ## Examples
/// Basic usage:
/// ```
/// use libpna::EntryReference;
/// let r = EntryReference::try_from("foo").unwrap();
///
/// assert_eq!("foo", r.as_str());
/// ```
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl TryFrom<&str> for EntryReference {
type Error = ();

/// ## Examples
/// ```
/// use libpna::EntryReference;
///
/// assert_eq!(EntryReference::try_from("/path/with/root"), EntryReference::try_from("/path/with/root"));
/// ```
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value.as_ref())
}
}

impl TryFrom<&Path> for EntryReference {
type Error = ();

/// ## Examples
/// ```
/// use std::path::Path;
/// use libpna::EntryReference;
///
/// let p = Path::new("path/to/file");
/// assert_eq!(EntryReference::try_from(p), EntryReference::try_from("path/to/file"));
/// ```
fn try_from(value: &Path) -> Result<Self, Self::Error> {
Self::new(value)
}
}