Skip to content

Manipulating path segments #188

Closed
Closed

Description

@SimonSapin

Url currently has pop_path_segments and push_path_segment methods, but they have a design problem reported by @sfackler. A path is at minimum / which is considered like a single path segment that happens to be empty (like trailing slashes in general), so push_path_segment("foo") make the path be //foo with two slashes.

Fixing this may involve breaking API changes, so instead I’ll make these methods private (effectively removing them) before publishing 1.0.0, in order to unblock it. I’ll add them back (or something else to provide this functionality) in 1.1.0.

Activity

  1. sfackler commented on Apr 21, 2016

    @sfackler
    Contributor

    In the meantime, what's the right way to add path segments? Something like

    let mut url = ...;
    url.set_path(&format!("{}/{}", url.path(), percent_encode(new_component.as_bytes(), PATH_SEGMENT_ENCODE_SET);
    

    ?

  2. sfackler commented on Apr 21, 2016

    @sfackler
    Contributor

    I guess it would actually need to check if url.path() == "/" to avoid the empty path problem.

  3. SimonSapin commented on Apr 22, 2016

    @SimonSapin
    MemberAuthor

    Yes, something like that.

  4. mhristache commented on Apr 24, 2016

    @mhristache
    Contributor

    I guess the current issue is related so I am posting here. Let me know if I should open a new issue.

    I am trying to switch to url 1.0.0 but I cannot find the equivalent of the url 0.5 code below:

    let mut path = url.path_mut().unwrap();
    path.push(segment.to_owned());

    I am trying to append a segment to a path which doesn't end with a trailing / and url.join() cannot handle this scenario. Is there a way to do this with url 1.0.0 or I need to do some ugly string concat to make sure a trailing / is always present and then use url.join()?

    Thanks

  5. SimonSapin commented on Apr 24, 2016

    @SimonSapin
    MemberAuthor

    In 1.0.0 you have to use string concatenation and url.set_path(p). In 1.1.0 I want to add something like url.push_path_segment(s) (but maybe a different API) which would do the right thing in various situations.

  6. jgillich commented on Apr 27, 2016

    @jgillich
    Contributor

    The example by @sfackler doens't appear to work, you'd have to create a String out of url.path() first, which is not great.

    src/transport.rs:46:40: 46:43 error: cannot borrow `url` as immutable because it is also borrowed as mutable [E0502]
    src/transport.rs:46         url.set_path(&format!("{}/{}", url.path(), path));
                                                               ^~~
    <std macros>:2:26: 2:57 note: in this expansion of format_args!
    src/transport.rs:46:23: 46:57 note: in this expansion of format! (defined in <std macros>)
    src/transport.rs:46:9: 46:12 note: previous borrow of `url` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `url` until the borrow ends
    src/transport.rs:46         url.set_path(&format!("{}/{}", url.path(), path));
                                ^~~
    
  7. SimonSapin commented on Apr 27, 2016

    @SimonSapin
    MemberAuthor

    Yeah, for now you have to move the format!(…) expression into a separate variable.

    The API I have in mind would be something like this:

    impl Url {
        fn path_segments_mut(&mut self) -> PathSegmentsMut { … }
    }
    struct PathSegmentsMut<'a> {
        url: &'a mut Url
    }
    impl<'a> PathSegmentsMut<'a> {
        fn iter(&self) -> PathSegments { … }
        fn clear(&mut self) { … }
        fn pop(&mut self) { … }
        fn push(&mut self, s: &str) { … }
    }
  8. added a commit that references this issue on May 2, 2016
    52ce55f
  9. added a commit that references this issue on May 3, 2016
    d91d175
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions