Skip to main content

ggen_core/codegen_lib/
mod.rs

1//! Generic code generation framework
2//!
3//! Type-safe, zero-cost abstractions for composable code generators.
4//!
5//! # Core Abstractions
6//!
7//! - `Queryable` trait: Abstracts query execution (e.g., SPARQL SELECT)
8//! - `Renderable` trait: Abstracts template rendering (e.g., Tera)
9//! - `Rule<Q, T>`: Composable rule combining query + template
10//! - `GeneratedFile`: Represents a generated file with content hash
11//! - `GenerationMode`: Specifies how to handle output files
12//!
13//! # Design Principles
14//!
15//! - **Type-first**: Traits encode contracts at compile time
16//! - **Zero-cost**: Generics only, no trait objects or runtime dispatch
17//! - **Composable**: Rules combine orthogonal concerns
18//! - **Deterministic**: Content hashing verifies identical output
19
20pub mod generated_file;
21pub mod generation_mode;
22pub mod queryable;
23pub mod renderable;
24pub mod rule;
25
26pub use generated_file::GeneratedFile;
27pub use generation_mode::GenerationMode;
28pub use queryable::Queryable;
29pub use renderable::Renderable;
30pub use rule::Rule;
31
32/// Error type for code generation
33pub type Result<T> = std::result::Result<T, Error>;
34
35/// Code generation errors
36#[derive(Debug)]
37pub struct Error {
38    message: String,
39}
40
41impl Error {
42    /// Create a new error
43    pub fn new(message: impl Into<String>) -> Self {
44        Self {
45            message: message.into(),
46        }
47    }
48
49    /// Create a template error
50    pub fn template(message: impl Into<String>) -> Self {
51        Self {
52            message: format!("Template error: {}", message.into()),
53        }
54    }
55
56    /// Create a query error
57    pub fn query(message: impl Into<String>) -> Self {
58        Self {
59            message: format!("Query error: {}", message.into()),
60        }
61    }
62}
63
64impl std::fmt::Display for Error {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{}", self.message)
67    }
68}
69
70impl std::error::Error for Error {}