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
//! # Anvil
//!
//! Anvil is a modular templating system for creating user-defined scaffolding systems.
//! It provides a composable API for file operations like generating, appending, transforming,
//! and moving files.
//!
//! ## Core Concepts
//!
//! Anvil is built around two primary traits:
//!
//! * [`Anvil`] - The base trait for template rendering engines
//! * [`Forge`] - The base trait for file operations using rendered templates
//!
//! Think of `Anvil` as the template you render, and `Forge` as what you do with that rendered content
//! (create a file, append to a file, transform a file, etc.).
//!
//! ## Design Philosophy
//!
//! - **Configuration is code**: Your scaffolding logic is defined directly in code, enabling
//! compile-time checking and integration with your application.
//! - **Compile time errors are better than runtime errors**: Detect issues at compile time whenever possible.
//! - **The library provides the building blocks, not the solutions**: Anvil gives you composable
//! components to build your own custom scaffolding systems.
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use anvil::{Anvil, Forge, generate::Generate};
//! use std::io::Write;
//!
//! // Simple implementation of the Anvil trait
//! struct SimpleTemplate {
//! content: String,
//! }
//!
//! impl Anvil for SimpleTemplate {
//! type Error = std::io::Error;
//!
//! fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
//! writer.write_all(self.content.as_bytes())?;
//! Ok(())
//! }
//! }
//!
//! // Using Generate for file creation
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let template = SimpleTemplate {
//! content: "Hello, Anvil!".to_string(),
//! };
//!
//! // Create a file generator using our template
//! let generator = Generate::new(template);
//!
//! // Generate the file
//! generator.forge("./output.txt")?;
//!
//! println!("File generated successfully!");
//! Ok(())
//! }
//! ```
//!
//! ## Inspiration and Credits
//!
//! - [Laravel Artisan](https://laravel.com/docs/11.x/artisan)
//! - [Rails Generators](https://guides.rubyonrails.org/generators.html)
//! - [Loco.rs](https://loco.rs/docs/getting-started/tour/#adding-a-crud-api)
//! - [Cargo Generate](https://github.com/cargo-generate/cargo-generate)
//! - [Cookiecutter actix simple clean architecture](https://github.com/microsoft/cookiecutter-rust-actix-clean-architecture)
//!
/// Module for appending content to existing files.
///
/// The operations in this module allow you to add content to the end of existing files
/// using the [`Append`](`append::Append`) struct.
///
/// # Example
///
/// ```rust,no_run
/// use anvil::{Anvil, Forge, append::Append};
/// use std::io::Write;
///
/// // Simple template that renders fixed content
/// struct SimpleTemplate {
/// content: String,
/// }
///
/// impl Anvil for SimpleTemplate {
/// type Error = std::io::Error;
///
/// fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
/// writer.write_all(self.content.as_bytes())?;
/// Ok(())
/// }
/// }
///
/// // Create a template and append it to a file
/// fn append_to_file() -> Result<(), Box<dyn std::error::Error>> {
/// let template = SimpleTemplate {
/// content: "\nAppended content".to_string(),
/// };
///
/// let append_op = Append::new(template);
/// append_op.forge("./existing_file.txt")?;
///
/// Ok(())
/// }
/// ```
/// Module for fallback mechanisms between two operations.
///
/// This module provides the [`Either`](`either::Either`) struct for creating fallback operations - if the first
/// operation fails, the second one will be attempted.
///
/// # Example
///
/// ```
/// use anvil::{Anvil, Forge, generate::Generate, either::{either, Either}};
/// use std::io::Write;
///
/// // Helper trait implementation (defined elsewhere)
/// struct TemplateA;
/// struct TemplateB;
///
/// impl Anvil for TemplateA {
/// type Error = std::io::Error;
/// fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
/// writer.write_all(b"Content from TemplateA")?;
/// Ok(())
/// }
/// }
///
/// impl Anvil for TemplateB {
/// type Error = std::io::Error;
/// fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
/// writer.write_all(b"Content from TemplateB")?;
/// Ok(())
/// }
/// }
///
/// // Try to use TemplateA, but fall back to TemplateB if it fails
/// fn create_with_fallback() -> Result<(), Box<dyn std::error::Error>> {
/// let primary_generator = Generate::new(TemplateA);
/// let fallback_generator = Generate::new(TemplateB);
///
/// // Create an Either operation with both generators
/// let operation = either(primary_generator, fallback_generator);
///
/// // Execute the operation with fallback
/// // Using a fake path for doc testing
/// let result = operation.forge("fake_path.txt");
/// // Just show we can handle the result
/// if result.is_ok() {
/// println!("Operation succeeded!");
/// }
///
/// Ok(())
/// }
/// ```
/// Module for creating files from templates.
///
/// This module provides the [`Generate`](`generate::Generate`) struct for generating new files from templates.
/// The file and parent directories will be created if they don't exist.
///
/// # Example
///
/// ```rust,no_run
/// use anvil::{Anvil, Forge, generate::Generate};
/// use std::io::Write;
///
/// // Simple template implementation
/// struct SimpleTemplate {
/// content: String,
/// }
///
/// impl Anvil for SimpleTemplate {
/// type Error = std::io::Error;
///
/// fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
/// writer.write_all(self.content.as_bytes())?;
/// Ok(())
/// }
/// }
///
/// // Generate a new file
/// fn generate_file() -> Result<(), Box<dyn std::error::Error>> {
/// let template = SimpleTemplate {
/// content: "# New File\n\nThis is a generated file.".to_string(),
/// };
///
/// let generator = Generate::new(template);
/// generator.forge("./path/to/new_file.md")?;
///
/// Ok(())
/// }
/// ```
/// Module for moving or renaming files.
///
/// This module provides the [`Move`](`mover::Move`) struct for moving or renaming files.
///
/// # Example
///
/// ```rust,no_run
/// use anvil::{Forge, mover::Move};
///
/// // Rename a file from old name to new name
/// fn rename_file() -> Result<(), Box<dyn std::error::Error>> {
/// let mover = Move::new("./old_name.txt");
/// mover.forge("./new_name.txt")?;
///
/// Ok(())
/// }
/// ```
/// Module for transforming the content of existing files.
///
/// This module provides the [`Transform`](`transform::Transform`) struct for reading, modifying,
/// and writing back file content.
///
/// # Example
///
/// ```rust,no_run
/// use anvil::{Forge, transform::Transform};
///
/// // Transform a file by adding a line to the beginning
/// fn add_header_comment() -> Result<(), Box<dyn std::error::Error>> {
/// let transform = Transform::new(|content| {
/// Ok(format!("// Generated file - do not edit directly\n{}", content))
/// });
///
/// transform.forge("./src/generated.rs")?;
///
/// Ok(())
/// }
/// ```
use ;
/// The core trait for template rendering engines.
///
/// `Anvil` provides the foundational functionality for rendering templates into strings
/// or writing them directly to files. Any type that implements this trait can be used
/// with the file operation types (Generate, Append, etc).
///
/// # Type Parameters
///
/// * `Error` - The error type that the rendering engine can produce.
///
/// # Implementation
///
/// When implementing this trait, you should provide the rendering logic in the `anvil` method,
/// which writes the rendered template to the provided writer.
///
/// # Examples
///
/// ```
/// use anvil::Anvil;
/// use std::io::Write;
///
/// struct SimpleTemplate {
/// content: String,
/// }
///
/// impl Anvil for SimpleTemplate {
/// type Error = std::io::Error;
///
/// fn anvil(&self, writer: &mut (impl Write + Sized)) -> Result<(), Self::Error> {
/// writer.write_all(self.content.as_bytes())?;
/// Ok(())
/// }
/// }
/// ```
/// The core trait for file operations.
///
/// `Forge` represents operations that can create, modify, or transform files.
/// It defines a uniform interface for all file manipulation actions in the Anvil system.
///
/// # Type Parameters
///
/// * `Error` - The error type that this operation can produce.
///
/// # Implementation
///
/// When implementing this trait, you should provide the file operation logic in the `forge` method,
/// which typically creates or modifies a file at the specified path.
///
/// # Examples
///
/// ```
/// use anvil::Forge;
/// use std::path::Path;
///
/// // A simple file creator
/// struct EmptyFileCreator;
///
/// impl Forge for EmptyFileCreator {
/// type Error = std::io::Error;
///
/// fn forge(&self, into: impl AsRef<Path>) -> Result<(), Self::Error> {
/// let path = into.as_ref();
/// std::fs::File::create(path)?;
/// Ok(())
/// }
/// }
/// ```