---
to: "generated.rs"
---
{{!--
  Main Template: Generates complete Rust project structure

  This template uses SPARQL queries to extract project metadata from RDF/TTL
  and generates a working Rust library with:
  - Cargo.toml with dependencies
  - src/lib.rs with modules
  - README.md with documentation
--}}

{{#sparql "
PREFIX doap: <http://usefulinc.com/ns/doap#>
PREFIX ggen: <http://ggen.dev/ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?version ?description ?edition ?license
WHERE {
  ?project a ggen:RustProject ;
           doap:name ?name ;
           doap:version ?version ;
           doap:description ?description ;
           ggen:edition ?edition ;
           doap:license ?license .
}
"}}
{{!-- Generate Cargo.toml --}}
{{file "Cargo.toml"}}
[package]
name = "{{name}}"
version = "{{version}}"
edition = "{{edition}}"
license = "{{license}}"
description = "{{description}}"

[dependencies]
{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?depName ?depVersion ?features
WHERE {
  ?project ggen:dependency ?dep .
  ?dep ggen:name ?depName ;
       ggen:version ?depVersion .
  OPTIONAL { ?dep ggen:features ?features }
}
"}}
{{depName}} = { version = "{{depVersion}}"{{#if features}}, features = ["{{features}}"]{{/if}} }
{{/sparql}}

[dev-dependencies]
{{/file}}

{{!-- Generate src/lib.rs --}}
{{file "src/lib.rs"}}
//! {{description}}
//!
//! This library was generated using ggen from RDF/TTL definitions.

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?moduleName ?moduleDesc
WHERE {
  ?project ggen:module ?module .
  ?module ggen:name ?moduleName ;
          ggen:description ?moduleDesc .
  FILTER(?moduleName = 'error')
}
"}}
/// {{moduleDesc}}
pub mod {{moduleName}} {
    use thiserror::Error;

    #[derive(Error, Debug)]
    pub enum CalcError {
{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?errorName ?errorMsg
WHERE {
  ?project ggen:module ?module .
  ?module ggen:name 'error' ;
          ggen:hasError ?error .
  ?error ggen:name ?errorName ;
         ggen:message ?errorMsg .
}
"}}
        #[error(\"{{errorMsg}}\")]
        {{errorName}},
{{/sparql}}
    }
}

{{/sparql}}

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?moduleName ?moduleDesc
WHERE {
  ?project ggen:module ?module .
  ?module ggen:name ?moduleName ;
          ggen:description ?moduleDesc .
  FILTER(?moduleName = 'operations')
}
"}}
/// {{moduleDesc}}
pub mod {{moduleName}} {
    use crate::error::CalcError;

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?funcName ?funcSig
WHERE {
  ?project ggen:module ?module .
  ?module ggen:name 'operations' ;
          ggen:functions ?funcList .
  ?funcList rdf:rest*/rdf:first ?func .
  ?func ggen:name ?funcName ;
        ggen:signature ?funcSig .
}
"}}
    /// {{funcName}} operation
    pub {{funcSig}} {
{{#if (eq funcName "add")}}
        a + b
{{/if}}
{{#if (eq funcName "subtract")}}
        a - b
{{/if}}
{{#if (eq funcName "multiply")}}
        a * b
{{/if}}
{{#if (eq funcName "divide")}}
        if b == 0.0 {
            Err(CalcError::DivideByZero)
        } else {
            Ok(a / b)
        }
{{/if}}
    }

{{/sparql}}
}

{{/sparql}}

#[cfg(test)]
mod tests {
    use super::operations::*;

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?testName ?assertion
WHERE {
  ?project ggen:testModule ?testMod .
  ?testMod ggen:testCase ?test .
  ?test ggen:name ?testName ;
        ggen:assertion ?assertion .
}
"}}
    #[test]
    fn {{testName}}() {
        assert!({{assertion}});
    }

{{/sparql}}
}
{{/file}}

{{!-- Generate README.md --}}
{{file "README.md"}}
# {{name}}

{{description}}

## Version

{{version}}

## Features

This library provides:

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?moduleName ?moduleDesc
WHERE {
  ?project ggen:module ?module .
  ?module ggen:name ?moduleName ;
          ggen:description ?moduleDesc .
}
"}}
- **{{moduleName}}**: {{moduleDesc}}
{{/sparql}}

## Usage

```rust
use {{name}}::operations::*;
use {{name}}::error::CalcError;

fn main() -> Result<(), CalcError> {
    let sum = add(10.0, 20.0);
    println!("Sum: {}", sum);

    let result = divide(10.0, 2.0)?;
    println!("Division: {}", result);

    Ok(())
}
```

## Dependencies

{{#sparql "
PREFIX ggen: <http://ggen.dev/ns#>

SELECT ?depName ?depVersion
WHERE {
  ?project ggen:dependency ?dep .
  ?dep ggen:name ?depName ;
       ggen:version ?depVersion .
}
"}}
- `{{depName}}` {{depVersion}}
{{/sparql}}

## License

{{license}}

---

*Generated by [ggen](https://github.com/ggen) from RDF/TTL definitions*
{{/file}}

{{/sparql}}
