Skip to content

Commit

Permalink
Adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Sep 2, 2024
1 parent 9c817a6 commit 2c71f5f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 40 additions & 3 deletions src/relative_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ pub(crate) enum Error {
LeadingSlash,
#[snafu(display("trailing slash"))]
TrailingSlash,
#[snafu(display("Windows disk prefix `{letter}:`"))]
DiskPrefix {
letter: char,
},
#[snafu(display("double slash"))]
DoubleSlash,
#[snafu(display("illegal character `{}`", character.escape_default()))]
#[snafu(display("illegal character `{character}`"))]
Character {
character: char,
},
Expand Down Expand Up @@ -151,6 +155,15 @@ impl FromStr for RelativePath {
return Err(Error::DoubleSlash);
}

let mut chars = s.chars();
let first = chars.next();
let second = chars.next();
if let Some((first, second)) = first.zip(second) {
if second == ':' {
return Err(Error::DiskPrefix { letter: first });
}
}

let mut path = String::new();

for (i, component) in s.split('/').enumerate() {
Expand Down Expand Up @@ -192,12 +205,25 @@ mod tests {
use super::*;

#[test]
fn errors() {
fn from_str() {
#[track_caller]
fn case(path: &str, expected: &str) {
assert_eq!(path.parse::<RelativePath>().unwrap(), expected);
}

case("foo", "foo");
case("foo/bar", "foo/bar");
}

#[test]
fn from_str_errors() {
#[track_caller]
fn case(path: &str, expected: Error) {
assert_eq!(path.parse::<RelativePath>().unwrap_err(), expected);
}

case("C:", Error::DiskPrefix { letter: 'C' });

case("", Error::Empty);
case(
".",
Expand All @@ -212,11 +238,22 @@ mod tests {
},
);
case("/", Error::LeadingSlash);
case("foo/", Error::TrailingSlash);
case("foo//bar", Error::DoubleSlash);
case("\\", Error::Character { character: '\\' });
}

#[test]
fn check_portability() {
fn portability() {
"foo"
.parse::<RelativePath>()
.unwrap()
.check_portability()
.unwrap();
}

#[test]
fn portability_errors() {
#[track_caller]
fn case(path: &str, expected: Error) {
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn backslash_error() {
"error: invalid path `\\`
because:
- illegal character `\\\\`
- illegal character `\\`
",
)
.failure();
Expand Down

0 comments on commit 2c71f5f

Please sign in to comment.