flow-types 0.1.0-rc.5

The Flow declaration model with no values: types, declarations and their identities, record and enum shapes, and which types decoding may build.
Documentation
//! The normal form of std's `path.Path` (`stdlib.md`, *`path`*), which
//! `path.of` makes and decoding holds a `Path` to (`abi.md`, *Decoding*).

/// The normal text of the path `text` names: `text` split on `/`, without
/// its empty and `.` components and keeping every `..`, joined by `/` after
/// a `/` when `text` starts with one; `.`, or `/` when absolute, when no
/// component is left. Every text names a path, so this never fails.
///
/// ```
/// assert_eq!(flow_types::normal_path("a//b/./c/"), "a/b/c");
/// assert_eq!(flow_types::normal_path("/../x"), "/../x");
/// assert_eq!(flow_types::normal_path(""), ".");
/// ```
pub fn normal_path(text: &str) -> String {
    let absolute = text.starts_with('/');
    let parts: Vec<&str> = text
        .split('/')
        .filter(|part| !part.is_empty() && *part != ".")
        .collect();
    let joined = parts.join("/");
    match (absolute, joined.is_empty()) {
        (true, _) => format!("/{joined}"),
        (false, true) => ".".to_owned(),
        (false, false) => joined,
    }
}

#[cfg(test)]
#[cfg_attr(coverage, coverage(off))]
mod tests {
    use super::*;

    #[test]
    fn the_normal_form_drops_empty_and_dot_components_and_keeps_every_dot_dot() {
        for (text, normal) in [
            ("a//b/./c/", "a/b/c"),
            ("", "."),
            (".", "."),
            ("/", "/"),
            ("//./", "/"),
            ("/../x", "/../x"),
            ("a/../b", "a/../b"),
            ("a\0b", "a\0b"),
        ] {
            assert_eq!(normal_path(text), normal, "{text:?}");
            assert_eq!(normal_path(normal), normal, "{normal:?}");
        }
        for spelled in ["a//b", "", "a/"] {
            assert_ne!(normal_path(spelled), spelled, "{spelled:?}");
        }
    }
}