Skip to content

Commit 9750fb7

Browse files
studootnrc
authored andcommitted
Canonicalize file paths within the map of file line ranges (#1098)
* Canonicalize file paths within the map of file line ranges * Forgot to run the tests - and of course, the formatting of the canonicalization change was off, but it's fixed now! * Move imports to the top of the file, as per @nrc. * Change `canonicalize_path_string` to return `Option<String>`, `None` indicating an error rather than an empty string * `format!` is better than string concatenation... * Change `canonicalize_path_string` to return `Result` rather than `Option`
1 parent 25f9732 commit 9750fb7

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/file_lines.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
//! This module contains types and functions to support formatting specific line ranges.
12-
use std::{cmp, iter, str};
12+
use std::{cmp, iter, path, str};
1313

1414
use itertools::Itertools;
1515
use multimap::MultiMap;
@@ -119,9 +119,10 @@ impl FileLines {
119119
Some(ref map) => map,
120120
};
121121

122-
match map.get_vec(range.file_name()) {
123-
None => false,
124-
Some(ranges) => ranges.iter().any(|r| r.contains(Range::from(range))),
122+
match canonicalize_path_string(range.file_name())
123+
.and_then(|canonical| map.get_vec(&canonical).ok_or(())) {
124+
Ok(ranges) => ranges.iter().any(|r| r.contains(Range::from(range))),
125+
Err(_) => false,
125126
}
126127
}
127128

@@ -151,13 +152,20 @@ impl<'a> iter::Iterator for Files<'a> {
151152
}
152153
}
153154

155+
fn canonicalize_path_string(s: &str) -> Result<String, ()> {
156+
match path::PathBuf::from(s).canonicalize() {
157+
Ok(canonicalized) => canonicalized.to_str().map(|s| s.to_string()).ok_or(()),
158+
_ => Err(()),
159+
}
160+
}
161+
154162
// This impl is needed for `Config::override_value` to work for use in tests.
155163
impl str::FromStr for FileLines {
156164
type Err = String;
157165

158166
fn from_str(s: &str) -> Result<FileLines, String> {
159167
let v: Vec<JsonSpan> = try!(json::decode(s).map_err(|e| e.to_string()));
160-
let m = v.into_iter().map(JsonSpan::into_tuple).collect();
168+
let m = try!(v.into_iter().map(JsonSpan::into_tuple).collect());
161169
Ok(FileLines::from_multimap(m))
162170
}
163171
}
@@ -171,9 +179,11 @@ struct JsonSpan {
171179

172180
impl JsonSpan {
173181
// To allow `collect()`ing into a `MultiMap`.
174-
fn into_tuple(self) -> (String, Range) {
182+
fn into_tuple(self) -> Result<(String, Range), String> {
175183
let (lo, hi) = self.range;
176-
(self.file, Range::new(lo, hi))
184+
let canonical = try!(canonicalize_path_string(&self.file)
185+
.map_err(|_| format!("Can't canonicalize {}", &self.file)));
186+
Ok((canonical, Range::new(lo, hi)))
177187
}
178188
}
179189

0 commit comments

Comments
 (0)