-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let's bubble up errors instead of panicking and also not use `exit(...)` because it does not call destructors which might give issues.
- Loading branch information
1 parent
4eccecf
commit bc9098e
Showing
11 changed files
with
89 additions
and
67 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
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
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
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 |
---|---|---|
@@ -1,70 +1,81 @@ | ||
extern crate flate2; | ||
use eyre::{ContextCompat, Result}; | ||
use flate2::{write::GzEncoder, Compression}; | ||
use std::{ | ||
fs::{read_dir, File, OpenOptions}, | ||
io::{copy, BufReader, ErrorKind}, | ||
path::Path, | ||
process::{exit, Command, Stdio}, | ||
process::{Command, Stdio}, | ||
}; | ||
|
||
fn main() { | ||
fn main() -> Result<()> { | ||
if let Err(e) = Command::new("scdoc") | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
{ | ||
if let ErrorKind::NotFound = e.kind() { | ||
exit(0); | ||
return Ok(()); | ||
} | ||
} | ||
|
||
// We just append "out" so it's easy to find all the scdoc output later in line 38. | ||
let man_pages: Vec<(String, String)> = read_and_replace_by_ext("./docs", ".scd", ".out"); | ||
let man_pages: Vec<(String, String)> = read_and_replace_by_ext("./docs", ".scd", ".out")?; | ||
for man_page in man_pages { | ||
let output = OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.open(Path::new(&man_page.1)) | ||
.unwrap(); | ||
.open(Path::new(&man_page.1))?; | ||
_ = Command::new("scdoc") | ||
.stdin(Stdio::from(File::open(man_page.0).unwrap())) | ||
.stdin(Stdio::from(File::open(man_page.0)?)) | ||
.stdout(output) | ||
.spawn(); | ||
} | ||
|
||
// Gzipping the man pages | ||
let scdoc_output_files: Vec<(String, String)> = | ||
read_and_replace_by_ext("./docs", ".out", ".gz"); | ||
read_and_replace_by_ext("./docs", ".out", ".gz")?; | ||
for scdoc_output in scdoc_output_files { | ||
let mut input = BufReader::new(File::open(scdoc_output.0).unwrap()); | ||
let mut input = BufReader::new(File::open(scdoc_output.0)?); | ||
let output = OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.open(Path::new(&scdoc_output.1)) | ||
.unwrap(); | ||
.open(Path::new(&scdoc_output.1))?; | ||
let mut encoder = GzEncoder::new(output, Compression::default()); | ||
copy(&mut input, &mut encoder).unwrap(); | ||
encoder.finish().unwrap(); | ||
copy(&mut input, &mut encoder)?; | ||
encoder.finish()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn read_and_replace_by_ext(path: &str, search: &str, replace: &str) -> Vec<(String, String)> { | ||
fn read_and_replace_by_ext( | ||
path: &str, | ||
search: &str, | ||
replace: &str, | ||
) -> Result<Vec<(String, String)>> { | ||
let mut files: Vec<(String, String)> = Vec::new(); | ||
for path in read_dir(path).unwrap() { | ||
let path = path.unwrap(); | ||
if path.file_type().unwrap().is_dir() { | ||
for path in read_dir(path)? { | ||
let path = path?; | ||
if path.file_type()?.is_dir() { | ||
continue; | ||
} | ||
|
||
if let Some(file_name) = path.path().to_str() { | ||
if *path.path().extension().unwrap().to_str().unwrap() != search[1..] { | ||
if *path | ||
.path() | ||
.extension() | ||
.wrap_err_with(|| format!("no extension found for {}", path.path().display()))? | ||
.to_string_lossy() | ||
!= search[1..] | ||
{ | ||
continue; | ||
} | ||
|
||
let file = file_name.replace(search, replace); | ||
files.push((file_name.to_string(), file)); | ||
} | ||
} | ||
files | ||
Ok(files) | ||
} |
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
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