Skip to main content

encode_compact

Function encode_compact 

Source
pub fn encode_compact(value: &LinoValue) -> String
Expand description

Encode a value to the compact, single-line Links Notation format.

Every string is base64-encoded and the whole document is written on one line. decode reads this form as well, so stored files remain readable by the library after switching to the default readable output.

§Arguments

  • value - The value to encode

§Returns

A string in compact Links Notation format

§Example

use lino_objects_codec::{encode_compact, decode, LinoValue};

let data = LinoValue::object([("name", LinoValue::String("Alice".to_string()))]);
let encoded = encode_compact(&data);
// String "Alice" is base64-encoded as "QWxpY2U="
assert!(encoded.contains("QWxpY2U="));
assert_eq!(decode(&encoded).unwrap(), data);
Examples found in repository?
examples/basic_usage.rs (line 192)
5fn main() {
6    println!("=== Links Notation Objects Codec - Rust Example ===\n");
7
8    // Example 1: Basic types
9    println!("1. Basic Types:");
10
11    // Null
12    let null_val = LinoValue::Null;
13    let encoded = encode(&null_val);
14    println!(
15        "   Null: {} -> decoded: {:?}",
16        encoded,
17        decode(&encoded).unwrap()
18    );
19
20    // Booleans
21    let true_val = LinoValue::Bool(true);
22    let encoded = encode(&true_val);
23    println!(
24        "   true: {} -> decoded: {:?}",
25        encoded,
26        decode(&encoded).unwrap()
27    );
28
29    // Integers
30    let int_val = LinoValue::Int(42);
31    let encoded = encode(&int_val);
32    println!(
33        "   42: {} -> decoded: {:?}",
34        encoded,
35        decode(&encoded).unwrap()
36    );
37
38    // Floats
39    let float_val = LinoValue::Float(3.14159);
40    let encoded = encode(&float_val);
41    println!(
42        "   3.14159: {} -> decoded: {:?}",
43        encoded,
44        decode(&encoded).unwrap()
45    );
46
47    // Special floats
48    let inf_val = LinoValue::Float(f64::INFINITY);
49    let encoded = encode(&inf_val);
50    println!(
51        "   Infinity: {} -> decoded: {:?}",
52        encoded,
53        decode(&encoded).unwrap()
54    );
55
56    let nan_val = LinoValue::Float(f64::NAN);
57    let encoded = encode(&nan_val);
58    let decoded = decode(&encoded).unwrap();
59    println!(
60        "   NaN: {} -> decoded is_nan: {}",
61        encoded,
62        decoded.as_float().unwrap().is_nan()
63    );
64
65    // Strings
66    let str_val = LinoValue::String("Hello, World!".to_string());
67    let encoded = encode(&str_val);
68    println!(
69        "   'Hello, World!': {} -> decoded: {:?}",
70        encoded,
71        decode(&encoded).unwrap()
72    );
73
74    // Unicode strings
75    let unicode_val = LinoValue::String("你好世界 🌍".to_string());
76    let encoded = encode(&unicode_val);
77    println!(
78        "   Unicode: {} -> decoded: {:?}",
79        encoded,
80        decode(&encoded).unwrap()
81    );
82
83    println!();
84
85    // Example 2: Collections
86    println!("2. Collections:");
87
88    // Array
89    let array_val = LinoValue::array([LinoValue::Int(1), LinoValue::Int(2), LinoValue::Int(3)]);
90    let encoded = encode(&array_val);
91    let decoded = decode(&encoded).unwrap();
92    println!("   Array [1, 2, 3]:\n{}", encoded);
93    println!("   Decoded: {:?}", decoded);
94
95    // Object
96    let obj_val = LinoValue::object([
97        ("name", LinoValue::String("Alice".to_string())),
98        ("age", LinoValue::Int(30)),
99        ("active", LinoValue::Bool(true)),
100    ]);
101    let encoded = encode(&obj_val);
102    let decoded = decode(&encoded).unwrap();
103    println!("   Object {{name, age, active}}:\n{}", encoded);
104    println!("   Decoded: {:?}", decoded);
105
106    println!();
107
108    // Example 3: Nested structures
109    println!("3. Nested Structure:");
110
111    let complex = LinoValue::object([
112        ("id", LinoValue::Int(123)),
113        ("name", LinoValue::String("Test Object".to_string())),
114        (
115            "tags",
116            LinoValue::array([
117                LinoValue::String("tag1".to_string()),
118                LinoValue::String("tag2".to_string()),
119            ]),
120        ),
121        (
122            "metadata",
123            LinoValue::object([
124                ("version", LinoValue::Int(1)),
125                ("created", LinoValue::String("2025-01-01".to_string())),
126            ]),
127        ),
128    ]);
129
130    let encoded = encode(&complex);
131    let decoded = decode(&encoded).unwrap();
132
133    println!("   Encoded:\n{}", encoded);
134    println!("   Decoded name: {:?}", decoded.get("name"));
135    println!("   Decoded tags: {:?}", decoded.get("tags"));
136    println!(
137        "   Decoded metadata.version: {:?}",
138        decoded.get("metadata").and_then(|m| m.get("version"))
139    );
140
141    println!();
142
143    // Example 4: Mixed type array
144    println!("4. Mixed Type Array:");
145
146    let mixed = LinoValue::array([
147        LinoValue::Int(1),
148        LinoValue::String("hello".to_string()),
149        LinoValue::Bool(true),
150        LinoValue::Null,
151        LinoValue::Float(2.5),
152    ]);
153
154    let encoded = encode(&mixed);
155    let decoded = decode(&encoded).unwrap();
156
157    println!("   Encoded:\n{}", encoded);
158    println!("   Decoded: {:?}", decoded);
159
160    println!();
161
162    // Example 5: Roundtrip verification
163    println!("5. Roundtrip Verification:");
164
165    let data = LinoValue::object([
166        (
167            "users",
168            LinoValue::array([
169                LinoValue::object([
170                    ("id", LinoValue::Int(1)),
171                    ("name", LinoValue::String("Alice".to_string())),
172                ]),
173                LinoValue::object([
174                    ("id", LinoValue::Int(2)),
175                    ("name", LinoValue::String("Bob".to_string())),
176                ]),
177            ]),
178        ),
179        ("count", LinoValue::Int(2)),
180    ]);
181
182    let encoded = encode(&data);
183    let decoded = decode(&encoded).unwrap();
184
185    println!("   Original == Decoded: {}", data == decoded);
186
187    println!();
188
189    // Example 6: The compact (base64) form is still available under its own name
190    println!("6. Compact Form:");
191
192    let compact = encode_compact(&data);
193    println!("   Compact: {}", compact);
194    println!(
195        "   Compact decodes back to the same value: {}",
196        decode(&compact).unwrap() == data
197    );
198
199    println!("\n=== Example completed successfully! ===");
200}