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
use std::borrow::Cow;
use crate::event::Atom;
use crate::ser::{MapEmitter, SeqEmitter, StructEmitter};
pub enum Chunk<'a> {
Atom(Atom<'a>),
Struct(Box<dyn StructEmitter + 'a>),
Map(Box<dyn MapEmitter + 'a>),
Seq(Box<dyn SeqEmitter + 'a>),
}
impl<'a> From<Atom<'a>> for Chunk<'a> {
fn from(atom: Atom<'a>) -> Self {
Chunk::Atom(atom)
}
}
macro_rules! impl_from {
($ty:ty, $atom:ident) => {
impl From<$ty> for Chunk<'static> {
fn from(value: $ty) -> Self {
Chunk::Atom(Atom::$atom(value as _))
}
}
};
}
impl_from!(u64, U64);
impl_from!(i64, I64);
impl_from!(f64, F64);
impl_from!(usize, U64);
impl_from!(isize, I64);
impl_from!(bool, Bool);
impl_from!(char, Char);
impl From<()> for Chunk<'static> {
fn from(_: ()) -> Chunk<'static> {
Chunk::Atom(Atom::Null)
}
}
impl<'a> From<&'a str> for Chunk<'a> {
fn from(value: &'a str) -> Chunk<'a> {
Chunk::Atom(Atom::Str(Cow::Borrowed(value)))
}
}
impl From<String> for Chunk<'static> {
fn from(value: String) -> Chunk<'static> {
Chunk::Atom(Atom::Str(Cow::Owned(value)))
}
}