toml_ops 0.1.0

Implement Toml pointer following the json path syntax, with type Option<&toml::Value>. Overload / as path operator to point into a node in toml tree, as well as some other meaningfull operator overload.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use super::*;

fn load_test_toml() -> Value
{
    let str_toml = include_str!("../examples/sample.toml");
    let v: Value = str_toml.parse().unwrap();
    return v;
}

#[test]
fn path_test() {
    let v = load_test_toml();
    assert_eq!(path(Some(&v), "ip").unwrap().as_str(), Some("127.0.0.1"));
    assert_eq!(v["ip"].as_str(), Some("127.0.0.1"));

    let op = TomlPtr::path(&v);
    let ip = op / "ip";
    assert_eq!(ip.valop.unwrap().as_str(), Some("127.0.0.1"));

    let ip = op / "host" / "ip";
    assert_eq!(ip.valop.unwrap().as_str(), Some("127.0.1.1"));

    let host = TomlPtr::path(&v) / "host";
    let ip = host / "ip";
    assert_eq!(ip.valop.unwrap().as_str(), Some("127.0.1.1"));
    let port = host / "port";
    assert_eq!(port.valop.unwrap().as_integer(), Some(8080));

    let proto = host / "protocol" / 1;
    assert_eq!(proto.valop.unwrap().as_str(), Some("udp"));

    let proto = v.path() / "host" / "protocol" / 2;
    assert_eq!(proto.unpath().unwrap().as_str(), Some("mmp"));

    let proto = v.path() / "host/protocol/2";
    assert_eq!(proto.unpath().unwrap().as_str(), Some("mmp"));

    let proto = v.pathto("/host/protocol/2");
    assert_eq!(proto.unpath().unwrap().as_str(), Some("mmp"));

    let server = v.path() / "service" / 0 / "name";
    assert_eq!(server.unpath().unwrap().as_str(), Some("serv_1"));

    // path() produce immutable reference, cannot write or assign
    // let server = server.unpath().unwrap();
    // *server = Value::String(String::from("serv 1"));

    let mut mv = load_test_toml();
    let ip = mv.get_mut("ip").unwrap();
    *ip = Value::String(String::from("127.0.0.2"));
    assert_eq!((mv.path() / "ip").unpath().unwrap().as_str(), Some("127.0.0.2"));
}

#[test]
fn path_none_test() {
    let v = load_test_toml();

    let root = v.path();
    assert_eq!(root.unpath().is_none(), false);
    assert_eq!(!root, false);

    let node = root / "ip";
    assert_eq!(node.unpath().is_none(), false);
    assert_eq!(!node, false);
    let node = root / "IP";
    assert_eq!(node.unpath().is_none(), true);
    assert_eq!(!node, true);

    let node = root / "host" /"protocol";
    assert_eq!(node.unpath().is_none(), false);
    let node = root / "host" /"protocol" / 1;
    assert_eq!(node.unpath().is_none(), false);
    let node = root / "host" /"protocol" / 3;
    assert_eq!(node.unpath().is_none(), true);

    let node = root / "service" / 0;
    assert_eq!(node.unpath().is_none(), false);
    let node = root / "service" / 0 / "description";
    assert_eq!(node.unpath().is_none(), true);
    let node = root / "service" / 0 / "desc";
    assert_eq!(node.unpath().is_none(), false);
    let node = root / "service" / 2;
    assert_eq!(node.unpath().is_none(), true);
}


#[test]
fn path_mut_test() {
    let mut v = load_test_toml();

    let root = v.path();
    assert_eq!(root.unpath().is_none(), false);
    assert_eq!(!root, false);

    let node = root / "ip";
    assert_eq!(node.unpath().is_none(), false);
    assert_eq!(!node, false);
    assert_eq!(!!node, true);
    assert_eq!(node.is_none(), false);

    let node = root / "IP";
    assert_eq!(node.unpath().is_none(), true);
    assert_eq!(!node, true);
    assert_eq!((*node).is_none(), true);

    let node = v.path_mut() / "ip";
    assert_eq!(node.unpath().is_none(), false);
    assert_eq!(!node, false);

    let node = v.path_mut() / "IP";
    assert_eq!(node.unpath().is_none(), true);
    assert_eq!(node.is_none(), true);
    assert_eq!(!node, true);
}

#[test]
fn path_build_test() {
    let pseg = "".build_path();
    dbg!(&pseg.paths);
    assert_eq!(pseg.paths.is_empty(), false);
    assert_eq!(pseg.paths, vec![""]);

    let pseg = "/".build_path();
    dbg!(&pseg.paths);
    assert_eq!(pseg.paths.is_empty(), false);
    assert_eq!(pseg.paths, vec!["", ""]);

    let pseg = "//".build_path();
    assert_eq!(pseg.paths, vec!["", "", ""]);

    let pseg = "/path/to/leaf".build_path();
    assert_eq!(pseg.paths, vec!["", "path", "to", "leaf"]);

    let pseg = "path/to/leaf".build_path();
    assert_eq!(pseg.paths, vec!["path", "to", "leaf"]);

    let pseg = "path/to//leaf".build_path();
    assert_eq!(pseg.paths, vec!["path", "to", "", "leaf"]);

    let pseg = "path.to.leaf".build_path();
    assert_eq!(pseg.paths, vec!["path", "to", "leaf"]);

    let pseg = "path/to.leaf".build_path();
    assert_eq!(pseg.paths, vec!["path", "to", "leaf"]);

    let pseg = "path/to.leaf/".build_path();
    assert_eq!(pseg.paths, vec!["path", "to", "leaf", ""]);

    let path = "34ab";
    let index = path.parse::<usize>();
    assert_eq!(index.is_ok(), false);

    let path = "34";
    let index = path.parse::<usize>();
    assert_eq!(index.is_ok(), true);
    assert_eq!(index, Ok(34));
}

#[test]
fn pipe_test() {
    let v = load_test_toml();

    // pipe ending slash operator to get inner scalar primitive value
    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.1");

    let ip = v.path() / "host" / "ip" | "";
    assert_eq!(ip, "127.0.1.1");

    let ip_default = "127";
    let ip = v.path() / "host" / "ip" | ip_default;
    assert_eq!(ip, "127.0.1.1");

    // pipe convertor accept &'static str or String,
    // but ofcourse &str is more efficient
    let ip_default: String = String::from("127");
    let ip = v.path() / "host" / "ip" | ip_default;
    assert_eq!(ip, "127.0.1.1");
    // ip_default is moved by | operator, and cannot use non-static &String
    // println!("{ip_default}");

    let port = v.path() / "host" / "port" | 0;
    assert_eq!(port, 8080);

    let port_default = 80;
    let port = v.path() / "host" / "port" | port_default;
    assert_eq!(port, 8080);
    assert_eq!(port_default, 80); // simple primitive wont moved

    // can save intermedia tmp value
    let misc = v.path() / "misc";
    let value = misc / "int" | 0;
    assert_eq!(value, 1234);
    let value = misc / "float" | 0.0;
    assert_eq!(value, 3.14);
    let value = misc / "bool" | false;
    assert_eq!(value, true);

    // path ignore repeated slash or dot
    let value = v.pathto("/misc/int") | 0;
    assert_eq!(value, 1234);
    let value = v.pathto("misc/int") | 0;
    assert_eq!(value, 1234);
    let value = v.pathto("misc/int/") | 0;
    assert_eq!(value, 1234);
    let value = v.pathto("misc.int") | 0;
    assert_eq!(value, 1234);
    let value = v.pathto("/misc/./int/") | 0;
    assert_eq!(value, 1234);
}

#[test]
fn pipe_mut_test() {
    let mut v = load_test_toml();

    let ip = v.path_mut() / "ip" | "";
    assert_eq!(ip, "127.0.0.1");

    let ip = v.path_mut() / "host" / "ip" | "";
    assert_eq!(ip, "127.0.1.1");

    let ip_default = "127";
    let ip = v.path_mut() / "host" / "ip" | ip_default;
    assert_eq!(ip, "127.0.1.1");

    let ip_default: String = String::from("127");
    let ip = v.path_mut() / "host" / "ip" | ip_default;
    assert_eq!(ip, "127.0.1.1");

    let port = v.path_mut() / "host" / "port" | 0;
    assert_eq!(port, 8080);

    let port_default = 80;
    let port = v.path_mut() / "host" / "port" | port_default;
    assert_eq!(port, 8080);
    assert_eq!(port_default, 80);

    // can save intermedia tmp value
    let misc = v.path_mut() / "misc";
    let value = misc / "int" | 0;
    assert_eq!(value, 1234);

    let value = v.pathto_mut("/misc.int") | 0;
    assert_eq!(value, 1234);

    let value = v.path_mut() / "misc" / "float" | 0.0;
    assert_eq!(value, 3.14);
    let value = v.path_mut() / "misc" / "bool" | false;
    assert_eq!(value, true);
}

#[test]
fn push_test() {
    let mut v = load_test_toml();

    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.1");

    let ip_node = v.path_mut() / "ip" << "127.0.0.2";
    let ip = ip_node | "";
    assert_eq!(ip, "127.0.0.2");
    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.2");

    // push mistype value has no effect.
    let ip_node = v.path_mut() / "ip";
    let ip_node = ip_node << 127;
    assert_eq!(ip_node.is_none(), true);
    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.2");

    // push scalar to leat node with supported type.
    let node = v.path_mut() / "misc" / "int" << 4242;
    let val = node | 0;
    assert_eq!(val, 4242);

    let node = v.path_mut() / "misc" / "float";
    let _ = node << 31.4;
    let val = v.path() / "misc" / "float" | 0.0;
    assert_eq!(val, 31.4);

    let node = v.path_mut() / "misc" / "float";
    let node = node << 3142;
    assert_eq!(node.is_none(), true);
    let val = node | 0.0;
    assert_eq!(val, 0.0);
    let val = v.path() / "misc" / "float" | 0.0;
    assert_eq!(val, 31.4);

    let node = v.path_mut() / "misc" / "bool";
    let _ = node << false;
    let val = v.path() / "misc" / "bool" | true;
    assert_eq!(val, false);

    // push a item to toml array
    let node = v.path_mut() / "host" / "protocol";
    let node = node << ("abc", ) << ["edf"];
    // enable print by: cargo test -- --nocapture
    // dbg!(node.unpath());
    let val = node / 3 | "";
    assert_eq!(val, "abc");
    let val = v.path() / "host" / "protocol" / 4 | "";
    assert_eq!(val, "edf");

    // push slice to toml array
    let node = v.path_mut() / "host" / "protocol";
    let _ = node << &["xyz"][..] << &["ABC", "DEF"][..];
    let node = v.path() / "host" / "protocol";
    println!("{}", node.unpath().unwrap());
    assert_eq!(node.unpath().unwrap().as_array().unwrap().len(), 8);

    // push key-val pair to toml table
    let node = v.path_mut() / "host";
    let _ = node << ("newkey1", 1) << ("newkey2", "2");
    let val = v.path() / "host" / "newkey1" | 0;
    assert_eq!(val, 1);
    let val = v.path() / "host" / "newkey2" | "";
    assert_eq!(val, "2");

    // use i32, must cast to i64, because toml integer save i64 
    let input: i32 = 32;
    let output_default: i32 = 2;
    let node = v.path_mut() / "misc" / "int";
    let _ = node << input as i64;
    let val = v.path() / "misc" / "int" | output_default as i64;
    assert_eq!(val, input as i64);
}

#[test]
fn assign_test() {
    let mut v = load_test_toml();

    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.1");

    let mut ip_node = v.path_mut() / "ip";
    ip_node <<= "127.0.0.2";
    let ip = v.path() / "ip" | "";
    assert_eq!(ip, "127.0.0.2");

    // can not assign to expression.
    // (v.path_mut() / "host" / "ip") <<= "127.0.1.2";

    let ip = v.path() / "host" / "ip" | "";
    assert_eq!(ip, "127.0.1.1");
    let mut ip_node = v.path_mut() / "host" / "ip";
    ip_node <<= 127.0; // type mismatch
    let ip = v.path() / "host" / "ip" | "";
    assert_eq!(ip, "");
    let ip = v.path() / "host" / "ip" | 0.0;
    assert_eq!(ip, 127.0);
    let mut ip_node = v.path_mut() / "host" / "ip";
    ip_node <<= "127.0.1.2";
    let ip = v.path() / "host" / "ip" | "";
    assert_eq!(ip, "127.0.1.2");

    // cannot create non-existed node with <<=
    let mut port_node = v.path_mut() / "port";
    assert_eq!(port_node.unpath().is_none(), true);
    port_node <<= 80;
    assert_eq!(port_node.unpath().is_none(), true);

    // <<= can assign any type that support toml::from() method.
    let vecint = vec![1, 2, 3, 4];
    let mut node = v.path_mut() / "misc" / "int";
    node <<= vecint;
    let int = v.path() / "misc" / "int" / 0 | 0;
    assert_eq!(int, 1);
    let int = v.path() / "misc" / "int" / 1 | 0;
    assert_eq!(int, 2);

    let mut node = v.path_mut() / "misc" / "int";
    node.assign(1234);
    let int = v.path() / "misc" / "int" | 0;
    assert_eq!(int, 1234);
}

#[test]
fn path_if_test() {
    let mut v = load_test_toml();

    let node = v.path() / "ip";
    if node.is_some() {
        let ip = node | "";
        assert_eq!(ip, "127.0.0.1");
    }
    if !!node {
        let ip = node | "";
        assert_eq!(ip, "127.0.0.1");
    }

    let node = v.path() / "IP";
    if node.is_none() {
        let ip = node | "";
        assert_eq!(ip, "");
    }
    if !node {
        let ip = node | "";
        assert_eq!(ip, "");
    }

    let node = v.path_mut() / "ip";
    // in mut version operator !node would move self
    if node.is_some() {
        let ip = node | "";
        assert_eq!(ip, "127.0.0.1");
    }

    let mut node = v.path_mut() / "ip";
    if !node.is_none() {
        node = node << "127.0.0.2";
        let ip = node | "";
        assert_eq!(ip, "127.0.0.2");
    }

    let mut node = v.path_mut() / "host" / "port";
    if !node.is_none() {
        node = node << "127.0.0.2";
        assert_eq!(node.is_none(), true);
        let val = node | 0;
        assert_eq!(val, 0);
    }
}