ploidy_codegen_rust/
graph.rs

1use std::ops::Deref;
2
3use ploidy_core::{
4    codegen::UniqueNames,
5    ir::{IrGraph, PrimitiveIrType, View},
6};
7
8use super::{config::CodegenConfig, naming::CodegenIdentScope};
9
10/// Decorates an [`IrGraph`] with Rust-specific information.
11#[derive(Debug)]
12pub struct CodegenGraph<'a>(IrGraph<'a>);
13
14impl<'a> CodegenGraph<'a> {
15    /// Wraps a type graph with the default configuration.
16    pub fn new(graph: IrGraph<'a>) -> Self {
17        Self::with_config(graph, &CodegenConfig::default())
18    }
19
20    /// Wraps a type graph with the given configuration.
21    pub fn with_config(graph: IrGraph<'a>, config: &CodegenConfig) -> Self {
22        // Decorate named schema types with their Rust identifier names.
23        let unique = UniqueNames::new();
24        let mut scope = CodegenIdentScope::new(&unique);
25        for mut view in graph.schemas() {
26            let ident = scope.uniquify(view.name());
27            view.extensions_mut().insert(ident);
28        }
29        // Decorate `DateTime` primitives with the format.
30        for mut view in graph
31            .primitives()
32            .filter(|view| matches!(view.ty(), PrimitiveIrType::DateTime))
33        {
34            view.extensions_mut().insert(config.date_time_format);
35        }
36        Self(graph)
37    }
38}
39
40impl<'a> Deref for CodegenGraph<'a> {
41    type Target = IrGraph<'a>;
42
43    fn deref(&self) -> &Self::Target {
44        &self.0
45    }
46}