Skip to content

Commit

Permalink
better names for some 'parse' methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Dec 31, 2023
1 parent 8999add commit 8dc557f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 83 deletions.
22 changes: 11 additions & 11 deletions src/location/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct Component {
}

impl Component {
pub fn parse(input: &str) -> Result<&Self, InvalidComponent> {
pub fn new(input: &str) -> Result<&Self, InvalidComponent> {
if input.is_empty() {
Err(InvalidComponent::Empty)?;
}
Expand All @@ -64,8 +64,8 @@ impl Component {
Ok(Self::from_ref_unchecked(input))
}

pub fn parse_boxed(input: Box<str>) -> Result<Box<Self>, InvalidComponent> {
Self::parse(input.as_ref())?;
pub fn new_boxed(input: Box<str>) -> Result<Box<Self>, InvalidComponent> {
Self::new(input.as_ref())?;
Ok(Self::from_box_unchecked(input))
}

Expand Down Expand Up @@ -104,23 +104,23 @@ impl<'a> From<&'a Component> for Box<Component> {

impl PartialEq<str> for Component {
fn eq(&self, other: &str) -> bool {
Self::parse(other).map_or(false, |other| self == other)
Self::new(other).map_or(false, |other| self == other)
}
}

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

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

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

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

Expand Down Expand Up @@ -156,29 +156,29 @@ mod test {

#[test]
fn valid_alphanumeric() {
let component = Component::parse("hell0").unwrap();
let component = Component::new("hell0").unwrap();
assert_eq!(component.raw_contents(), "hell0");
}

#[test]
fn valid_slug() {
let component = Component::parse("hello-world").unwrap();
let component = Component::new("hello-world").unwrap();
assert_eq!(component.raw_contents(), "hello-world");
}

#[test]
fn valid_with_spaces_and_punct() {
let component = Component::parse("Hello, world!").unwrap();
let component = Component::new("Hello, world!").unwrap();
assert_eq!(component.raw_contents(), "Hello, world!");
}

#[test]
fn invalid_bar() {
Component::parse("ha/he").unwrap_err();
Component::new("ha/he").unwrap_err();
}

#[test]
fn invalid_hash() {
Component::parse("ha#he").unwrap_err();
Component::new("ha#he").unwrap_err();
}
}
24 changes: 12 additions & 12 deletions src/location/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,58 +93,58 @@ pub trait LocationStrExt {

impl LocationStrExt for str {
fn try_id(&self) -> Result<&Id, InvalidId> {
Id::parse(self)
Id::new(self)
}

fn try_component(&self) -> Result<&Component, InvalidComponent> {
Component::parse(self)
Component::new(self)
}

fn try_path(&self) -> Result<&Path, InvalidPath> {
Path::parse(self)
Path::new(self)
}

fn try_internal_loc(&self) -> Result<&Internal, InvalidInternal> {
Internal::parse(self)
Internal::new(self)
}

fn try_external_loc(&self) -> Result<&External, InvalidExternal> {
External::parse(self)
External::new(self)
}

fn try_location(&self) -> Result<&Location, InvalidLocation> {
Location::parse(self)
Location::new(self)
}

fn try_into_id(self: Box<Self>) -> Result<Box<Id>, InvalidId> {
Id::parse_boxed(self)
Id::new_boxed(self)
}

fn try_into_component(
self: Box<Self>,
) -> Result<Box<Component>, InvalidComponent> {
Component::parse_boxed(self)
Component::new_boxed(self)
}

fn try_into_path(self: Box<Self>) -> Result<Box<Path>, InvalidPath> {
Path::parse_boxed(self)
Path::new_boxed(self)
}

fn try_into_internal_loc(
self: Box<Self>,
) -> Result<Box<Internal>, InvalidInternal> {
Internal::parse_boxed(self)
Internal::new_boxed(self)
}

fn try_into_external_loc(
self: Box<Self>,
) -> Result<Box<External>, InvalidExternal> {
External::parse_boxed(self)
External::new_boxed(self)
}

fn try_into_location(
self: Box<Self>,
) -> Result<Box<Location>, InvalidLocation> {
Location::parse_boxed(self)
Location::new_boxed(self)
}
}
12 changes: 6 additions & 6 deletions src/location/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ pub struct External {
}

impl External {
pub fn parse(input: &str) -> Result<&Self, InvalidExternal> {
pub fn new(input: &str) -> Result<&Self, InvalidExternal> {
let (external_loc, _) = parse(input)?;
Ok(external_loc)
}

pub fn parse_boxed(input: Box<str>) -> Result<Box<Self>, InvalidExternal> {
Self::parse(input.as_ref())?;
pub fn new_boxed(input: Box<str>) -> Result<Box<Self>, InvalidExternal> {
Self::new(input.as_ref())?;
Ok(Self::from_box_unchecked(input))
}

Expand Down Expand Up @@ -126,23 +126,23 @@ impl<'a> From<&'a External> for Box<External> {
impl PartialEq<str> for External {
fn eq(&self, other: &str) -> bool {
Self::parse(other).map_or(false, |other| self == other)
Self::new(other).map_or(false, |other| self == other)
}
}
impl<'input> TryFrom<&'input str> for &'input External {
type Error = InvalidExternal;
fn try_from(input: &'input str) -> Result<Self, Self::Error> {
External::parse(input)
External::new(input)
}
}
impl TryFrom<Box<str>> for Box<External> {
type Error = InvalidExternal;
fn try_from(input: Box<str>) -> Result<Self, Self::Error> {
External::parse_boxed(input)
External::new_boxed(input)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/location/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ pub fn parse<'a>(
input: &'a str,
) -> Result<(&'a Location, ViewRef<'a>), InvalidLocation> {
let view = if input.contains("://") {
let external = External::parse(input)?;
let external = External::new(input)?;
ViewRef::External(external)
} else {
let internal = Internal::parse(input)?;
let internal = Internal::new(input)?;
ViewRef::Internal(internal)
};
let location = Location::from_ref_unchecked(input);
Expand All @@ -57,13 +57,13 @@ pub struct Location {
}

impl Location {
pub fn parse(input: &str) -> Result<&Self, InvalidLocation> {
pub fn new(input: &str) -> Result<&Self, InvalidLocation> {
let (external_loc, _) = parse(input)?;
Ok(external_loc)
}

pub fn parse_boxed(input: Box<str>) -> Result<Box<Self>, InvalidLocation> {
Self::parse(input.as_ref())?;
pub fn new_boxed(input: Box<str>) -> Result<Box<Self>, InvalidLocation> {
Self::new(input.as_ref())?;
Ok(Self::from_box_unchecked(input))
}

Expand Down Expand Up @@ -155,23 +155,23 @@ impl<'a> From<&'a Location> for Box<Location> {

impl PartialEq<str> for Location {
fn eq(&self, other: &str) -> bool {
Self::parse(other).map_or(false, |other| self == other)
Self::new(other).map_or(false, |other| self == other)
}
}

impl<'input> TryFrom<&'input str> for &'input Location {
type Error = InvalidLocation;

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

impl TryFrom<Box<str>> for Box<Location> {
type Error = InvalidLocation;

fn try_from(input: Box<str>) -> Result<Self, Self::Error> {
Location::parse_boxed(input)
Location::new_boxed(input)
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/location/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Id {
}

impl Id {
pub fn parse(input: &str) -> Result<&Self, InvalidId> {
pub fn new(input: &str) -> Result<&Self, InvalidId> {
let mut iter = input.chars();

let ch = iter.next().ok_or(InvalidId::Empty)?;
Expand All @@ -52,8 +52,8 @@ impl Id {
Ok(Self::from_ref_unchecked(input))
}

pub fn parse_boxed(input: Box<str>) -> Result<Box<Self>, InvalidId> {
Self::parse(input.as_ref())?;
pub fn new_boxed(input: Box<str>) -> Result<Box<Self>, InvalidId> {
Self::new(input.as_ref())?;
Ok(Self::from_box_unchecked(input))
}

Expand Down Expand Up @@ -93,23 +93,23 @@ impl<'a> From<&'a Id> for Box<Id> {

impl PartialEq<str> for Id {
fn eq(&self, other: &str) -> bool {
Self::parse(other).map_or(false, |other| self == other)
Self::new(other).map_or(false, |other| self == other)
}
}

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

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

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

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

Expand Down Expand Up @@ -145,28 +145,28 @@ mod test {

#[test]
fn valid_alphanumeric() {
let id = Id::parse("hell0").unwrap();
let id = Id::new("hell0").unwrap();
assert_eq!(id.raw_contents(), "hell0");
}

#[test]
fn valid_slug() {
let id = Id::parse("hello-world_yahoo").unwrap();
let id = Id::new("hello-world_yahoo").unwrap();
assert_eq!(id.raw_contents(), "hello-world_yahoo");
}

#[test]
fn invalid_space() {
Id::parse("h a").unwrap_err();
Id::new("h a").unwrap_err();
}

#[test]
fn invalid_bar() {
Id::parse("ha/he").unwrap_err();
Id::new("ha/he").unwrap_err();
}

#[test]
fn invalid_hash() {
Id::parse("ha#he").unwrap_err();
Id::new("ha#he").unwrap_err();
}
}
Loading

0 comments on commit 8dc557f

Please sign in to comment.