-
Notifications
You must be signed in to change notification settings - Fork 9
/
tstreamerinfo.rs
151 lines (139 loc) · 4.62 KB
/
tstreamerinfo.rs
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
use std::fmt::Debug;
use nom::{combinator::eof, multi::length_value, number::complete::*, IResult};
use quote::*;
use crate::{
code_gen::rust::{ToNamedRustParser, ToRustParser, ToRustStruct, ToRustType},
code_gen::utils::type_is_core,
core::*,
};
#[derive(Debug)]
#[allow(dead_code)]
pub struct TStreamerInfo {
tstreamerinfo_ver: u16,
named: TNamed,
checksum: u32,
new_class_version: u32,
data_members: Vec<TStreamer>,
}
/// Parse one `TStreamerInfo` object (as found in the `TList`)
pub(crate) fn tstreamerinfo<'s>(
i: &'s [u8],
context: &'s Context,
) -> IResult<&'s [u8], TStreamerInfo> {
let parse_members = |i| tobjarray(|raw_obj, _context| tstreamer(raw_obj), i, context);
let (i, tstreamerinfo_ver) = be_u16(i)?;
let (i, named) = length_value(checked_byte_count, tnamed)(i)?;
let (i, checksum) = be_u32(i)?;
let (i, new_class_version) = be_u32(i)?;
let (i, _size_tobjarray_with_class_info) = checked_byte_count(i)?;
let (i, _class_info_objarray) = classinfo(i)?;
let (i, data_members) = length_value(checked_byte_count, parse_members)(i)?;
let (i, _eof) = eof(i)?;
Ok((
i,
TStreamerInfo {
tstreamerinfo_ver,
named,
checksum,
new_class_version,
data_members,
},
))
}
impl ToRustParser for TStreamerInfo {
/// Generate a parser that can parse an an object described by this TStreamer
#[rustfmt::skip::macros(do_parse)]
fn to_inline_parser(&self) -> Tokens {
if type_is_core(self.named.name.as_str()) {
// Don't generate a parser if its a core type
return quote!(#(self.named.name.to_lowercase()));
}
let struct_name = Ident::new(self.named.name.as_str());
let member_names: &Vec<Ident> =
&self.data_members.iter().map(|m| m.member_name()).collect();
let member_parsers: &Vec<Tokens> = &self
.data_members
.iter()
.map(|m| m.to_inline_parser())
.collect();
quote! {
do_parse!(ver: be_u16 >>
#(#member_names : #member_parsers >> )*
({let phantom = PhantomData;
#struct_name {
phantom,
ver,
#(#member_names),*
}})
)}
}
}
impl ToNamedRustParser for TStreamerInfo {
fn parser_name(&self) -> Tokens {
let ret = Ident::new(self.named.name.to_lowercase());
quote!(#ret)
}
fn to_named_parser(&self) -> Tokens {
if type_is_core(self.named.name.as_str()) {
// Don't generate a parser if its a core type
return quote! {};
}
let parser_name = self.parser_name();
let parser = self.to_inline_parser();
let struct_name = self.type_name();
quote! {
pub fn #parser_name<'s>(input: &'s[u8], context: &'s Context<'s>)
-> IResult<&'s[u8], #struct_name<'s>> {
value!(input, #parser)
}
}
}
}
impl ToRustStruct for TStreamerInfo {
/// Generate a struct corresponding to this TStreamerInfo
fn to_struct(&self) -> Tokens {
if type_is_core(self.named.name.as_str()) {
return quote! {};
}
let name = self.type_name();
let fields = &self.data_members;
let ver_comment = self.type_doc();
quote! {
#[derive(Debug)]
pub struct #name<'s> {
/// Gurantee that this object does not outlive its underlying slice
phantom: PhantomData<&'s[u8]>,
#ver_comment
ver: u16,
#(#fields), *
}
}
}
}
impl ToRustType for TStreamerInfo {
fn type_doc(&self) -> Tokens {
let ret = Ident::new("\n/// Version of the read layout\n");
quote!(#ret)
}
fn type_name(&self) -> Tokens {
let ret = Ident::new(self.named.name.as_str());
quote!(#ret)
}
}
impl TStreamerInfo {
pub(crate) fn to_yaml(&self) -> String {
if type_is_core(self.named.name.as_str()) {
return "".to_string();
};
let mut s = "".to_string();
s += format!("{}:\n", self.named.name).as_str();
s += format!(" version: {}\n", self.new_class_version).as_str();
s += " members:\n";
for obj in &self.data_members {
s += format!(" # {}\n", obj.member_comment()).as_str();
s += format!(" {}: {}\n", obj.member_name(), obj.type_name()).as_str();
}
s += "\n";
s
}
}