Skip to content

Commit

Permalink
trying to make a good path API
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Sep 12, 2023
1 parent 1b670bd commit ca121d7
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 9 deletions.
79 changes: 74 additions & 5 deletions src/location/component.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
use std::{mem, rc::Rc, sync::Arc};
use std::{error::Error, fmt, mem, rc::Rc, sync::Arc};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidComponent {
Empty,
CurrentDir,
ParentDir,
Bar,
Hash,
}

impl fmt::Display for InvalidComponent {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Empty => write!(fmtr, "Path component cannot be empty"),
Self::CurrentDir => {
write!(
fmtr,
"Path compoonent cannot reference current directory"
)
},
Self::ParentDir => {
write!(fmtr, "Path component cannot reference parent directory")
},
Self::Bar => {
write!(fmtr, "Path component cannot contain a bar '/'")
},
Self::Hash => {
write!(fmtr, "Path component cannot contain a hash '#'")
},
}
}
}

impl Error for InvalidComponent {}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand All @@ -7,12 +41,12 @@ pub struct Component {
}

impl Component {
pub fn parse(input: &str) -> Result<&Self, InvalidId> {
pub fn parse(input: &str) -> Result<&Self, InvalidComponent> {
Self::check_parse_input(input)?;
Ok(Self::from_ref(input))
}

pub fn parse_owned(input: Box<str>) -> Result<Box<Self>, InvalidId> {
pub fn parse_owned(input: Box<str>) -> Result<Box<Self>, InvalidComponent> {
Self::check_parse_input(input.as_ref())?;
Ok(Self::from_box(input))
}
Expand All @@ -25,8 +59,43 @@ impl Component {
unsafe { mem::transmute(input) }
}

fn check_parse_input(input: &str) -> Result<(), InvalidId> {
todo!()
fn check_parse_input(input: &str) -> Result<(), InvalidComponent> {
if input.is_empty() {
Err(InvalidComponent::Empty)?;
}
if input == "." {
Err(InvalidComponent::CurrentDir)?;
}
if input == ".." {
Err(InvalidComponent::ParentDir)?;
}

for ch in input.chars() {
if ch == '/' {
Err(InvalidComponent::Bar)?;
}
if ch == '#' {
Err(InvalidComponent::Hash)?;
}
}

Ok(())
}
}

impl<'input> TryFrom<&'input str> for &'input Component {
type Error = InvalidComponent;

fn try_from(input: &'input str) -> Result<Self, Self::Error> {
Component::parse(input)
}
}

impl TryFrom<Box<str>> for Box<Component> {
type Error = InvalidComponent;

fn try_from(input: Box<str>) -> Result<Self, Self::Error> {
Component::parse_owned(input)
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/location/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ impl Id {
}
}

impl<'input> TryFrom<&'input str> for &'input Id {
type Error = InvalidId;

fn try_from(input: &'input str) -> Result<Self, Self::Error> {
Id::parse(input)
}
}

impl TryFrom<Box<str>> for Box<Id> {
type Error = InvalidId;

fn try_from(input: Box<str>) -> Result<Self, Self::Error> {
Id::parse_owned(input)
}
}

pub trait AsId {
fn as_id(&self) -> &Id;
}
Expand Down
Empty file added src/location/parse.rs
Empty file.
Loading

0 comments on commit ca121d7

Please sign in to comment.