-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,53 @@ | ||
use crate::util::Fallible; | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use std::process::{Command, Stdio}; | ||
use yarner_lib::config::Config; | ||
use yarner_lib::document::Document; | ||
|
||
pub fn pre_process( | ||
config: &Config, | ||
documents: HashMap<PathBuf, Document>, | ||
) -> Fallible<HashMap<PathBuf, Document>> { | ||
let mut docs = documents; | ||
for (name, proc) in &config.preprocessor { | ||
let json = yarner_lib::to_json(proc, &docs)?; | ||
let mut child = Command::new(format!("yarner-{}", name)) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
.map_err(|err| format_error(err.into(), name))?; | ||
|
||
{ | ||
let stdin = child | ||
.stdin | ||
.as_mut() | ||
.ok_or("Unable to access child process stdin.") | ||
.map_err(|err| format_error(err.into(), name))?; | ||
stdin | ||
.write_all(json.as_bytes()) | ||
.map_err(|err| format_error(err.into(), name))?; | ||
} | ||
|
||
let output = child | ||
.wait_with_output() | ||
.map_err(|err| format_error(err.into(), name))?; | ||
let out_json = | ||
String::from_utf8(output.stdout).map_err(|err| format_error(err.into(), name))?; | ||
|
||
let (_, new_docs) = | ||
yarner_lib::from_json(&out_json).map_err(|err| format_error(err.into(), name))?; | ||
docs = new_docs; | ||
} | ||
Ok(docs) | ||
} | ||
|
||
fn format_error(err: Box<dyn Error>, name: &str) -> String { | ||
format!( | ||
"Failed to run command 'yarner-{}': {}", | ||
name, | ||
err.to_string() | ||
) | ||
} |
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,12 +1,23 @@ | ||
use crate::config::Config; | ||
use crate::document::Document; | ||
use std::collections::HashMap; | ||
use std::io::Read; | ||
use std::path::PathBuf; | ||
use toml::Value; | ||
|
||
pub mod config; | ||
pub mod document; | ||
|
||
#[allow(dead_code)] | ||
fn to_json(config: &Config, documents: &HashMap<PathBuf, Document>) -> serde_json::Result<String> { | ||
pub fn to_json( | ||
config: &Value, | ||
documents: &HashMap<PathBuf, Document>, | ||
) -> serde_json::Result<String> { | ||
serde_json::to_string_pretty(&(config, documents)) | ||
} | ||
|
||
pub fn from_json(json: &str) -> serde_json::Result<(Value, HashMap<PathBuf, Document>)> { | ||
serde_json::from_str(json) | ||
} | ||
|
||
pub fn parse_input<R: Read>(reader: R) -> serde_json::Result<(Value, HashMap<PathBuf, Document>)> { | ||
serde_json::from_reader(reader) | ||
} |