rasant 1.0.0

Rasant is a lightweight, high performance and flexible Rust library for structured logging.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
pub mod scalar;
pub mod value;

use std::fmt;
use std::slice;

use crate::constant::{ATTRIBUTE_KEY_ERROR, ATTRIBUTE_KEYS_PRIORITY, ATTRIBUTE_KEYS_RESTRICTED};

pub use scalar::Scalar;
pub use value::Value;

/// Attribute metadata flags for attributes.
pub enum MetadataField {
	/// The attribute key is restricted - i.e. it cannot be set by users.
	Restricted = (1 << 0),
	/// Priority attribute, which are always listed/returned first.
	Priority = (1 << 1),
	/// An attribute for an error.
	Error = (1 << 2),
	/// Ephemeral attribute - i.e. defined just for an individual log write.
	Ephemeral = (1 << 3),
}

/// Attribute metadata implementation.
pub trait MetadataImpl {
	/// Resolves base [`Metadata`] for a given attribute key, as [`&str].
	fn from_key<'f>(key: &'f str) -> Self;
	/// Evaluates whether a [`MetadataField`] is set or not.
	fn get(&self, field: MetadataField) -> bool;
	/// Sets/unsets a [`MetadataField`].
	fn set(&mut self, field: MetadataField, value: bool);
}

/// Metadata associated with {key: attribute} pairs.
// usize is overkill for this usecase, but guarantees memory alignment within the vectors used by Map.
pub type Metadata = usize;

impl MetadataImpl for Metadata {
	fn from_key<'f>(key: &'f str) -> Self {
		let mut res: Metadata = 0;

		if ATTRIBUTE_KEYS_RESTRICTED.iter().any(|&r| key == r) {
			// don't bother resolving the rest of metadata for restricted keys
			return MetadataField::Restricted as Metadata;
		}

		if ATTRIBUTE_KEYS_PRIORITY.iter().any(|&p| key == p) {
			res |= MetadataField::Priority as Metadata;
		}

		if key == ATTRIBUTE_KEY_ERROR {
			res |= MetadataField::Error as Metadata;
		}

		return res;
	}

	fn get(&self, field: MetadataField) -> bool {
		self & (field as usize) != 0
	}

	fn set(&mut self, field: MetadataField, value: bool) {
		match value {
			true => *self |= field as Metadata,
			false => *self &= !(field as Metadata),
		}
	}
}

/// A store for a ordered map of (key -> [`Value`])
#[derive(Debug, Clone)]
pub struct Map {
	/// A container for all strings in this map (keys and scalars)
	string_pool: String,
	/// Indeces for every string in the pool: [start, end)
	string_idxs: Vec<(usize, usize)>,
	/// Name string index and metadata for all keys.
	keys: Vec<(usize, Metadata)>,
	/// A container for all Scalars used by Values in this store.
	scalar_pool: Vec<Scalar>,
	/// Indexes for 1st and 2nd set of Scalar's associated with each key, as [1_start, end), [2_start, 2_end)
	/// (0.0) indicates no 2nd set present.
	scalar_idxs: Vec<(usize, usize, usize, usize)>,
}

impl Map {
	pub fn new() -> Self {
		Self {
			string_pool: String::new(),
			string_idxs: Vec::new(),
			keys: Vec::new(),
			scalar_pool: Vec::new(),
			scalar_idxs: Vec::new(),
		}
	}

	pub fn clear(&mut self) {
		self.string_pool.clear();
		self.string_idxs.clear();
		self.keys.clear();
		self.scalar_pool.clear();
		self.scalar_idxs.clear();
	}

	pub fn copy_from(&mut self, other: &Self) {
		self.string_pool.clear();
		self.string_idxs.clear();
		self.keys.clear();
		self.scalar_pool.clear();
		self.scalar_idxs.clear();

		if !other.is_empty() {
			self.string_pool.push_str(&other.string_pool);
			self.string_idxs.extend(&other.string_idxs);
			self.keys.extend(&other.keys);
			// iterating over scalar_pool yields &Clone instead of Clone >:(
			self.scalar_pool.extend_from_slice(&other.scalar_pool);
			self.scalar_idxs.extend(&other.scalar_idxs);
		}
	}

	pub fn len(&self) -> usize {
		self.keys.len()
	}

	fn is_empty(&self) -> bool {
		self.keys.is_empty()
	}

	fn store_size(&self) -> usize {
		self.scalar_pool.len()
	}

	pub fn key_iter(&self) -> MapKeyIter<'_> {
		MapKeyIter::new(self)
	}

	pub fn key_value_iter(&self) -> MapKeyValueIter<'_> {
		MapKeyValueIter::new(self)
	}

	pub fn iter(&self) -> MapIter<'_> {
		MapIter::new(self)
	}

	// for key strings (relatively short, and small in number), a linear
	// search turns out to be the most efficient way to seek.
	fn idx_by_key(&self, key: &str) -> Option<usize> {
		let key_size = key.len();
		for i in 0..self.keys.len() {
			let (key_start, key_end) = self.string_idxs[self.keys[i].0];
			if (key_end - key_start) != key_size {
				continue;
			}
			if key == &self.string_pool[key_start..key_end] {
				return Some(i);
			}
		}

		None
	}

	fn key_by_idx(&self, idx: usize) -> Option<&str> {
		match idx < self.keys.len() {
			false => None,
			true => {
				let (start, end) = self.string_idxs[self.keys[idx].0];
				Some(&self.string_pool[start..end])
			}
		}
	}

	fn key_meta_by_idx(&self, idx: usize) -> Option<(&str, Metadata)> {
		match idx < self.keys.len() {
			false => None,
			true => {
				let (str_idx, meta) = self.keys[idx];
				let (start, end) = self.string_idxs[str_idx];
				Some((&self.string_pool[start..end], meta))
			}
		}
	}

	fn meta_by_idx(&self, idx: usize) -> Metadata {
		self.keys[idx].1
	}

	fn set_meta_by_idx(&mut self, idx: usize, meta: Metadata) {
		self.keys[idx].1 = meta
	}

	fn value_by_idx(&self, idx: usize) -> Value<'_> {
		let (start_1, end_1, start_2, end_2) = self.scalar_idxs[idx];

		match start_2 != 0 || end_2 != 0 {
			// a 2nd set of scalars means we have a Map
			true => Value::from((&self.scalar_pool[start_1..end_1], &self.scalar_pool[start_2..end_2])),
			false => Value::from(&self.scalar_pool[start_1..end_1]),
		}
	}

	pub fn has(&self, key: &str) -> bool {
		self.idx_by_key(key).is_some()
	}

	fn has_idx(&self, idx: usize) -> bool {
		idx < self.keys.len()
	}

	fn string_pool_add(&mut self, s: &str) -> usize {
		let start = self.string_pool.len();
		let end = start + s.len();
		let idx = self.string_idxs.len();

		self.string_pool.push_str(s);
		self.string_idxs.push((start, end));

		idx
	}

	fn string_pool_remove(&mut self, del_idx: usize) {
		let (del_start, del_end) = self.string_idxs[del_idx];
		let del_size = del_end - del_start;
		self.string_pool.drain(del_start..del_end);
		self.string_idxs.remove(del_idx);

		// Re-align all indexed string entries...
		self.string_idxs.iter_mut().for_each(|(start, end)| {
			if *start >= del_size && *start >= del_start {
				*start -= del_size;
				*end -= del_size;
			}
		});

		// ...and all items refering to strings by idx.
		self.keys.iter_mut().for_each(|(idx, _)| {
			if *idx > 0 && *idx >= del_idx {
				*idx -= 1;
			}
		});
		self.scalar_pool.iter_mut().for_each(|mut v| {
			if let Scalar::StringIndex(idx, _) = &mut v {
				if *idx != 0 && *idx >= del_idx {
					*idx -= 1
				}
			}
		});
	}

	fn scalar_convert_to_pooled(&mut self, sc: &Scalar) -> Scalar {
		if let Scalar::String(s, needs_escaping) = sc {
			let idx = self.string_pool_add(s);
			return Scalar::StringIndex(idx, *needs_escaping);
		}

		sc.clone()
	}

	fn scalar_pool_delete_strings(&mut self, start: usize, end: usize) {
		for i in start..end {
			if let Scalar::StringIndex(idx, _) = &self.scalar_pool[i] {
				self.string_pool_remove(*idx);
			}
		}
	}

	fn scalar_pool_extend(&mut self, ss: &[Scalar]) -> usize {
		let idx = self.scalar_pool.len();

		for s in ss {
			let ns = self.scalar_convert_to_pooled(&s);
			self.scalar_pool.push(ns);
		}

		idx
	}

	fn scalar_pool_remove(&mut self, start: usize, end: usize) {
		let size = end - start;

		self.scalar_pool_delete_strings(start, end);
		self.scalar_pool.drain(start..end);

		self.scalar_idxs.iter_mut().for_each(|(pool_start_1, pool_end_1, pool_start_2, pool_end_2)| {
			if *pool_start_1 >= size && *pool_start_1 > start {
				*pool_start_1 -= size;
				*pool_end_1 -= size;
			}
			if *pool_start_2 >= size && *pool_start_2 > start {
				*pool_start_2 -= size;
				*pool_end_2 -= size;
			}
		})
	}

	fn scalar_pool_overwrite(&mut self, start: usize, ss: &[Scalar]) {
		self.scalar_pool_delete_strings(start, start + ss.len());

		let mut i = start;
		for s in ss {
			let ns = self.scalar_convert_to_pooled(&s);
			self.scalar_pool[i] = ns;
			i += 1;
		}
	}

	fn scalar_pool_add(&mut self, insert_first: bool, ss_1: &[Scalar], ss_2: &[Scalar]) {
		let start_1 = self.scalar_pool_extend(ss_1);
		let end_1 = start_1 + ss_1.len();

		let (mut start_2, mut end_2) = (0 as usize, 0 as usize);
		if !ss_2.is_empty() {
			start_2 = self.scalar_pool_extend(ss_2);
			end_2 = start_2 + ss_2.len();
		};

		match insert_first {
			true => self.scalar_idxs.insert(0, (start_1, end_1, start_2, end_2)),
			false => self.scalar_idxs.push((start_1, end_1, start_2, end_2)),
		}
	}

	fn scalar_pool_replace(&mut self, idx: usize, ss_1: &[Scalar], ss_2: &[Scalar]) {
		let (mut start_1, mut end_1, _, _) = self.scalar_idxs[idx];
		let pre_size_1 = end_1 - start_1;

		// delete slot for first slice
		if ss_1.len() == pre_size_1 {
			// yay, new scalars fit in the existing slot
			self.scalar_pool_overwrite(start_1, ss_1);
		} else {
			// we'll have to resize and extend :'(
			self.scalar_pool_remove(start_1, end_1);

			start_1 = self.scalar_pool_extend(ss_1);
			end_1 = start_1 + ss_1.len();

			self.scalar_idxs[idx].0 = start_1;
			self.scalar_idxs[idx].1 = end_1;
		}

		// delete slot for second slice, if present
		let (_, _, mut start_2, mut end_2) = self.scalar_idxs[idx];
		let pre_size_2 = end_2 - start_2;

		if ss_2.len() == pre_size_2 {
			self.scalar_pool_overwrite(start_2, ss_2);
		} else {
			self.scalar_pool_remove(start_2, end_2);

			if ss_2.is_empty() {
				start_2 = 0;
				end_2 = 0;
			} else {
				start_2 = self.scalar_pool_extend(ss_2);
				end_2 = start_2 + ss_2.len();
			};

			self.scalar_idxs[idx].2 = start_2;
			self.scalar_idxs[idx].3 = end_2;
		}
	}

	pub fn get(&self, key: &str) -> Option<(Value<'_>, Metadata)> {
		match self.idx_by_key(key) {
			Some(i) => {
				let meta = self.meta_by_idx(i);
				Some((self.value_by_idx(i), meta))
			}
			None => None,
		}
	}

	pub fn get_value(&self, key: &str) -> Option<Value<'_>> {
		match self.idx_by_key(key) {
			Some(i) => Some(self.value_by_idx(i)),
			None => None,
		}
	}

	fn set(&mut self, key: &str, val: &Value, ephemeral: bool) {
		if key.is_empty() {
			panic!("empty log attribute key {{\"\" -> {val:?}}}");
		}
		if key.chars().any(|c| c.is_whitespace() || !c.is_ascii()) {
			panic!("invalid log attribute key {{\"{key}\" -> {val:?}}}");
		}

		let mut meta = Metadata::from_key(key);
		if meta.get(MetadataField::Restricted) {
			panic!("cannot use restricted log attribute key {{\"{key}\" -> {val:?}}}");
		}
		meta.set(MetadataField::Ephemeral, ephemeral);

		let (ss_1, ss_2): (&[Scalar], &[Scalar]) = match val {
			Value::Scalar(s) => (slice::from_ref(s), &[]),
			Value::List(ss) => (*ss, &[]),
			Value::Map(keys, vals) => {
				if keys.len() != vals.len() {
					panic!("Map scalars mismatch for attribute key {{{key} -> {val:?}}}");
				}
				// TODO: handling duplicate keys without a panic would be nice...
				for i in 0..keys.len() {
					let key_i = &keys[i];
					for j in i + 1..keys.len() {
						let key_j = &keys[j];
						if key_i == key_j {
							panic!("Duplicate key for Map {{{key_i} -> {val_i}}} vs {{{key_j} -> {val_j}}}", val_i = &vals[i], val_j = &vals[j]);
						}
					}
				}

				(*keys, *vals)
			}
		};
		if ss_1.is_empty() {
			panic!("no scalars for attribute key {{\"{key}\" -> {val:?}}}");
		}

		if let Some(i) = self.idx_by_key(key) {
			// overwrite existing key
			self.set_meta_by_idx(i, meta);
			self.scalar_pool_replace(i, ss_1, ss_2);
			return;
		}

		// insert new key
		let key_idx = self.string_pool_add(key);
		match meta.get(MetadataField::Priority) {
			true => {
				// priority keys are inserted first
				self.scalar_pool_add(true, ss_1, ss_2);
				self.keys.insert(0, (key_idx, meta));
			}
			false => {
				// insert new key last
				self.scalar_pool_add(false, ss_1, ss_2);
				self.keys.push((key_idx, meta));
			}
		}
	}

	pub fn insert_ref(&mut self, key: &str, val: &Value) {
		self.set(key, val, false);
	}

	pub fn insert(&mut self, key: &str, val: Value) {
		self.set(key, &val, false);
	}

	pub fn insert_ref_ephemeral(&mut self, key: &str, val: &Value) {
		self.set(key, val, true);
	}

	pub fn insert_ephemeral(&mut self, key: &str, val: Value) {
		self.set(key, &val, true);
	}

	pub fn str_by_idx<'f>(&'f self, idx: usize) -> &'f str {
		if idx >= self.string_pool.len() {
			panic!("invalid pooled string #{idx} for Map");
		}

		let (start, end) = self.string_idxs[idx];
		&self.string_pool[start..end]
	}
}

/// A key iterator for attribute maps.
pub struct MapKeyIter<'s> {
	map: &'s Map,
	idx: usize,
}

impl<'i> MapKeyIter<'i> {
	/// Intiializes an attribute map key iterator.
	pub fn new(map: &'i Map) -> Self {
		Self { map: map, idx: 0 }
	}
}

impl<'i> Iterator for MapKeyIter<'i> {
	type Item = &'i str;

	fn next(&mut self) -> Option<Self::Item> {
		let key = self.map.key_by_idx(self.idx);
		self.idx += 1;

		key
	}
}

/// A {key, [`Value`]} iterator for attribute maps.
pub struct MapKeyValueIter<'s> {
	map: &'s Map,
	idx: usize,
}

impl<'i> MapKeyValueIter<'i> {
	/// Intiializes an attribute map {key, [`Value`]} iterator.
	pub fn new(map: &'i Map) -> Self {
		Self { map: map, idx: 0 }
	}
}

impl<'i> Iterator for MapKeyValueIter<'i> {
	// {key: Value}
	type Item = (&'i str, Value<'i>);

	fn next(&mut self) -> Option<Self::Item> {
		match self.map.key_by_idx(self.idx) {
			None => None,
			Some(key) => {
				let val = self.map.value_by_idx(self.idx);
				self.idx += 1;

				Some((key, val))
			}
		}
	}
}

/// A {key, [`Value`], [`Metadata`]} iterator for attribute maps.
pub struct MapIter<'s> {
	map: &'s Map,
	idx: usize,
}

impl<'i> MapIter<'i> {
	/// Intiializes an attribute map key -> {key, [`Value`], [`Metadata`]} iterator.
	pub fn new(map: &'i Map) -> Self {
		Self { map: map, idx: 0 }
	}
}

impl<'i> Iterator for MapIter<'i> {
	// {key: Value}
	type Item = (&'i str, Value<'i>, Metadata);

	fn next(&mut self) -> Option<Self::Item> {
		match self.map.key_by_idx(self.idx) {
			None => None,
			Some(key) => {
				// TODO: clean me up
				let meta = self.map.keys[self.idx].1;
				let val = self.map.value_by_idx(self.idx);
				self.idx += 1;

				Some((key, val, meta))
			}
		}
	}
}

impl fmt::Display for Map {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let mut first: bool = true;
		for (key, val, meta) in self.iter() {
			write!(
				f,
				"{spacer}{ephemeral}{key}=",
				spacer = if first { "" } else { " " },
				ephemeral = if meta.get(MetadataField::Ephemeral) { "*" } else { "" }
			)?;
			val.write_fmt(f, self)?;
			first = false;
		}

		Ok(())
	}
}

/* ----------------------- Tests ----------------------- */

#[cfg(test)]
mod map {
	use super::*;

	#[test]
	fn indexed_keys_order() {
		let mut attr = Map::new();

		attr.set("key_a", &Value::from(123), true);
		attr.set("key_b", &Value::from(456), false);
		attr.set("key_c", &Value::from(&[Scalar::from(789), Scalar::from("abc")]), true);
		attr.set("key_b", &Value::from("overwrites should not change key order"), true);
		attr.set("error", &Value::from("priority keys should go first"), false);

		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 5);
		assert_eq!(attr.idx_by_key("error"), Some(0));
		assert_eq!(attr.idx_by_key("key_a"), Some(1));
		assert_eq!(attr.idx_by_key("key_b"), Some(2));
		assert_eq!(attr.idx_by_key("key_c"), Some(3));
		assert_eq!(attr.idx_by_key("bad_key"), None);
	}

	#[test]
	fn basic_operations() {
		let mut attr = Map::new();

		assert_eq!(attr.len(), 0);
		assert_eq!(attr.store_size(), 0);

		attr.set("c", &Value::from(-5678), true);
		attr.set("d", &Value::from(9012.3456), false);
		attr.set("b", &Value::from(1234), true);
		assert_eq!(attr.len(), 3);
		assert_eq!(attr.store_size(), 3);
		assert_eq!(attr.to_string(), "*c=-5678 d=9012.3456 *b=1234");

		// overwrite existing key
		attr.set("d", &Value::from(7890.1234), true);
		attr.set("error", &Value::from("first!"), false);
		attr.set("e", &Value::from(&[Scalar::from(7788 as usize), Scalar::from(9900)]), true);
		attr.set("a", &Value::Scalar(Scalar::from("lalala")), false);
		assert_eq!(attr.len(), 6);
		assert_eq!(attr.store_size(), 7);
		assert_eq!(attr.to_string(), "error=\"first!\" *c=-5678 *d=7890.1234 *b=1234 *e=[0x1e6c, 9900] a=\"lalala\"");
	}

	#[test]
	fn key_overwrite() {
		let mut attr = Map::new();

		attr.set("a", &Value::from(&[Scalar::from(1234), Scalar::from(-5678)]), false);
		attr.set("b", &Value::from("lalala"), true);
		attr.set("c", &Value::from(&[Scalar::from(true), Scalar::from(false), Scalar::from(true)]), false);
		attr.set("d", &Value::from(false), true);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 7);
		assert_eq!(attr.to_string(), "a=[1234, -5678] *b=\"lalala\" c=[true, false, true] *d=false");

		// same size overwrite
		attr.set("b", &Value::from(123.456), false);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 7);
		assert_eq!(attr.to_string(), "a=[1234, -5678] b=123.456 c=[true, false, true] *d=false");

		// overwrite with size increasee
		attr.set("b", &Value::from(&[Scalar::from(1), Scalar::from(2), Scalar::from(3), Scalar::from(4)]), true);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 10);
		assert_eq!(attr.to_string(), "a=[1234, -5678] *b=[1, 2, 3, 4] c=[true, false, true] *d=false");

		// overwrite with size decrease
		attr.set("c", &Value::from("lololo"), true);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 8);
		assert_eq!(attr.to_string(), "a=[1234, -5678] *b=[1, 2, 3, 4] *c=\"lololo\" *d=false");

		// modify the first and last attribute sizes to check edge handling
		attr.set("a", &Value::from(1234), true);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 7);
		assert_eq!(attr.to_string(), "*a=1234 *b=[1, 2, 3, 4] *c=\"lololo\" *d=false");

		attr.set("d", &Value::from((&[Scalar::from("sub_a"), Scalar::from("sub_b")], &[Scalar::from(true), Scalar::from(false)])), false);
		assert_eq!(attr.len(), 4);
		assert_eq!(attr.store_size(), 10);
		assert_eq!(attr.to_string(), "*a=1234 *b=[1, 2, 3, 4] *c=\"lololo\" d={\"sub_a\": true, \"sub_b\": false}");
	}

	#[test]
	fn string_pool_behavior() {
		let mut attr = Map::new();

		attr.insert(
			"key_a",
			Value::from(&[
				Scalar::from(String::from("string #1")),
				Scalar::from(String::from("then string #2")),
				Scalar::from(String::from("finally string #3")),
			]),
		);
		attr.insert(
			"key_b",
			Value::from((
				&[
					Scalar::from(String::from("sub key 1")),
					Scalar::from(String::from("sub key #2")),
					Scalar::from(String::from("and sub key 3")),
				],
				&[Scalar::from(String::from("lala")), Scalar::from(String::from("lelele")), Scalar::from(String::from("lolololo"))],
			)),
		);
		attr.insert("key_c", Value::from("static_string"));
		attr.insert("key_d", Value::from(String::from("heap string")));

		assert_eq!(
			attr.to_string(),
			"key_a=[\"string #1\", \"then string #2\", \"finally string #3\"] key_b={\"sub key 1\": \"lala\", \"sub key #2\": \"lelele\", \"and sub key 3\": \"lolololo\"} key_c=\"static_string\" key_d=\"heap string\"",
		);
		assert_eq!(
			attr.string_pool,
			"key_astring #1then string #2finally string #3key_bsub key 1sub key #2and sub key 3lalalelelelolololokey_ckey_dheap string"
		);

		// replace pooled string list with a static list of the same size.
		attr.insert("key_a", Value::from(&[Scalar::from(123), Scalar::from("boo"), Scalar::from(456)]));
		assert_eq!(
			attr.to_string(),
			"key_a=[123, \"boo\", 456] key_b={\"sub key 1\": \"lala\", \"sub key #2\": \"lelele\", \"and sub key 3\": \"lolololo\"} key_c=\"static_string\" key_d=\"heap string\""
		);
		assert_eq!(attr.string_pool, "key_akey_bsub key 1sub key #2and sub key 3lalalelelelolololokey_ckey_dheap string");

		// replace string list with different size map
		attr.insert(
			"key_a",
			Value::from((
				&[Scalar::from(String::from("new sub key 1")), Scalar::from(String::from("with sub key 2"))],
				&[Scalar::from(3.14159), Scalar::from("i'm static!")],
			)),
		);
		assert_eq!(
			attr.to_string(),
			"key_a={\"new sub key 1\": 3.14159, \"with sub key 2\": \"i\\'m static!\"} key_b={\"sub key 1\": \"lala\", \"sub key #2\": \"lelele\", \"and sub key 3\": \"lolololo\"} key_c=\"static_string\" key_d=\"heap string\""
		);
		assert_eq!(
			attr.string_pool,
			"key_akey_bsub key 1sub key #2and sub key 3lalalelelelolololokey_ckey_dheap stringnew sub key 1with sub key 2"
		);

		// replace map with different sized list
		attr.insert("key_b", Value::from(&[Scalar::from(String::from("new string")), Scalar::from(12345)]));
		assert_eq!(
			attr.to_string(),
			"key_a={\"new sub key 1\": 3.14159, \"with sub key 2\": \"i\\'m static!\"} key_b=[\"new string\", 12345] key_c=\"static_string\" key_d=\"heap string\""
		);
		assert_eq!(attr.string_pool, "key_akey_bkey_ckey_dheap stringnew sub key 1with sub key 2new string");

		attr.insert("key_a", Value::from(1111));
		attr.insert("key_b", Value::from(2222));
		assert_eq!(attr.to_string(), "key_a=1111 key_b=2222 key_c=\"static_string\" key_d=\"heap string\"");
		assert_eq!(attr.string_pool, "key_akey_bkey_ckey_dheap string",);
	}

	#[test]
	#[should_panic]
	fn insert_empty_key() {
		Map::new().set("", &Value::from("oh no"), false);
	}

	#[test]
	#[should_panic]
	fn insert_whitespaces_key() {
		Map::new().set("no whitespace\tin\tkeys", &Value::from("please!"), true);
	}

	#[test]
	#[should_panic]
	fn insert_non_ascii() {
		Map::new().set("como_estás", &Value::from(1234), false);
	}

	#[test]
	#[should_panic]
	fn insert_unicode_key() {
		Map::new().set("oh❤pretty!", &Value::from(5678), true);
	}

	#[test]
	#[should_panic]
	fn insert_restricted_key() {
		Map::new().set("level", &Value::from(55555), false);
	}

	#[test]
	#[should_panic]
	fn insert_empty_list() {
		Map::new().set("a_key", &Value::from(&[]), true);
	}

	#[test]
	#[should_panic]
	fn invalid_empty_map() {
		Map::new().set("wrong_map", &Value::from((&[], &[])), false);
	}

	#[test]
	#[should_panic]
	fn insert_invalid_map() {
		Map::new().set(
			"wrong_map",
			&Value::Map(
				[Scalar::from("key_a"), Scalar::from("key_b"), Scalar::from("key_c")].as_slice(),
				[Scalar::from(123), Scalar::from("oh no")].as_slice(),
			),
			true,
		);
	}

	#[test]
	#[should_panic]
	fn insert_duplicate_key_map() {
		Map::new().set(
			"wrong_map",
			&Value::Map(
				[Scalar::from("key_a"), Scalar::from("key_b"), Scalar::from("key_a")].as_slice(),
				[Scalar::from(123), Scalar::from(456.789), Scalar::from("oh no")].as_slice(),
			),
			false,
		);
	}

	#[test]
	fn key_iterator() {
		let mut attr = Map::new();

		attr.insert("key_a", Value::from(123));
		attr.insert("key_b", Value::from(&[Scalar::from(456), Scalar::from(true)]));
		attr.insert("error", Value::from("first error"));
		attr.insert("key_c", Value::from(&[Scalar::from(789), Scalar::from(false)]));
		attr.insert("key_d", Value::from(3.14159));

		let mut got_keys: Vec<&str> = Vec::new();
		for key in attr.key_iter() {
			got_keys.push(key);
		}

		assert_eq!(got_keys, &["error", "key_a", "key_b", "key_c", "key_d"]);
	}

	#[test]
	fn key_value_iterator() {
		let mut attr = Map::new();

		attr.insert("key_a", Value::from(123));
		attr.insert("key_b", Value::from(&[Scalar::from(456), Scalar::from(true)]));
		attr.insert("key_c", Value::from(&[Scalar::from(789), Scalar::from(false)]));
		attr.insert("error", Value::from("an error"));
		attr.insert("key_d", Value::from(&[Scalar::from("new"), Scalar::from("key")]));

		let mut got: Vec<(&str, Value)> = Vec::new();
		for kvs in attr.key_value_iter() {
			got.push((kvs.0, kvs.1));
		}

		assert_eq!(
			got,
			&[
				("error", Value::from("an error")),
				("key_a", Value::from(123)),
				("key_b", Value::from(&[Scalar::from(456), Scalar::from(true)])),
				("key_c", Value::from(&[Scalar::from(789), Scalar::from(false)])),
				("key_d", Value::from(&[Scalar::from("new"), Scalar::from("key")])),
			]
		);
	}

	#[test]
	fn full_iterator() {
		let mut attr = Map::new();

		attr.insert("key_a", Value::from(123));
		attr.insert("key_b", Value::from(&[Scalar::from(456), Scalar::from(true)]));
		attr.insert("key_c", Value::from(&[Scalar::from(789), Scalar::from(false)]));
		attr.insert("error", Value::from("an error"));
		attr.insert("key_d", Value::from(&[Scalar::from("new"), Scalar::from("key")]));

		let mut got: Vec<(&str, Value, Metadata)> = Vec::new();
		for kvs in attr.iter() {
			got.push(kvs);
		}

		assert_eq!(
			got,
			&[
				("error", Value::from("an error"), 0b110),
				("key_a", Value::from(123), 0),
				("key_b", Value::from(&[Scalar::from(456), Scalar::from(true)]), 0),
				("key_c", Value::from(&[Scalar::from(789), Scalar::from(false)]), 0),
				("key_d", Value::from(&[Scalar::from("new"), Scalar::from("key")]), 0),
			]
		);
	}
}