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
/*!
## Example
A silly example of using codebiber to mix autogenerated code with handwritten code.
Here, the original code contans handwritten lines
```c
void handwritten_line1();
void handwritten_line2();
```
```c
void handwritten_line3();
```
```c
void handwritten_line4();
```
```c
void handwritten_line5();
```
and some sections marked to be overwritten
```c
// << codegen foo >>
// << /codegen >>
```
```c
// << codegen bar >>
// << /codegen >>
```
and a section that was already generated by another function
```c
// << codegen baz >>
void generated_line_by_some_other_function();
// << /codegen >>
```
The function [`generate`] accepts the input code, a [configuration](Config) and
most important the function which will generate the code.
In our example, the function will be called once for each section.
Each time it accepts the section name (in our case `foo`, `bar` or `baz`) and
returns the generated code for this section.
If the section is not its responsibility, it can also return Ok(None) and the
previous content will be used again.
In our example, the generator function `gen_code_lines` returns new code for the
sections `foo` and `bar` while leaving `baz` untouched.
This results in the generated code section
```c
// << codegen foo >>
void autogen_line_foo();
// << /codegen 8fb912f5 >>
```
```c
// << codegen bar >>
void autogen_line_bar1();
void autogen_line_bar2();
// << /codegen 88c5186d >>
```
```c
// << codegen baz >>
void generated_line_by_some_other_function();
// << /codegen ded33021 >>
```
Note the hashsums. They protect against overwritting accidental modifications.
They are simply a crc32 hahsum.
```rust
extern crate codebiber;
const INPUT : &str = r"
void handwritten_line1();
void handwritten_line2();
// << codegen foo >>
// << /codegen 00000000 >>
void handwritten_line3();
// << codegen bar >>
// << /codegen 00000000 >>
void handwritten_line4();
// << codegen baz >>
void generated_line_by_some_other_function();
// << /codegen ded33021 >>
void handwritten_line5();
";
fn main() -> codebiber::Result
{
let actual_output = codebiber::generate(INPUT, gen_code_lines)?;
assert_eq!(actual_output, Some(EXPECTED_OUTPUT.to_owned()));
Ok(())
}
fn gen_code_lines(name: &str) -> codebiber::Fmt_Result
{
let generated = match name
{
"foo" => Some("void autogen_line_foo();".to_owned()),
"bar" => Some("void autogen_line_bar1();\nvoid autogen_line_bar2();".to_owned()),
_ => None,
};
Ok(generated)
}
const EXPECTED_OUTPUT : &str = r"
void handwritten_line1();
void handwritten_line2();
// << codegen foo >>
void autogen_line_foo();
// << /codegen 8fb912f5 >>
void handwritten_line3();
// << codegen bar >>
void autogen_line_bar1();
void autogen_line_bar2();
// << /codegen 88c5186d >>
void handwritten_line4();
// << codegen baz >>
void generated_line_by_some_other_function();
// << /codegen ded33021 >>
void handwritten_line5();
";
```
Please not how the generated code in the `bar` section is indented.
That's because the generated code inherits the indentation of the marker line
`// << codegen bar >>`
# Example 2
While in this example all marker lines start with `//`, marker lines can start
and with any characters as long as they don't end with `<` and don't contain
`<<`.
```rust
extern crate codebiber;
const INPUT : &str = r"
/* << codegen foo >> */
(* << /codegen >> *)
#if 0 // << codegen bar >>
#endif // << /codegen >>
## << codegen bar >> For python like languages
-- << /codegen >> Note how you can also write stuff after the marker
esoteric langugage using keywords << codegen bar >> for comments
<< /codegen >>
";
fn main() -> codebiber::Result
{
let actual_output = codebiber::generate(INPUT, gen_code_lines)?;
assert_eq!(actual_output, Some(EXPECTED_OUTPUT.to_owned()));
Ok(())
}
fn gen_code_lines(name: &str) -> codebiber::Fmt_Result
{
let generated = match name
{
"foo" => Some("void autogen_line_foo();".to_owned()),
"bar" => Some("void autogen_line_bar1();\nvoid autogen_line_bar2();".to_owned()),
_ => None,
};
Ok(generated)
}
const EXPECTED_OUTPUT : &str = r"
/* << codegen foo >> */
void autogen_line_foo();
(* << /codegen 8fb912f5 >> *)
#if 0 // << codegen bar >>
void autogen_line_bar1();
void autogen_line_bar2();
#endif // << /codegen 88c5186d >>
## << codegen bar >> For python like languages
void autogen_line_bar1();
void autogen_line_bar2();
-- << /codegen 88c5186d >> Note how you can also write stuff after the marker
esoteric langugage using keywords << codegen bar >> for comments
void autogen_line_bar1();
void autogen_line_bar2();
<< /codegen 88c5186d >>
";
```
Also note how every `bar` section shares the same hashsum. Thats because the
hashsum is generated before indenting the code.
*/
pub use Indentation;
pub use ;
pub use ;