-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make examples less sensitive to the exact path from which they're run. (
#241)
- Loading branch information
1 parent
ef02bb3
commit e41d382
Showing
6 changed files
with
115 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::{ | ||
fs, | ||
path::{Components, PathBuf}, | ||
}; | ||
|
||
/// Pop path components from the front of the path component iterator, then try the read again. | ||
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> { | ||
match iter.next() { | ||
Some(_) => match fs::read_to_string(iter.as_path()) { | ||
Ok(content) => Ok(content), | ||
Err(_) => { | ||
if tries_remaining > 0 { | ||
path_compensating_read(iter, tries_remaining - 1) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
}, | ||
None => Err(()), | ||
} | ||
} | ||
|
||
pub(super) fn read_file(path: &str) -> String { | ||
match fs::read_to_string(path) { | ||
Ok(content) => content, | ||
Err(e) => { | ||
// Maybe the user is too deep in the directory tree. | ||
// Try skipping some components from the front of the path. | ||
let path = PathBuf::from(path); | ||
path_compensating_read(path.components(), 3) | ||
.map_err(|_| e) | ||
.expect("failed to read file") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::{ | ||
fs, | ||
path::{Components, PathBuf}, | ||
}; | ||
|
||
/// Pop path components from the front of the path component iterator, then try the read again. | ||
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> { | ||
match iter.next() { | ||
Some(_) => match fs::read_to_string(iter.as_path()) { | ||
Ok(content) => Ok(content), | ||
Err(_) => { | ||
if tries_remaining > 0 { | ||
path_compensating_read(iter, tries_remaining - 1) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
}, | ||
None => Err(()), | ||
} | ||
} | ||
|
||
pub(super) fn read_file(path: &str) -> String { | ||
match fs::read_to_string(path) { | ||
Ok(content) => content, | ||
Err(e) => { | ||
// Maybe the user is too deep in the directory tree. | ||
// Try skipping some components from the front of the path. | ||
let path = PathBuf::from(path); | ||
path_compensating_read(path.components(), 3) | ||
.map_err(|_| e) | ||
.expect("failed to read file") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::{ | ||
fs, | ||
path::{Components, PathBuf}, | ||
}; | ||
|
||
/// Pop path components from the front of the path component iterator, then try the read again. | ||
fn path_compensating_read(mut iter: Components<'_>, tries_remaining: i64) -> Result<String, ()> { | ||
match iter.next() { | ||
Some(_) => match fs::read_to_string(iter.as_path()) { | ||
Ok(content) => Ok(content), | ||
Err(_) => { | ||
if tries_remaining > 0 { | ||
path_compensating_read(iter, tries_remaining - 1) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
}, | ||
None => Err(()), | ||
} | ||
} | ||
|
||
pub(super) fn read_file(path: &str) -> String { | ||
match fs::read_to_string(path) { | ||
Ok(content) => content, | ||
Err(e) => { | ||
// Maybe the user is too deep in the directory tree. | ||
// Try skipping some components from the front of the path. | ||
let path = PathBuf::from(path); | ||
path_compensating_read(path.components(), 3) | ||
.map_err(|_| e) | ||
.expect("failed to read file") | ||
} | ||
} | ||
} |