rgen-core 0.1.0

Core graph-aware code generation engine
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use anyhow::Result;
use gray_matter::{engine::YAML, Matter, ParsedEntity};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tera::{Context, Tera};

use crate::graph::Graph;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Frontmatter {
    // Hygen core
    pub to: Option<String>,
    pub from: Option<String>,
    #[serde(default)]
    pub force: bool,
    #[serde(default)]
    pub unless_exists: bool,

    // Injection
    #[serde(default)]
    pub inject: bool,
    pub before: Option<String>,
    pub after: Option<String>,
    #[serde(default)]
    pub prepend: bool,
    #[serde(default)]
    pub append: bool,
    pub at_line: Option<u32>,
    #[serde(default)]
    pub eof_last: bool,
    pub skip_if: Option<String>,

    // Shell hooks
    #[serde(alias = "sh")]
    pub sh_before: Option<String>,
    pub sh_after: Option<String>,

    // Graph additions (renderable)
    #[serde(default)]
    pub base: Option<String>,
    #[serde(default)]
    pub prefixes: BTreeMap<String, String>,
    #[serde(default, deserialize_with = "string_or_seq")]
    pub rdf_inline: Vec<String>,
    #[serde(default, deserialize_with = "string_or_seq")]
    pub rdf: Vec<String>, // treat as inline TTL in prototype
    #[serde(default, deserialize_with = "sparql_map")]
    pub sparql: BTreeMap<String, String>,

    // Optional template variables defined in frontmatter
    #[serde(default)]
    pub vars: BTreeMap<String, String>,

    // Safety and idempotency
    #[serde(default)]
    pub backup: Option<bool>,
    #[serde(default)]
    pub idempotent: bool,

    // Additional fields for compatibility
    #[serde(default, deserialize_with = "string_or_seq")]
    pub shape: Vec<String>,
    #[serde(default)]
    pub determinism: Option<serde_yaml::Value>,
}

pub struct Template {
    raw_frontmatter: serde_yaml::Value,
    pub front: Frontmatter, // populated after render_frontmatter()
    pub body: String,
}

impl Template {
    /// Parse frontmatter + body. Does NOT render yet.
    pub fn parse(input: &str) -> Result<Self> {
        let matter = Matter::<YAML>::new();
        let ParsedEntity { data, content, .. } = matter.parse::<serde_yaml::Value>(input)?;
        let raw_frontmatter = data.unwrap_or(serde_yaml::Value::Null);
        Ok(Self {
            raw_frontmatter,
            front: Frontmatter::default(),
            body: content,
        })
    }

    /// Render frontmatter through Tera once to resolve {{ }} in YAML.
    pub fn render_frontmatter(&mut self, tera: &mut Tera, vars: &Context) -> Result<()> {
        let yaml_src = serde_yaml::to_string(&self.raw_frontmatter)?;
        let rendered_yaml = tera.render_str(&yaml_src, vars)?;
        self.front = serde_yaml::from_str::<Frontmatter>(&rendered_yaml)?;
        Ok(())
    }

    /// Load RDF and run SPARQL using the rendered frontmatter.
    pub fn process_graph(
        &mut self, graph: &mut Graph, tera: &mut Tera, vars: &Context, template_path: &std::path::Path,
    ) -> Result<()> {
        // Ensure frontmatter is rendered before graph ops
        if self.front.to.is_none()
            && self.front.from.is_none()
            && self.front.rdf_inline.is_empty()
            && self.front.rdf.is_empty()
            && self.front.sparql.is_empty()
        {
            self.render_frontmatter(tera, vars)?;
        }

        // Build prolog once
        let prolog = crate::graph::build_prolog(&self.front.prefixes, self.front.base.as_deref());

        // Insert inline RDF
        for ttl in &self.front.rdf_inline {
            let ttl_rendered = tera.render_str(ttl, vars)?;
            let final_ttl = if prolog.is_empty() {
                ttl_rendered
            } else {
                format!("{prolog}\n{ttl_rendered}")
            };
            graph.insert_turtle(&final_ttl)?;
        }

        // Load RDF files - resolve relative to template directory
        for rdf_file in &self.front.rdf {
            let rendered_path = tera.render_str(rdf_file, vars)?;
            
            // Resolve relative to template's directory
            let template_dir = template_path.parent().unwrap_or(std::path::Path::new("."));
            let rdf_path = template_dir.join(&rendered_path);
            
            if let Ok(ttl_content) = std::fs::read_to_string(&rdf_path) {
                let final_ttl = if prolog.is_empty() {
                    ttl_content
                } else {
                    format!("{prolog}\n{ttl_content}")
                };
                graph.insert_turtle(&final_ttl)?;
            }
        }

        // Execute SPARQL (prepend PREFIX/BASE prolog)
        for q in self.front.sparql.values() {
            let q_rendered = tera.render_str(q, vars)?;
            let final_q = if prolog.is_empty() {
                q_rendered
            } else {
                format!("{prolog}\n{q_rendered}")
            };
            let _ = graph.query(&final_q)?;
        }

        Ok(())
    }

    /// Render template body with Tera.
    /// If `from:` is specified in frontmatter, read that file and use as body source.
    pub fn render(&self, tera: &mut Tera, vars: &Context) -> Result<String> {
        let body_source = if let Some(from_path) = &self.front.from {
            // Render the from path as a template to resolve variables
            let rendered_from = tera.render_str(from_path, vars)?;
            std::fs::read_to_string(&rendered_from).map_err(|e| {
                anyhow::anyhow!("Failed to read from file '{}': {}", rendered_from, e)
            })?
        } else {
            self.body.clone()
        };

        Ok(tera.render_str(&body_source, vars)?)
    }
}

/* ---------------- helpers ---------------- */

// Accept either "rdf: <string>" or "rdf: [<string>, ...]"
fn string_or_seq<'de, D>(de: D) -> Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de::{Error as DeError, SeqAccess, Visitor};
    use std::fmt;

    struct StrOrSeq;

    impl<'de> Visitor<'de> for StrOrSeq {
        type Value = Vec<String>;

        fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.write_str("a string or a sequence of strings")
        }

        fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
        where
            E: DeError,
        {
            Ok(vec![v.to_string()])
        }

        fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
        where
            E: DeError,
        {
            Ok(vec![v])
        }

        fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut out = Vec::new();
            while let Some(s) = seq.next_element::<String>()? {
                out.push(s);
            }
            Ok(out)
        }
    }

    de.deserialize_any(StrOrSeq)
}

// Accept either "sparql: '<query>'" or "sparql: { name: '<query>' }"
fn sparql_map<'de, D>(de: D) -> Result<BTreeMap<String, String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum OneOrMapOrSeq {
        One(String),
        Map(BTreeMap<String, String>),
        Seq(Vec<String>),
    }
    match OneOrMapOrSeq::deserialize(de)? {
        OneOrMapOrSeq::One(q) => {
            let mut m = BTreeMap::new();
            m.insert("default".to_string(), q);
            Ok(m)
        }
        OneOrMapOrSeq::Map(m) => Ok(m),
        OneOrMapOrSeq::Seq(queries) => {
            let mut m = BTreeMap::new();
            for (i, query) in queries.into_iter().enumerate() {
                m.insert(format!("query_{}", i), query);
            }
            Ok(m)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tera::Context;

    #[test]
    fn test_template_parse_basic() -> Result<()> {
        let input = r#"---
to: "{{name}}.rs"
---
fn main() {
    println!("Hello, {{name}}!");
}"#;
        let template = Template::parse(input)?;

        assert_eq!(
            template.body,
            "fn main() {\n    println!(\"Hello, {{name}}!\");\n}"
        );
        assert!(template.front.to.is_none()); // Not rendered yet

        Ok(())
    }

    #[test]
    fn test_template_parse_no_frontmatter() -> Result<()> {
        let input = r#"fn main() {
    println!("Hello, world!");
}"#;
        let template = Template::parse(input)?;

        assert_eq!(
            template.body,
            "fn main() {\n    println!(\"Hello, world!\");\n}"
        );

        Ok(())
    }

    #[test]
    fn test_template_parse_empty_frontmatter() -> Result<()> {
        let input = r#"---
---
fn main() {
    println!("Hello, world!");
}"#;
        let template = Template::parse(input)?;

        assert_eq!(
            template.body,
            "fn main() {\n    println!(\"Hello, world!\");\n}"
        );

        Ok(())
    }

    #[test]
    fn test_render_frontmatter() -> Result<()> {
        let input = r#"---
to: "{{name}}.rs"
vars:
  greeting: "Hello"
---
fn main() {
    println!("{{greeting}}, {{name}}!");
}"#;
        let mut template = Template::parse(input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        template.render_frontmatter(&mut tera, &vars)?;

        assert_eq!(template.front.to, Some("Alice.rs".to_string()));
        assert_eq!(
            template.front.vars.get("greeting"),
            Some(&"Hello".to_string())
        );

        Ok(())
    }

    #[test]
    fn test_render_frontmatter_with_prefixes() -> Result<()> {
        let input = r#"---
prefixes:
  ex: "http://example.org/"
  rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
base: "http://example.org/{{namespace}}/"
---
fn main() {
    println!("Hello, world!");
}"#;
        let mut template = Template::parse(input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("namespace", "test");

        template.render_frontmatter(&mut tera, &vars)?;

        assert_eq!(
            template.front.prefixes.get("ex"),
            Some(&"http://example.org/".to_string())
        );
        assert_eq!(
            template.front.prefixes.get("rdf"),
            Some(&"http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string())
        );
        assert_eq!(
            template.front.base,
            Some("http://example.org/test/".to_string())
        );

        Ok(())
    }

    #[test]
    fn test_render_frontmatter_with_rdf_inline() -> Result<()> {
        let input = r#"---
rdf_inline:
  - "@prefix ex: <http://example.org/> . ex:{{name}} a ex:Person ."
---
fn main() {
    println!("Hello, world!");
}"#;
        let mut template = Template::parse(input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        template.render_frontmatter(&mut tera, &vars)?;

        assert_eq!(template.front.rdf_inline.len(), 1);
        assert_eq!(
            template.front.rdf_inline[0],
            "@prefix ex: <http://example.org/> . ex:Alice a ex:Person ."
        );

        Ok(())
    }

    #[test]
    fn test_render_frontmatter_with_sparql() -> Result<()> {
        let input = r#"---
sparql:
  people: "SELECT ?person WHERE { ?person a ex:Person . ?person ex:name '{{name}}' }"
---
fn main() {
    println!("Hello, world!");
}"#;
        let mut template = Template::parse(input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        template.render_frontmatter(&mut tera, &vars)?;

        assert_eq!(template.front.sparql.len(), 1);
        assert_eq!(
            template.front.sparql.get("people"),
            Some(
                &"SELECT ?person WHERE { ?person a ex:Person . ?person ex:name 'Alice' }"
                    .to_string()
            )
        );

        Ok(())
    }

    #[test]
    fn test_render_template_body() -> Result<()> {
        let input = r#"---
to: "{{name}}.rs"
---
fn main() {
    println!("Hello, {{name}}!");
}"#;
        let template = Template::parse(input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        let rendered = template.render(&mut tera, &vars)?;

        assert_eq!(rendered, "fn main() {\n    println!(\"Hello, Alice!\");\n}");

        Ok(())
    }

    #[test]
    fn test_render_template_with_from() -> Result<()> {
        use tempfile::NamedTempFile;

        // Create a temporary file with template content
        let mut temp_file = NamedTempFile::new()?;
        writeln!(temp_file, "fn main() {{")?;
        writeln!(temp_file, "    println!(\"Hello, {{{{name}}}}!\");")?;
        writeln!(temp_file, "}}")?;
        temp_file.flush()?;

        let input = format!(
            r#"---
from: "{}"
---
This should be ignored"#,
            temp_file.path().to_str().unwrap()
        );

        let mut template = Template::parse(&input)?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        // Render frontmatter first to populate the 'from' field
        template.render_frontmatter(&mut tera, &vars)?;

        let rendered = template.render(&mut tera, &vars)?;

        assert!(rendered.contains("fn main() {"));
        assert!(rendered.contains("println!(\"Hello, Alice!\");"));
        assert!(!rendered.contains("This should be ignored"));

        Ok(())
    }

    #[test]
    fn test_process_graph_with_rdf_inline() -> Result<()> {
        let input = r#"---
prefixes:
  ex: "http://example.org/"
rdf_inline:
  - "@prefix ex: <http://example.org/> . ex:{{name}} a ex:Person ."
---
fn main() {
    println!("Hello, world!");
}"#;
        let mut template = Template::parse(input)?;
        let mut graph = Graph::new()?;

        let mut tera = Tera::default();
        let mut vars = Context::new();
        vars.insert("name", "Alice");

        template.process_graph(&mut graph, &mut tera, &vars, std::path::Path::new("test.tmpl"))?;

        // Check that the RDF was inserted
        assert!(!graph.is_empty());

        // Query for the inserted data
        let results = graph.query("SELECT ?s WHERE { ?s a <http://example.org/Person> }")?;
        if let oxigraph::sparql::QueryResults::Solutions(mut it) = results {
            let first = it.next().unwrap().unwrap();
            let s = first.get("s").unwrap().to_string();
            assert_eq!(s, "<http://example.org/Alice>");
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }

        Ok(())
    }

    #[test]
    fn test_process_graph_with_sparql() -> Result<()> {
        let input = r#"---
prefixes:
  ex: "http://example.org/"
rdf_inline:
  - "@prefix ex: <http://example.org/> . ex:alice a ex:Person . ex:bob a ex:Person ."
sparql:
  count_people: "SELECT (COUNT(?person) AS ?count) WHERE { ?person a ex:Person }"
---
fn main() {
    println!("Hello, world!");
}"#;
        let mut template = Template::parse(input)?;
        let mut graph = Graph::new()?;

        let mut tera = Tera::default();
        let vars = Context::new();

        template.process_graph(&mut graph, &mut tera, &vars, std::path::Path::new("test.tmpl"))?;

        // Check that the RDF was inserted
        assert!(!graph.is_empty());

        // The SPARQL query should have been executed (though we can't easily test the result)
        // But we can verify the graph has the expected data
        let results = graph.query("SELECT ?s WHERE { ?s a <http://example.org/Person> }")?;
        if let oxigraph::sparql::QueryResults::Solutions(it) = results {
            let count = it.count();
            assert_eq!(count, 2); // alice and bob
        } else {
            return Err(anyhow::anyhow!("Expected Solutions results"));
        }

        Ok(())
    }

    #[test]
    fn test_string_or_seq_deserializer() -> Result<()> {
        // Test string input
        let yaml_str = r#"rdf_inline: "single string""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;
        assert_eq!(frontmatter.rdf_inline, vec!["single string"]);

        // Test array input
        let yaml_array = r#"rdf_inline: ["string1", "string2"]"#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_array)?;
        assert_eq!(frontmatter.rdf_inline, vec!["string1", "string2"]);

        Ok(())
    }

    #[test]
    fn test_sparql_map_deserializer() -> Result<()> {
        // Test single string input
        let yaml_str = r#"sparql: "SELECT ?s WHERE { ?s ?p ?o }""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;
        assert_eq!(
            frontmatter.sparql.get("default"),
            Some(&"SELECT ?s WHERE { ?s ?p ?o }".to_string())
        );

        // Test map input
        let yaml_map = r#"sparql:
  query1: "SELECT ?s WHERE { ?s ?p ?o }"
  query2: "SELECT ?o WHERE { ?s ?p ?o }""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_map)?;
        assert_eq!(
            frontmatter.sparql.get("query1"),
            Some(&"SELECT ?s WHERE { ?s ?p ?o }".to_string())
        );
        assert_eq!(
            frontmatter.sparql.get("query2"),
            Some(&"SELECT ?o WHERE { ?s ?p ?o }".to_string())
        );

        // Test array input
        let yaml_array =
            r#"sparql: ["SELECT ?s WHERE { ?s ?p ?o }", "SELECT ?o WHERE { ?s ?p ?o }"]"#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_array)?;
        assert_eq!(
            frontmatter.sparql.get("query_0"),
            Some(&"SELECT ?s WHERE { ?s ?p ?o }".to_string())
        );
        assert_eq!(
            frontmatter.sparql.get("query_1"),
            Some(&"SELECT ?o WHERE { ?s ?p ?o }".to_string())
        );

        Ok(())
    }

    #[test]
    fn test_frontmatter_defaults() -> Result<()> {
        let yaml_str = r#"to: "test.rs""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;

        // Test default values
        assert_eq!(frontmatter.force, false);
        assert_eq!(frontmatter.unless_exists, false);
        assert_eq!(frontmatter.inject, false);
        assert_eq!(frontmatter.prepend, false);
        assert_eq!(frontmatter.append, false);
        assert_eq!(frontmatter.eof_last, false);
        assert_eq!(frontmatter.idempotent, false);
        assert!(frontmatter.prefixes.is_empty());
        assert!(frontmatter.rdf_inline.is_empty());
        assert!(frontmatter.rdf.is_empty());
        assert!(frontmatter.sparql.is_empty());
        assert!(frontmatter.vars.is_empty());
        assert!(frontmatter.shape.is_empty());

        Ok(())
    }

    #[test]
    fn test_frontmatter_boolean_fields() -> Result<()> {
        let yaml_str = r#"force: true
unless_exists: true
inject: true
prepend: true
append: true
eof_last: true
idempotent: true"#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;

        assert_eq!(frontmatter.force, true);
        assert_eq!(frontmatter.unless_exists, true);
        assert_eq!(frontmatter.inject, true);
        assert_eq!(frontmatter.prepend, true);
        assert_eq!(frontmatter.append, true);
        assert_eq!(frontmatter.eof_last, true);
        assert_eq!(frontmatter.idempotent, true);

        Ok(())
    }

    #[test]
    fn test_frontmatter_injection_fields() -> Result<()> {
        let yaml_str = r#"inject: true
before: "// Before comment"
after: "// After comment"
at_line: 5
skip_if: "existing code""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;

        assert_eq!(frontmatter.inject, true);
        assert_eq!(frontmatter.before, Some("// Before comment".to_string()));
        assert_eq!(frontmatter.after, Some("// After comment".to_string()));
        assert_eq!(frontmatter.at_line, Some(5));
        assert_eq!(frontmatter.skip_if, Some("existing code".to_string()));

        Ok(())
    }

    #[test]
    fn test_frontmatter_shell_hooks() -> Result<()> {
        let yaml_str = r#"sh_before: "echo Before generation"
sh_after: "echo After generation""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;

        assert_eq!(
            frontmatter.sh_before,
            Some("echo Before generation".to_string())
        );
        assert_eq!(
            frontmatter.sh_after,
            Some("echo After generation".to_string())
        );

        Ok(())
    }

    #[test]
    fn test_frontmatter_sh_alias() -> Result<()> {
        let yaml_str = r#"sh: "echo Shell hook""#;
        let frontmatter: Frontmatter = serde_yaml::from_str(yaml_str)?;

        assert_eq!(frontmatter.sh_before, Some("echo Shell hook".to_string()));

        Ok(())
    }
}