From 577da30fc32e98c05edd5883099c6cdcedd7f34f Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Thu, 16 May 2024 14:39:17 -0700 Subject: [PATCH] add custom target for didc --- rust/candid_parser/src/bindings/rust.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/rust/candid_parser/src/bindings/rust.rs b/rust/candid_parser/src/bindings/rust.rs index d136b631..3e66a2f0 100644 --- a/rust/candid_parser/src/bindings/rust.rs +++ b/rust/candid_parser/src/bindings/rust.rs @@ -8,6 +8,7 @@ use candid::types::{Field, Function, Label, SharedLabel, Type, TypeEnv, TypeInne use convert_case::{Case, Casing}; use pretty::RcDoc; use serde::Serialize; +use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet}; #[derive(Default, Deserialize, Clone, Debug)] @@ -572,16 +573,23 @@ pub fn compile( external: ExternalConfig, ) -> String { let source = match external.0.get("target").map(|s| s.as_str()) { - Some("canister_call") | None => include_str!("rust_call.hbs"), - Some("agent") => include_str!("rust_agent.hbs"), - Some("stub") => include_str!("rust_stub.hbs"), + Some("canister_call") | None => Cow::Borrowed(include_str!("rust_call.hbs")), + Some("agent") => Cow::Borrowed(include_str!("rust_agent.hbs")), + Some("stub") => Cow::Borrowed(include_str!("rust_stub.hbs")), + Some("custom") => { + let template = external + .0 + .get("template") + .expect("template field expected for custom target"); + Cow::Owned(std::fs::read_to_string(template).unwrap()) + } _ => unimplemented!(), }; let (output, unused) = emit_bindgen(tree, env, actor); for e in unused { eprintln!("WARNING: path {e} is unused"); } - output_handlebar(output, external, source) + output_handlebar(output, external, &source) } pub enum TypePath {