use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
pub struct HostCapsuleTypeConfig {
pub host_type: String,
#[serde(default)]
pub package: String,
#[serde(default)]
pub package_version: String,
#[serde(default)]
pub construct_expr: String,
}
impl HostCapsuleTypeConfig {
pub fn construct(&self, ptr_expr: &str, default_expr: &str) -> String {
let template = if self.construct_expr.is_empty() {
default_expr
} else {
self.construct_expr.as_str()
};
template.replace("{ptr}", ptr_expr)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construct_substitutes_ptr_placeholder() {
let cfg = HostCapsuleTypeConfig {
host_type: "*tree_sitter.Language".to_string(),
package: "github.com/tree-sitter/go-tree-sitter".to_string(),
package_version: "v0.25.0".to_string(),
construct_expr: "tree_sitter.NewLanguage(unsafe.Pointer({ptr}))".to_string(),
};
assert_eq!(
cfg.construct("ptr", "DEFAULT"),
"tree_sitter.NewLanguage(unsafe.Pointer(ptr))"
);
}
#[test]
fn construct_falls_back_to_default_when_empty() {
let cfg = HostCapsuleTypeConfig {
host_type: "*tree_sitter.Language".to_string(),
package: String::new(),
package_version: String::new(),
construct_expr: String::new(),
};
assert_eq!(
cfg.construct("ptr", "tree_sitter.NewLanguage(unsafe.Pointer({ptr}))"),
"tree_sitter.NewLanguage(unsafe.Pointer(ptr))"
);
}
}