1use jrsonnet_evaluator::{2 bail,3 manifest::{escape_string_json_buf, ManifestFormat, ToStringFormat},4 Result, Val,5};67pub struct PythonFormat {8 #[cfg(feature = "exp-preserve-order")]9 preserve_order: bool,10}1112impl ManifestFormat for PythonFormat {13 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {14 match val {15 Val::Bool(true) => buf.push_str("True"),16 Val::Bool(false) => buf.push_str("False"),17 Val::Null => buf.push_str("None"),18 Val::Str(s) => escape_string_json_buf(&s.to_string(), buf),19 Val::Num(_) => ToStringFormat.manifest_buf(val, buf)?,20 Val::Arr(arr) => {21 buf.push('[');22 for (i, el) in arr.iter().enumerate() {23 let el = el?;24 if i != 0 {25 buf.push_str(", ");26 }27 self.manifest_buf(el, buf)?;28 }29 buf.push(']');30 }31 Val::Obj(obj) => {32 obj.run_assertions()?;33 buf.push('{');34 let fields = obj.fields(35 #[cfg(feature = "exp-preserve-order")]36 self.preserve_order,37 );38 for (i, field) in fields.into_iter().enumerate() {39 if i != 0 {40 buf.push_str(", ");41 }42 escape_string_json_buf(&field, buf);43 buf.push_str(": ");44 let value = obj.get(field)?.expect("field exists");45 self.manifest_buf(value, buf)?;46 }47 buf.push('}');48 }49 Val::Func(_) => bail!("tried to manifest function"),50 }51 Ok(())52 }53}5455pub struct PythonVarsFormat {56 #[cfg(feature = "exp-preserve-order")]57 preserve_order: bool,58}5960impl PythonVarsFormat {}6162impl ManifestFormat for PythonVarsFormat {63 fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {64 let inner = PythonFormat {65 #[cfg(feature = "exp-preserve-order")]66 preserve_order: self.preserve_order,67 };68 let Val::Obj(obj) = val else {69 bail!("python vars root should be object");70 };71 obj.run_assertions()?;7273 let fields = obj.fields(74 #[cfg(feature = "exp-preserve-order")]75 self.preserve_order,76 );7778 for field in fields {79 80 buf.push_str(&field);81 buf.push_str(" = ");82 inner.manifest_buf(obj.get(field)?.expect("field exists"), buf)?;83 buf.push('\n');84 }85 Ok(())86 }87}