-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpack_codegen_docs.rs
151 lines (122 loc) · 5.25 KB
/
pack_codegen_docs.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 crate::pack::*;
use crate::common::*;
use proc_macro2::Span;
use quote::{ToTokens};
use syn::parse_quote;
use crate::utils::*;
pub fn struct_runtime_formatter(parsed: &PackStruct) -> syn::Result<proc_macro2::TokenStream> {
let (impl_generics, ty_generics, where_clause) = parsed.derive_input.generics.split_for_impl();
let name = &parsed.derive_input.ident;
let snake_name = to_snake_case(&name.to_string());
let stdlib_prefix = collections_prefix();
let debug_fields_fn = syn::Ident::new(&format!("debug_fields_{}", snake_name), Span::call_site());
let display_header = format!("{} ({} {})",
name,
parsed.num_bytes,
if parsed.num_bytes == 1 { "byte" } else { "bytes" }
);
let mut debug_fields = vec![];
for field in &parsed.fields {
match field {
FieldKind::Regular { ref ident, ref field } => {
let name_str = &ident.to_string();
let bits: syn::ExprRange = syn::parse_str(&format!("{}..{}", field.bit_range.start, field.bit_range.end))?;
debug_fields.push(quote! {
::packed_struct::debug_fmt::DebugBitField {
name: #name_str.into(),
bits: #bits,
display_value: format!("{:?}", src.#ident).into()
}
});
},
FieldKind::Array { ref ident, ref elements, .. } => {
for (i, field) in elements.iter().enumerate() {
let name_str = format!("{}[{}]", ident, i);
let bits: syn::ExprRange = syn::parse_str(&format!("{}..{}", field.bit_range.start, field.bit_range.end))?;
debug_fields.push(quote! {
::packed_struct::debug_fmt::DebugBitField {
name: #name_str.into(),
bits: #bits,
display_value: format!("{:?}", src.#ident[#i]).into()
}
});
}
}
}
}
let num_fields = debug_fields.len();
let num_bytes = parsed.num_bytes;
let result_ty = result_type();
let q = quote! {
#[doc(hidden)]
pub fn #debug_fields_fn(src: &#name) -> [::packed_struct::debug_fmt::DebugBitField<'static>; #num_fields] {
[#(#debug_fields),*]
}
#[allow(unused_imports)]
impl #impl_generics ::packed_struct::debug_fmt::PackedStructDebug for #name #ty_generics #where_clause {
fn fmt_fields(&self, fmt: &mut #stdlib_prefix::fmt::Formatter) -> #result_ty <(), #stdlib_prefix::fmt::Error> {
use ::packed_struct::PackedStruct;
let fields = #debug_fields_fn(self);
let packed: [u8; #num_bytes] = self.pack()?;
::packed_struct::debug_fmt::packable_fmt_fields(fmt, &packed, &fields)
}
fn packed_struct_display_header() -> &'static str {
#display_header
}
}
#[allow(unused_imports)]
impl #impl_generics #stdlib_prefix::fmt::Display for #name #ty_generics #where_clause {
#[allow(unused_imports)]
fn fmt(&self, f: &mut #stdlib_prefix::fmt::Formatter) -> #stdlib_prefix::fmt::Result {
let display = ::packed_struct::debug_fmt::PackedStructDisplay::new(self);
display.fmt(f)
}
}
};
Ok(q)
}
use std::ops::Range;
use crate::utils_syn::tokens_to_string;
pub fn type_docs(parsed: &PackStruct) -> proc_macro2::TokenStream {
let mut doc = quote! {};
let mut doc_html = |s: &str| {
let p: syn::Attribute = parse_quote! {
#[doc = #s ]
};
p.to_tokens(&mut doc);
};
doc_html(&format!("Structure that can be packed an unpacked into {size_bytes} bytes.\r\n",
size_bytes = parsed.num_bytes
));
doc_html("<table>\r\n");
doc_html("<thead><tr><td>Bit, MSB0</td><td>Name</td><td>Type</td></tr></thead>\r\n");
doc_html("<tbody>\r\n");
{
let mut emit_field_docs = |bits: &Range<usize>, field_ident, ty| {
let bits_str = {
if bits.start == bits.end {
format!("{}", bits.start)
} else {
format!("{}:{}", bits.start, bits.end)
}
};
// todo: friendly integer, reserved types. add LSB/MSB integer info.
doc_html(&format!("<tr><td>{}</td><td>{}</td><td>{}</td></tr>\r\n", bits_str, field_ident, tokens_to_string(ty)));
};
for field in &parsed.fields {
match field {
FieldKind::Regular { ref ident, ref field } => {
emit_field_docs(&field.bit_range, ident.to_string(), &field.ty);
},
FieldKind::Array { ref ident, ref elements, .. } => {
for (i, field) in elements.iter().enumerate() {
emit_field_docs(&field.bit_range, format!("{}[{}]", ident, i), &field.ty);
}
}
}
}
}
doc_html("</tbody>\r\n");
doc_html("</table>\r\n");
doc
}