Skip to main content

encode

Function encode 

Source
pub fn encode(value: &Value) -> Result<String, Error>
Expand description

Writes a value as text in the tagged encoding.

Every refusal is an Error::Evaluation, with one of these reasons:

  • non_finite_number: a float that is not finite;
  • depth_limit_exceeded: lists and maps nested past 100 levels, or a text that would, a tag’s own braces counted;
  • reserved_map_key: a map with a $type key, which would read back as a tag;
  • non_string_map_key: a map key that is not a string (an integer, a boolean or undefined), for which a JSON object has no form;
  • invalid_tagged_value: a date or an instant outside the years 0 to 9999 the encoding carries.
use predicator_rs::{tagged, Date, Map, Value};

let date = Date::new(2026, 8, 6).unwrap();
let value = Value::from(vec![Value::from(1), Value::from(1.0), Value::from(date)]);
assert_eq!(
    tagged::encode(&value).unwrap(),
    r#"[1,1.0,{"$type":"date","value":"2026-08-06"}]"#
);
let reserved: Map = [("$type", Value::from("x"))].into_iter().collect();
assert_eq!(tagged::encode(&reserved.into()).unwrap_err().reason(), "reserved_map_key");