-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
track_caller
to make debugging bindgen
build script errors ea…
…sier (#3383)
- Loading branch information
Showing
3 changed files
with
40 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
use std::io::BufRead; | ||
|
||
#[track_caller] | ||
pub fn read_file_lines(path: &str) -> Vec<String> { | ||
let file = std::io::BufReader::new( | ||
std::fs::File::open(path).unwrap_or_else(|_| panic!("failed to open file `{path}`")), | ||
); | ||
let Ok(file) = std::fs::File::open(path) else { | ||
panic!("failed to open file `{path}`") | ||
}; | ||
|
||
file.lines() | ||
.map(|line| line.unwrap_or_else(|_| panic!("failed to read file lines `{path}`"))) | ||
.collect() | ||
let file = std::io::BufReader::new(file); | ||
let mut lines = vec![]; | ||
|
||
for line in file.lines() { | ||
let Ok(line) = line else { | ||
panic!("failed to read file lines `{path}`"); | ||
}; | ||
|
||
lines.push(line); | ||
} | ||
|
||
lines | ||
} | ||
|
||
#[track_caller] | ||
pub fn write_to_file<C: AsRef<[u8]>>(path: &str, contents: C) { | ||
if let Some(parent) = std::path::Path::new(path).parent() { | ||
std::fs::create_dir_all(parent) | ||
.unwrap_or_else(|_| panic!("failed to create directory `{path}`")); | ||
if std::fs::create_dir_all(parent).is_err() { | ||
panic!("failed to create directory `{path}`"); | ||
} | ||
} | ||
|
||
std::fs::write(path, contents).unwrap_or_else(|_| panic!("failed to write file `{path}`")); | ||
if std::fs::write(path, contents).is_err() { | ||
panic!("failed to write file `{path}`"); | ||
} | ||
} |
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