darling_core 0.24.0

Helper crate for proc-macro library for reading attributes into structs when implementing custom derives. Use https://crates.io/crates/darling in your code.
Documentation
use std::borrow::Cow;

/// Transform Rust paths to a readable and comparable string.
///
/// # Limitations
/// * Leading colons are ignored.
/// * Angle brackets and `as` elements are ignored.
///
/// # Example
/// ```rust
/// # use darling_core::util::path_to_string;
/// # use syn::parse_quote;
/// assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b");
/// ```
pub fn path_to_string(path: &syn::Path) -> String {
    path.segments
        .iter()
        // like `.intersperse("::")`, but that's not stable yet
        .enumerate()
        .flat_map(|(i, s)| {
            if i == 0 {
                [Cow::Borrowed(""), s.ident.to_string().into()]
            } else {
                [Cow::Borrowed("::"), s.ident.to_string().into()]
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use syn::parse_quote;

    use super::path_to_string;

    #[test]
    fn simple_ident() {
        assert_eq!(path_to_string(&parse_quote!(a)), "a");
    }

    #[test]
    fn simple_path() {
        assert_eq!(path_to_string(&parse_quote!(a::b)), "a::b");
    }
}