This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
156 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use core::fmt; | ||
|
||
use crate::{ | ||
str::Str, | ||
value::{FromValue, ToValue, Value}, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct Path<'a>(Str<'a>); | ||
|
||
impl<'a> From<&'a str> for Path<'a> { | ||
fn from(value: &'a str) -> Self { | ||
Path(Str::from(value)) | ||
} | ||
} | ||
|
||
impl<'a> From<Str<'a>> for Path<'a> { | ||
fn from(value: Str<'a>) -> Self { | ||
Path(value) | ||
} | ||
} | ||
|
||
impl<'a> ToValue for Path<'a> { | ||
fn to_value(&self) -> Value { | ||
self.0.to_value() | ||
} | ||
} | ||
|
||
impl<'a> FromValue<'a> for Path<'a> { | ||
fn from_value(value: Value<'a>) -> Option<Self> { | ||
Some(value.cast()?) | ||
} | ||
} | ||
|
||
impl Path<'static> { | ||
pub const fn new(source: &'static str) -> Self { | ||
Path(Str::new(source)) | ||
} | ||
} | ||
|
||
impl<'a> Path<'a> { | ||
pub const fn new_ref(source: &'a str) -> Self { | ||
Path(Str::new_ref(source)) | ||
} | ||
|
||
pub fn by_ref<'b>(&'b self) -> Path<'b> { | ||
Path(self.0.by_ref()) | ||
} | ||
|
||
pub fn segments(&self) -> impl Iterator<Item = &str> { | ||
self.0.as_str().split("::") | ||
} | ||
|
||
pub fn is_child_of<'b>(&self, other: &Path<'b>) -> bool { | ||
let child = self.0.as_str(); | ||
let parent = other.0.as_str(); | ||
|
||
if child.len() >= parent.len() && child.is_char_boundary(parent.len()) { | ||
let (child_prefix, child_suffix) = child.split_at(parent.len()); | ||
|
||
child_prefix == parent && (child_suffix.is_empty() || child_suffix.starts_with("::")) | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
impl<'a> fmt::Debug for Path<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(&self.0, f) | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for Path<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Display::fmt(&self.0, f) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn is_child_of() { | ||
let a = Path::new("a"); | ||
let aa = Path::new("aa"); | ||
let b = Path::new("b"); | ||
let a_b = Path::new("a::b"); | ||
|
||
assert!(!aa.is_child_of(&a)); | ||
assert!(!b.is_child_of(&a)); | ||
assert!(!a.is_child_of(&a_b)); | ||
|
||
assert!(a.is_child_of(&a)); | ||
assert!(a_b.is_child_of(&a)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ mod hook; | |
mod key; | ||
mod optional; | ||
mod props; | ||
mod source; | ||
mod span; | ||
mod template; | ||
mod util; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use proc_macro2::TokenStream; | ||
|
||
pub(crate) fn source_tokens() -> TokenStream { | ||
quote!(emit::__private::__private_module!()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.