difftreelog
feat yaml stream output
in: master
5 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -54,7 +54,7 @@
#[no_mangle]
pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {
match v {
- 1 => vm.set_manifest_format(ManifestFormat::None),
+ 1 => vm.set_manifest_format(ManifestFormat::String),
0 => vm.set_manifest_format(ManifestFormat::Json(4)),
_ => panic!("incorrect output format"),
}
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -5,7 +5,7 @@
pub enum ManifestFormatName {
/// Expect string as output, and write them directly
- None,
+ String,
Json,
Yaml,
}
@@ -14,7 +14,7 @@
type Err = &'static str;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
Ok(match s {
- "none" => ManifestFormatName::None,
+ "string" => ManifestFormatName::String,
"json" => ManifestFormatName::Json,
"yaml" => ManifestFormatName::Yaml,
_ => return Err("no such format"),
@@ -27,14 +27,17 @@
// #[clap(group = clap::ArgGroup::new("output_format"), help_heading = "MANIFESTIFICATION OUTPUT")]
pub struct ManifestOpts {
/// Output format, wraps resulting value to corresponding std.manifest call.
- /// If none - then jsonnet file is expected to return plain string value, otherwise
+ /// If string - then jsonnet file is expected to return plain string value, otherwise
/// output will be serialized to specified format
- #[clap(long, short = 'f', default_value = "json", possible_values = &["none", "json", "yaml"]/*, group = "output_format"*/)]
+ #[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"]/*, group = "output_format"*/)]
format: ManifestFormatName,
/// Expect string as output, and write them directly.
- /// Shortcut for --format=none, and can't be set with format together
+ /// Shortcut for --format=string, and can't be set with format together
#[clap(long, short = 'S'/*, group = "output_format"*/)]
string: bool,
+ /// Write output as YAML stream, can be used with --format json/yaml
+ #[clap(long, short = 'y')]
+ yaml_stream: bool,
/// Numbed of spaces to pad output manifest with.
/// 0 for hard tabs, -1 for single line output
#[clap(long, default_value = "3")]
@@ -43,10 +46,10 @@
impl ConfigureState for ManifestOpts {
fn configure(&self, state: &EvaluationState) -> Result<()> {
if self.string {
- state.set_manifest_format(ManifestFormat::None);
+ state.set_manifest_format(ManifestFormat::String);
} else {
match self.format {
- ManifestFormatName::None => state.set_manifest_format(ManifestFormat::None),
+ ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),
ManifestFormatName::Json => {
state.set_manifest_format(ManifestFormat::Json(self.line_padding))
}
@@ -55,6 +58,11 @@
}
}
}
+ if self.yaml_stream {
+ state.set_manifest_format(ManifestFormat::YamlStream(Box::new(
+ state.manifest_format(),
+ )))
+ }
Ok(())
}
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -58,6 +58,11 @@
DivisionByZero,
StringManifestOutputIsNotAString,
+ StreamManifestOutputIsNotAArray,
+ MultiManifestOutputIsNotAObject,
+
+ StreamManifestOutputCannotBeRecursed,
+ StreamManifestCannotNestString,
ImportCallbackError(String),
InvalidUnicodeCodepointGot(u32),
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -53,13 +53,6 @@
}
}
-#[derive(Clone)]
-pub enum ManifestFormat {
- Yaml(usize),
- Json(usize),
- None,
-}
-
pub struct EvaluationSettings {
/// Limits recursion by limiting stack frames
pub max_stack: usize,
@@ -321,16 +314,7 @@
}
pub fn manifest(&self, val: Val) -> Result<Rc<str>> {
- self.run_in_state(|| {
- Ok(match self.manifest_format() {
- ManifestFormat::Yaml(padding) => val.into_yaml(padding)?,
- ManifestFormat::Json(padding) => val.into_json(padding)?,
- ManifestFormat::None => match val {
- Val::Str(s) => s,
- _ => throw!(StringManifestOutputIsNotAString),
- },
- })
- })
+ self.run_in_state(|| val.manifest(&self.manifest_format()))
}
/// If passed value is function - call with set TLA
@@ -521,7 +505,7 @@
evaluator
.evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())
.unwrap()
- .into_json(0)
+ .to_json(0)
.unwrap()
.replace("\n", "")
})
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth131 }131 }132}132}133134#[derive(Clone)]135pub enum ManifestFormat {136 YamlStream(Box<ManifestFormat>),137 Yaml(usize),138 Json(usize),139 String,140}133141134#[derive(Debug, Clone)]142#[derive(Debug, Clone)]135pub enum Val {143pub enum Val {222 }230 }231232233 pub fn manifest(&self, ty: &ManifestFormat) -> Result<Rc<str>> {234 Ok(match ty {235 ManifestFormat::YamlStream(format) => {236 let arr = match self {237 Val::Arr(a) => a,238 _ => throw!(StreamManifestOutputIsNotAArray),239 };240 let mut out = String::new();241242 match format as &ManifestFormat {243 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),244 ManifestFormat::String => throw!(StreamManifestCannotNestString),245 _ => {}246 };247248 if !arr.is_empty() {249 for v in arr.iter() {250 out.push_str("---\n");251 out.push_str(&v.manifest(format)?);252 out.push_str("\n");253 }254 out.push_str("...");255 }256257 out.into()258 }259 ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,260 ManifestFormat::Json(padding) => self.to_json(*padding)?,261 ManifestFormat::String => match self {262 Val::Str(s) => s.clone(),263 _ => throw!(StringManifestOutputIsNotAString),264 },265 })266 }223267224 /// For manifestification268 /// For manifestification225 pub fn into_json(self, padding: usize) -> Result<Rc<str>> {269 pub fn to_json(&self, padding: usize) -> Result<Rc<str>> {226 manifest_json_ex(270 manifest_json_ex(227 &self,271 self,228 &ManifestJsonOptions {272 &ManifestJsonOptions {229 padding: &" ".repeat(padding),273 padding: &" ".repeat(padding),230 mtype: if padding == 0 {274 mtype: if padding == 0 {239283240 /// Calls std.manifestJson284 /// Calls std.manifestJson241 #[cfg(feature = "faster")]285 #[cfg(feature = "faster")]242 pub fn into_std_json(self, padding: usize) -> Result<Rc<str>> {286 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {243 manifest_json_ex(287 manifest_json_ex(244 &self,288 &self,245 &ManifestJsonOptions {289 &ManifestJsonOptions {252296253 /// Calls std.manifestJson297 /// Calls std.manifestJson254 #[cfg(not(feature = "faster"))]298 #[cfg(not(feature = "faster"))]255 pub fn into_std_json(self, padding: usize) -> Result<Rc<str>> {299 pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {256 with_state(|s| {300 with_state(|s| {257 let ctx = s301 let ctx = s258 .create_default_context()?302 .create_default_context()?259 .with_var("__tmp__to_json__".into(), self)?;303 .with_var("__tmp__to_json__".into(), self.clone())?;260 Ok(evaluate(304 Ok(evaluate(261 ctx,305 ctx,262 &el!(Expr::Apply(306 &el!(Expr::Apply(274 .try_cast_str("to json")?)318 .try_cast_str("to json")?)275 })319 })276 }320 }277 pub fn into_yaml(self, padding: usize) -> Result<Rc<str>> {321 pub fn to_yaml(&self, padding: usize) -> Result<Rc<str>> {278 with_state(|s| {322 with_state(|s| {279 let ctx = s323 let ctx = s280 .create_default_context()?324 .create_default_context()?281 .with_var("__tmp__to_json__".into(), self);325 .with_var("__tmp__to_json__".into(), self.clone());282 Ok(evaluate(326 Ok(evaluate(283 ctx,327 ctx,284 &el!(Expr::Apply(328 &el!(Expr::Apply(