Skip to content

Commit 21d415c

Browse files
authored
Improved codegen templates loading (#36)
1 parent 83f60d5 commit 21d415c

File tree

11 files changed

+245
-218
lines changed

11 files changed

+245
-218
lines changed

cli/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "telchar"
33
description = "A toolchain that improves the developer experience of integrating Plutus validators in off-chain processes"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
edition = "2021"
66
repository = "https://github.com/txpipe/telchar"
77
homepage = "https://registry.telchar.txpipe.io"

cli/src/command/codegen_local.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::str::FromStr;
22

3-
use telchar_codegen::{template::Template, get_blueprint_from_path, get_template_from_blueprint};
3+
use telchar_codegen::Codegen;
4+
use telchar_codegen::template::Template;
45

56
pub async fn run(sub_matches: &clap::ArgMatches) {
7+
let codegen = Codegen::new();
68
let blueprint_reference = sub_matches.get_one::<String>("blueprint_path").expect("required");
79
let template_reference = sub_matches.get_one::<String>("template").expect("required");
8-
let blueprint = get_blueprint_from_path(blueprint_reference.to_string());
10+
let blueprint = codegen.get_blueprint_from_path(blueprint_reference.to_string());
911
let template = Template::from_str(&template_reference).unwrap();
10-
println!("{}", get_template_from_blueprint(blueprint, template));
12+
println!("{}", codegen.get_template_from_blueprint(blueprint, template));
1113
}

cli/src/command/publish.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use oci_client::{
77
Client, Reference,
88
};
99

10-
use telchar_codegen::get_blueprint_from_json;
10+
use telchar_codegen::Codegen;
1111
use telchar_codegen::blueprint::Blueprint;
1212

1313
use serde::{Deserialize, Serialize};
@@ -154,10 +154,11 @@ fn get_blueprint_file(path: String) -> (Blueprint, String) {
154154
let plutus_exists = fs::exists(&path_buf).unwrap_or(false);
155155

156156
if plutus_exists {
157+
let codegen = Codegen::new();
157158
println!("Found blueprint file at: {:?}", path_buf.display());
158159
println!("Processing...");
159160
let output_string = fs::read_to_string(path_buf).expect("Unable to read file");
160-
let output = get_blueprint_from_json(output_string.clone());
161+
let output = codegen.get_blueprint_from_json(output_string.clone());
161162
println!("Blueprint processed successfully!");
162163
return (output, output_string);
163164
}

codegen/src/lib.rs

Lines changed: 204 additions & 191 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ use std::str::FromStr;
44
use handlebars::{handlebars_helper, Handlebars};
55
use super::schema;
66

7-
mod blaze;
7+
static TEMPLATE_BLAZE: &'static str = include_str!(".././templates/blaze.hbs");
8+
static TEMPLATE_LUCID_EVOLUTION: &'static str = include_str!(".././templates/lucid-evolution.hbs");
9+
static TEMPLATE_MESH: &'static str = include_str!(".././templates/mesh.hbs");
810

911
pub enum Template {
1012
Blaze,
13+
LucidEvolution,
14+
Mesh,
1115
}
1216

1317
impl str::FromStr for Template {
1418
type Err = ();
1519
fn from_str(input: &str) -> Result<Template, Self::Err> {
1620
match input {
1721
"blaze" => Ok(Template::Blaze),
22+
"lucid-evolution" => Ok(Template::LucidEvolution),
23+
"mesh" => Ok(Template::Mesh),
1824
_ => Err(()),
1925
}
2026
}
@@ -24,26 +30,30 @@ impl fmt::Display for Template {
2430
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2531
match self {
2632
Template::Blaze => write!(f, "blaze"),
33+
Template::LucidEvolution => write!(f, "lucid-evolution"),
34+
Template::Mesh => write!(f, "mesh"),
2735
}
2836
}
2937
}
3038

31-
fn get_template_name(template: Template) -> String {
39+
pub fn get_template_name(template: Template) -> String {
3240
match template {
3341
Template::Blaze => "blaze".to_string(),
42+
Template::LucidEvolution => "lucid-evolution".to_string(),
43+
Template::Mesh => "mesh".to_string(),
3444
}
3545
}
3646

3747
fn helper_is_type(type_name_str: String, type_name: schema::TypeName) -> bool {
3848
schema::TypeName::from_str(&type_name_str).unwrap().eq(&type_name)
3949
}
4050

41-
pub fn get_template_from_schemas(schemas: Vec<schema::Schema>, template: Template) -> String {
42-
let template_name = get_template_name(template);
43-
51+
pub fn init_handlebars() -> Handlebars<'static> {
4452
let mut handlebars = Handlebars::new();
4553

46-
handlebars.register_template_string("blaze", blaze::get_template()).unwrap();
54+
handlebars.register_template_string("blaze", TEMPLATE_BLAZE).unwrap();
55+
handlebars.register_template_string("lucid-evolution", TEMPLATE_LUCID_EVOLUTION).unwrap();
56+
handlebars.register_template_string("mesh", TEMPLATE_MESH).unwrap();
4757

4858
handlebars_helper!(is_integer: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::Integer));
4959
handlebars.register_helper("is_integer", Box::new(is_integer));
@@ -69,5 +79,5 @@ pub fn get_template_from_schemas(schemas: Vec<schema::Schema>, template: Templat
6979
handlebars_helper!(is_list: |type_name: str| helper_is_type(type_name.into(), schema::TypeName::List));
7080
handlebars.register_helper("is_list", Box::new(is_list));
7181

72-
handlebars.render(&template_name, &schemas).unwrap()
73-
}
82+
handlebars
83+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
pub fn get_template() -> String {
3-
r#"
41
import { Data } from "@blaze-cardano/sdk";
52

63
{{#each this}}
@@ -49,6 +46,4 @@ const {{name}} = Data.Tuple([
4946
const {{name}} = Data.Array({{properties.[0].schema_name}});
5047
{{/if}}
5148

52-
{{/each}}
53-
"#.to_string()
54-
}
49+
{{/each}}

codegen/templates/lucid-evolution.hbs

Whitespace-only changes.

codegen/templates/mesh.hbs

Whitespace-only changes.

registry/backend/src/oci.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use oci_client::{client::ImageData, secrets::RegistryAuth, Client, Reference};
22
use serde::{Deserialize, Serialize};
33
use serde_json::{Number, Value};
4+
use telchar_codegen::Codegen;
45
use telchar_codegen::blueprint;
56

67
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -161,8 +162,9 @@ pub fn get_blueprint(image: &ImageData) -> Option<blueprint::Blueprint> {
161162
let blueprint = image.layers.iter().find(|l| l.media_type == "application/vnd.telchar.blueprint.v1+json");
162163

163164
if let Some(blueprint) = blueprint {
165+
let codegen = Codegen::new();
164166
let data = String::from_utf8_lossy(&blueprint.data).to_string();
165-
return Some(telchar_codegen::get_blueprint_from_json(data));
167+
return Some(codegen.get_blueprint_from_json(data));
166168
}
167169

168170
return None;

0 commit comments

Comments
 (0)