Skip to content

Commit

Permalink
Move path construction back to MetaForm for non-breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Oct 7, 2022
1 parent 192d12c commit 403c720
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::prelude::{
Error as FmtError,
Formatter,
},
vec,
vec::Vec,
};

Expand Down Expand Up @@ -102,29 +101,6 @@ impl Path<MetaForm> {
Self::from_segments(segments)
.expect("All path segments should be valid Rust identifiers")
}
}

impl<T> Path<T>
where
T: Form,
{
/// Create an empty path for types which shall not be named
#[allow(unused)]
pub(crate) fn voldemort() -> Self {
Self {
segments: Vec::new(),
}
}

/// Crate a Path for types in the Prelude namespace
///
/// # Panics
///
/// - If the supplied ident is not a valid Rust identifier
pub(crate) fn prelude(ident: T::String) -> Self {
Self::from_segments(vec![ident.clone()])
.unwrap_or_else(|_| panic!("{:?} is not a valid Rust identifier", ident))
}

/// Create a Path from the given segments
///
Expand All @@ -133,22 +109,45 @@ where
/// - If no segments are supplied
/// - If any of the segments are invalid Rust identifiers
pub fn from_segments<I>(segments: I) -> Result<Self, PathError>
where
I: IntoIterator<Item = T::String>,
where
I: IntoIterator<Item = <MetaForm as Form>::String>,
{
let segments = segments.into_iter().collect::<Vec<_>>();
if segments.is_empty() {
return Err(PathError::MissingSegments)
}
if let Some(err_at) = segments
.iter()
.position(|seg| !is_rust_identifier(<T::String as AsRef<str>>::as_ref(seg)))
.position(|seg| !is_rust_identifier(seg))
{
return Err(PathError::InvalidIdentifier { segment: err_at })
}
Ok(Path { segments })
}

/// Crate a Path for types in the Prelude namespace
///
/// # Panics
///
/// - If the supplied ident is not a valid Rust identifier
pub(crate) fn prelude(ident: <MetaForm as Form>::String) -> Self {
Self::from_segments([ident])
.unwrap_or_else(|_| panic!("{:?} is not a valid Rust identifier", ident))
}
}

impl<T> Path<T>
where
T: Form,
{
/// Create an empty path for types which shall not be named
#[allow(unused)]
pub(crate) fn voldemort() -> Self {
Self {
segments: Vec::new(),
}
}

/// Create a Path from the given segments.
///
/// Does *not* check that the segments are valid Rust identifiers.
Expand Down

0 comments on commit 403c720

Please sign in to comment.