csw_generate/generator.rs
1//! Code generator traits and utilities.
2
3use csw_derive::TypeSystem;
4use std::path::Path;
5
6/// Trait for code generators.
7///
8/// Implement this trait to add support for generating code in a new language.
9pub trait Generator {
10 /// The error type for this generator.
11 type Error: std::error::Error;
12
13 /// Generate code for the given type system.
14 ///
15 /// # Arguments
16 ///
17 /// * `ts` - The type system to generate code for
18 /// * `output_dir` - Directory to write generated files to
19 fn generate(ts: &TypeSystem, output_dir: &Path) -> Result<(), Self::Error>;
20}
21
22/// Options for code generation.
23#[derive(Clone, Debug, Default)]
24pub struct GeneratorOptions {
25 /// Generate a type checker
26 pub checker: bool,
27
28 /// Generate an interpreter
29 pub interpreter: bool,
30
31 /// Generate a parser
32 pub parser: bool,
33
34 /// Generate documentation
35 pub docs: bool,
36
37 /// Generate tests
38 pub tests: bool,
39}
40
41impl GeneratorOptions {
42 /// Create options for generating everything.
43 pub fn all() -> Self {
44 Self {
45 checker: true,
46 interpreter: true,
47 parser: true,
48 docs: true,
49 tests: true,
50 }
51 }
52
53 /// Create options for generating only the checker.
54 pub fn checker_only() -> Self {
55 Self {
56 checker: true,
57 ..Default::default()
58 }
59 }
60}