difftreelog
feat yaml stream output
in: master
5 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth54#[no_mangle]54#[no_mangle]55pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {55pub extern "C" fn jsonnet_string_output(vm: &EvaluationState, v: c_int) {56 match v {56 match v {57 1 => vm.set_manifest_format(ManifestFormat::None),57 1 => vm.set_manifest_format(ManifestFormat::String),58 0 => vm.set_manifest_format(ManifestFormat::Json(4)),58 0 => vm.set_manifest_format(ManifestFormat::Json(4)),59 _ => panic!("incorrect output format"),59 _ => panic!("incorrect output format"),60 }60 }crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth556pub enum ManifestFormatName {6pub enum ManifestFormatName {7 /// Expect string as output, and write them directly7 /// Expect string as output, and write them directly8 None,8 String,9 Json,9 Json,10 Yaml,10 Yaml,11}11}14 type Err = &'static str;14 type Err = &'static str;15 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {15 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {16 Ok(match s {16 Ok(match s {17 "none" => ManifestFormatName::None,17 "string" => ManifestFormatName::String,18 "json" => ManifestFormatName::Json,18 "json" => ManifestFormatName::Json,19 "yaml" => ManifestFormatName::Yaml,19 "yaml" => ManifestFormatName::Yaml,20 _ => return Err("no such format"),20 _ => return Err("no such format"),27// #[clap(group = clap::ArgGroup::new("output_format"), help_heading = "MANIFESTIFICATION OUTPUT")]27// #[clap(group = clap::ArgGroup::new("output_format"), help_heading = "MANIFESTIFICATION OUTPUT")]28pub struct ManifestOpts {28pub struct ManifestOpts {29 /// Output format, wraps resulting value to corresponding std.manifest call.29 /// Output format, wraps resulting value to corresponding std.manifest call.30 /// If none - then jsonnet file is expected to return plain string value, otherwise30 /// If string - then jsonnet file is expected to return plain string value, otherwise31 /// output will be serialized to specified format31 /// output will be serialized to specified format32 #[clap(long, short = 'f', default_value = "json", possible_values = &["none", "json", "yaml"]/*, group = "output_format"*/)]32 #[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"]/*, group = "output_format"*/)]33 format: ManifestFormatName,33 format: ManifestFormatName,34 /// Expect string as output, and write them directly.34 /// Expect string as output, and write them directly.35 /// Shortcut for --format=none, and can't be set with format together35 /// Shortcut for --format=string, and can't be set with format together36 #[clap(long, short = 'S'/*, group = "output_format"*/)]36 #[clap(long, short = 'S'/*, group = "output_format"*/)]37 string: bool,37 string: bool,38 /// Write output as YAML stream, can be used with --format json/yaml39 #[clap(long, short = 'y')]40 yaml_stream: bool,38 /// Numbed of spaces to pad output manifest with.41 /// Numbed of spaces to pad output manifest with.39 /// 0 for hard tabs, -1 for single line output42 /// 0 for hard tabs, -1 for single line output40 #[clap(long, default_value = "3")]43 #[clap(long, default_value = "3")]43impl ConfigureState for ManifestOpts {46impl ConfigureState for ManifestOpts {44 fn configure(&self, state: &EvaluationState) -> Result<()> {47 fn configure(&self, state: &EvaluationState) -> Result<()> {45 if self.string {48 if self.string {46 state.set_manifest_format(ManifestFormat::None);49 state.set_manifest_format(ManifestFormat::String);47 } else {50 } else {48 match self.format {51 match self.format {49 ManifestFormatName::None => state.set_manifest_format(ManifestFormat::None),52 ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),50 ManifestFormatName::Json => {53 ManifestFormatName::Json => {51 state.set_manifest_format(ManifestFormat::Json(self.line_padding))54 state.set_manifest_format(ManifestFormat::Json(self.line_padding))52 }55 }55 }58 }56 }59 }57 }60 }61 if self.yaml_stream {62 state.set_manifest_format(ManifestFormat::YamlStream(Box::new(63 state.manifest_format(),64 )))65 }58 Ok(())66 Ok(())59 }67 }60}68}crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth58 DivisionByZero,58 DivisionByZero,595960 StringManifestOutputIsNotAString,60 StringManifestOutputIsNotAString,61 StreamManifestOutputIsNotAArray,62 MultiManifestOutputIsNotAObject,6364 StreamManifestOutputCannotBeRecursed,65 StreamManifestCannotNestString,616662 ImportCallbackError(String),67 ImportCallbackError(String),63 InvalidUnicodeCodepointGot(u32),68 InvalidUnicodeCodepointGot(u32),crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth53 }53 }54}54}5556#[derive(Clone)]57pub enum ManifestFormat {58 Yaml(usize),59 Json(usize),60 None,61}625563pub struct EvaluationSettings {56pub struct EvaluationSettings {64 /// Limits recursion by limiting stack frames57 /// Limits recursion by limiting stack frames321 }314 }322315323 pub fn manifest(&self, val: Val) -> Result<Rc<str>> {316 pub fn manifest(&self, val: Val) -> Result<Rc<str>> {324 self.run_in_state(|| {317 self.run_in_state(|| val.manifest(&self.manifest_format()))325 Ok(match self.manifest_format() {326 ManifestFormat::Yaml(padding) => val.into_yaml(padding)?,327 ManifestFormat::Json(padding) => val.into_json(padding)?,328 ManifestFormat::None => match val {329 Val::Str(s) => s,330 _ => throw!(StringManifestOutputIsNotAString),331 },332 })333 })334 }318 }335319336 /// If passed value is function - call with set TLA320 /// If passed value is function - call with set TLA521 evaluator505 evaluator522 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())506 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())523 .unwrap()507 .unwrap()524 .into_json(0)508 .to_json(0)525 .unwrap()509 .unwrap()526 .replace("\n", "")510 .replace("\n", "")527 })511 })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(