ts-webapi 0.4.11

Library for my web API projects
Documentation
//! URI helpers

use http::Uri;

/// Extension trait for URIs
pub trait UriExt {
    /// Get the path segment by index:
    ///
    /// ```notrust
    /// http://localhost:4000/some-route/sub-route
    ///                      /|--------|/|-------|
    ///                        index 0    index 1
    /// ```
    fn get_path_segment(&self, index: usize) -> Option<&str>;
}

impl UriExt for Uri {
    fn get_path_segment(&self, index: usize) -> Option<&str> {
        self.path().split('/').nth(index + 1)
    }
}

#[cfg(test)]
mod test {
    use http::Uri;

    use crate::uri::UriExt;

    #[test]
    fn gets_corect_path_segment() {
        let uri = Uri::from_static("http://localhost:4000/some-route/some-sub-route//bad-route/");
        assert_eq!(Some("some-route"), uri.get_path_segment(0));
        assert_eq!(Some("some-sub-route"), uri.get_path_segment(1));
        assert_eq!(Some(""), uri.get_path_segment(2));
        assert_eq!(Some("bad-route"), uri.get_path_segment(3));
        assert_eq!(Some(""), uri.get_path_segment(4));
        assert_eq!(None, uri.get_path_segment(5));
    }
}