Skip to content

Commit

Permalink
amend me later
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Sep 12, 2023
1 parent 32ae09a commit 17d514a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 424 deletions.
4 changes: 3 additions & 1 deletion src/location.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod dsl;
pub mod id;
pub mod component;
pub mod path;
/*
pub mod internal;
pub mod external;
pub mod general;
pub mod dsl;
*/
83 changes: 16 additions & 67 deletions src/location/component.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, fmt, mem, rc::Rc, sync::Arc};
use std::{error::Error, fmt, mem};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidComponent {
Expand Down Expand Up @@ -42,24 +42,6 @@ pub struct Component {

impl Component {
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>, InvalidComponent> {
Self::check_parse_input(input.as_ref())?;
Ok(Self::from_box(input))
}

fn from_ref(input: &str) -> &Self {
unsafe { mem::transmute(input) }
}

fn from_box(input: Box<str>) -> Box<Self> {
unsafe { mem::transmute(input) }
}

fn check_parse_input(input: &str) -> Result<(), InvalidComponent> {
if input.is_empty() {
Err(InvalidComponent::Empty)?;
}
Expand All @@ -79,7 +61,20 @@ impl Component {
}
}

Ok(())
Ok(Self::from_ref_unchecked(input))
}

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

pub(crate) const fn from_ref_unchecked(input: &str) -> &Self {
unsafe { mem::transmute(input) }
}

pub(crate) const fn from_box_unchecked(input: Box<str>) -> Box<Self> {
unsafe { mem::transmute(input) }
}
}

Expand All @@ -95,52 +90,6 @@ impl TryFrom<Box<str>> for Box<Component> {
type Error = InvalidComponent;

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

pub trait AsComponent {
fn as_component(&self) -> &Component;
}

impl AsComponent for Component {
fn as_component(&self) -> &Component {
self
}
}

impl<'this, C> AsComponent for &'this C
where
C: AsComponent + ?Sized,
{
fn as_component(&self) -> &Component {
(**self).as_component()
}
}

impl<C> AsComponent for Box<C>
where
C: AsComponent + ?Sized,
{
fn as_component(&self) -> &Component {
(**self).as_component()
}
}

impl<C> AsComponent for Rc<C>
where
C: AsComponent + ?Sized,
{
fn as_component(&self) -> &Component {
(**self).as_component()
}
}

impl<C> AsComponent for Arc<C>
where
C: AsComponent + ?Sized,
{
fn as_component(&self) -> &Component {
(**self).as_component()
Component::parse_boxed(input)
}
}
83 changes: 16 additions & 67 deletions src/location/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, fmt, mem, rc::Rc, sync::Arc};
use std::{error::Error, fmt, mem};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidId {
Expand Down Expand Up @@ -36,24 +36,6 @@ pub struct Id {

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

pub fn parse_owned(input: Box<str>) -> Result<Box<Self>, InvalidId> {
Self::check_parse_input(input.as_ref())?;
Ok(Self::from_box(input))
}

fn from_ref(input: &str) -> &Self {
unsafe { mem::transmute(input) }
}

fn from_box(input: Box<str>) -> Box<Self> {
unsafe { mem::transmute(input) }
}

fn check_parse_input(input: &str) -> Result<(), InvalidId> {
let mut iter = input.chars();

let ch = iter.next().ok_or(InvalidId::Empty)?;
Expand All @@ -67,7 +49,20 @@ impl Id {
}
}

Ok(())
Ok(Self::from_ref_unchecked(input))
}

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

pub(crate) const fn from_ref_unchecked(input: &str) -> &Self {
unsafe { mem::transmute(input) }
}

pub(crate) const fn from_box_unchecked(input: Box<str>) -> Box<Self> {
unsafe { mem::transmute(input) }
}
}

Expand All @@ -83,52 +78,6 @@ 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;
}

impl AsId for Id {
fn as_id(&self) -> &Id {
self
}
}

impl<'this, I> AsId for &'this I
where
I: AsId + ?Sized,
{
fn as_id(&self) -> &Id {
(**self).as_id()
}
}

impl<I> AsId for Box<I>
where
I: AsId + ?Sized,
{
fn as_id(&self) -> &Id {
(**self).as_id()
}
}

impl<I> AsId for Rc<I>
where
I: AsId + ?Sized,
{
fn as_id(&self) -> &Id {
(**self).as_id()
}
}

impl<I> AsId for Arc<I>
where
I: AsId + ?Sized,
{
fn as_id(&self) -> &Id {
(**self).as_id()
Id::parse_boxed(input)
}
}
Loading

0 comments on commit 17d514a

Please sign in to comment.