@@ -107,13 +107,17 @@ impl<P> PathLikeWithPosition<P> {
/// Parses a string that possibly has `:row:column` suffix.
/// Ignores trailing `:`s, so `test.rs:22:` is parsed as `test.rs:22`.
/// If any of the row/column component parsing fails, the whole string is then parsed as a path like.
+ /// If on Windows, `s` will replace `/` with `\` for compatibility.
pub fn parse_str<E>(
s: &str,
- parse_path_like_str: impl Fn(&str) -> Result<P, E>,
+ parse_path_like_str: impl Fn(&str, &str) -> Result<P, E>,
) -> Result<Self, E> {
+ #[cfg(target_os = "windows")]
+ let s = &s.replace('/', "\\");
+
let fallback = |fallback_str| {
Ok(Self {
- path_like: parse_path_like_str(fallback_str)?,
+ path_like: parse_path_like_str(s, fallback_str)?,
row: None,
column: None,
})
@@ -125,7 +129,7 @@ impl<P> PathLikeWithPosition<P> {
{
let is_absolute = trimmed.starts_with(r"\\?\");
if is_absolute {
- return Self::parse_absolute_path(trimmed, parse_path_like_str);
+ return Self::parse_absolute_path(trimmed, |p| parse_path_like_str(s, p));
}
}
@@ -150,7 +154,7 @@ impl<P> PathLikeWithPosition<P> {
Ok(row) => {
if maybe_col_str.is_empty() {
Ok(Self {
- path_like: parse_path_like_str(path_like_str)?,
+ path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: None,
})
@@ -159,12 +163,12 @@ impl<P> PathLikeWithPosition<P> {
maybe_col_str.split_once(':').unwrap_or((maybe_col_str, ""));
match maybe_col_str.parse::<u32>() {
Ok(col) => Ok(Self {
- path_like: parse_path_like_str(path_like_str)?,
+ path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: Some(col),
}),
Err(_) => Ok(Self {
- path_like: parse_path_like_str(path_like_str)?,
+ path_like: parse_path_like_str(s, path_like_str)?,
row: Some(row),
column: None,
}),
@@ -172,7 +176,7 @@ impl<P> PathLikeWithPosition<P> {
}
}
Err(_) => Ok(Self {
- path_like: parse_path_like_str(path_like_str)?,
+ path_like: parse_path_like_str(s, path_like_str)?,
row: None,
column: None,
}),
@@ -323,11 +327,13 @@ impl PathMatcher {
mod tests {
use super::*;
- type TestPath = PathLikeWithPosition<String>;
+ type TestPath = PathLikeWithPosition<(String, String)>;
fn parse_str(s: &str) -> TestPath {
- TestPath::parse_str(s, |s| Ok::<_, std::convert::Infallible>(s.to_string()))
- .expect("infallible")
+ TestPath::parse_str(s, |normalized, s| {
+ Ok::<_, std::convert::Infallible>((normalized.to_string(), s.to_string()))
+ })
+ .expect("infallible")
}
#[test]
@@ -336,7 +342,7 @@ mod tests {
(
"test_file.rs",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@@ -344,7 +350,7 @@ mod tests {
(
"test_file.rs:1",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:1".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@@ -352,7 +358,7 @@ mod tests {
(
"test_file.rs:1:2",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:1:2".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: Some(2),
},
@@ -384,7 +390,7 @@ mod tests {
assert_eq!(
actual,
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: (input.to_string(), "test_file.rs".to_string()),
row,
column,
},
@@ -401,7 +407,7 @@ mod tests {
(
"test_file.rs:",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@@ -409,7 +415,7 @@ mod tests {
(
"test_file.rs:1:",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@@ -417,7 +423,10 @@ mod tests {
(
"crates/file_finder/src/file_finder.rs:1902:13:",
PathLikeWithPosition {
- path_like: "crates/file_finder/src/file_finder.rs".to_string(),
+ path_like: (
+ "crates/file_finder/src/file_finder.rs:1902:13:".to_string(),
+ "crates/file_finder/src/file_finder.rs".to_string(),
+ ),
row: Some(1902),
column: Some(13),
},
@@ -429,7 +438,7 @@ mod tests {
(
"test_file.rs:",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:".to_string(), "test_file.rs".to_string()),
row: None,
column: None,
},
@@ -437,7 +446,7 @@ mod tests {
(
"test_file.rs:1:",
PathLikeWithPosition {
- path_like: "test_file.rs".to_string(),
+ path_like: ("test_file.rs:1:".to_string(), "test_file.rs".to_string()),
row: Some(1),
column: None,
},
@@ -445,7 +454,10 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:",
PathLikeWithPosition {
- path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
+ path_like: (
+ "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:".to_string(),
+ "C:\\Users\\someone\\test_file.rs".to_string(),
+ ),
row: Some(1902),
column: Some(13),
},
@@ -453,7 +465,10 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:",
PathLikeWithPosition {
- path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
+ path_like: (
+ "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:13:15:".to_string(),
+ "C:\\Users\\someone\\test_file.rs".to_string(),
+ ),
row: Some(1902),
column: Some(13),
},
@@ -461,11 +476,36 @@ mod tests {
(
"\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:",
PathLikeWithPosition {
- path_like: "C:\\Users\\someone\\test_file.rs".to_string(),
+ path_like: (
+ "\\\\?\\C:\\Users\\someone\\test_file.rs:1902:::15:".to_string(),
+ "C:\\Users\\someone\\test_file.rs".to_string(),
+ ),
row: Some(1902),
column: None,
},
),
+ (
+ "crates/utils/paths.rs",
+ PathLikeWithPosition {
+ path_like: (
+ "crates\\utils\\paths.rs".to_string(),
+ "crates\\utils\\paths.rs".to_string(),
+ ),
+ row: None,
+ column: None,
+ },
+ ),
+ (
+ "crates/utils/paths.rs:101",
+ PathLikeWithPosition {
+ path_like: (
+ "crates\\utils\\paths.rs:101".to_string(),
+ "crates\\utils\\paths.rs".to_string(),
+ ),
+ row: Some(101),
+ column: None,
+ },
+ ),
];
for (input, expected) in input_and_expected {