#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateKind {
RustModule,
DampenXml,
}
#[derive(Debug, Clone)]
pub struct WindowTemplate {
pub content: String,
pub kind: TemplateKind,
}
#[derive(Debug, Clone)]
pub struct WindowNameVariants {
pub snake: String,
pub pascal: String,
pub title: String,
}
impl WindowTemplate {
pub fn load(kind: TemplateKind) -> Self {
let content = match kind {
TemplateKind::RustModule => {
include_str!("../../../templates/add/window.rs.template")
}
TemplateKind::DampenXml => {
include_str!("../../../templates/add/window.dampen.template")
}
};
Self {
content: content.to_string(),
kind,
}
}
pub fn render(&self, window_name: &WindowNameVariants) -> String {
self.content
.replace("{{WINDOW_NAME}}", &window_name.snake)
.replace("{{WINDOW_NAME_PASCAL}}", &window_name.pascal)
.replace("{{WINDOW_NAME_TITLE}}", &window_name.title)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_template_load_rust_module() {
let template = WindowTemplate::load(TemplateKind::RustModule);
assert!(!template.content.is_empty());
assert_eq!(template.kind, TemplateKind::RustModule);
assert!(template.content.contains("{{WINDOW_NAME}}"));
assert!(template.content.contains("use dampen_core"));
assert!(template.content.contains("Model"));
}
#[test]
fn test_template_load_dampen_xml() {
let template = WindowTemplate::load(TemplateKind::DampenXml);
assert!(!template.content.is_empty());
assert_eq!(template.kind, TemplateKind::DampenXml);
assert!(template.content.contains("{{WINDOW_NAME_TITLE}}"));
}
#[test]
fn test_template_render_snake_case() {
let template = WindowTemplate {
content: "File: {{WINDOW_NAME}}.rs".to_string(),
kind: TemplateKind::RustModule,
};
let names = WindowNameVariants {
snake: "user_profile".to_string(),
pascal: "UserProfile".to_string(),
title: "User Profile".to_string(),
};
let rendered = template.render(&names);
assert_eq!(rendered, "File: user_profile.rs");
}
#[test]
fn test_template_render_pascal_case() {
let template = WindowTemplate {
content: "struct {{WINDOW_NAME_PASCAL}} {}".to_string(),
kind: TemplateKind::RustModule,
};
let names = WindowNameVariants {
snake: "user_profile".to_string(),
pascal: "UserProfile".to_string(),
title: "User Profile".to_string(),
};
let rendered = template.render(&names);
assert_eq!(rendered, "struct UserProfile {}");
}
#[test]
fn test_template_render_title_case() {
let template = WindowTemplate {
content: "<text>{{WINDOW_NAME_TITLE}}</text>".to_string(),
kind: TemplateKind::DampenXml,
};
let names = WindowNameVariants {
snake: "user_profile".to_string(),
pascal: "UserProfile".to_string(),
title: "User Profile".to_string(),
};
let rendered = template.render(&names);
assert_eq!(rendered, "<text>User Profile</text>");
}
#[test]
fn test_template_render_all_variants() {
let template = WindowTemplate {
content: "{{WINDOW_NAME}} {{WINDOW_NAME_PASCAL}} {{WINDOW_NAME_TITLE}}".to_string(),
kind: TemplateKind::RustModule,
};
let names = WindowNameVariants {
snake: "test_window".to_string(),
pascal: "TestWindow".to_string(),
title: "Test Window".to_string(),
};
let rendered = template.render(&names);
assert_eq!(rendered, "test_window TestWindow Test Window");
}
#[test]
fn test_template_render_multiple_occurrences() {
let template = WindowTemplate {
content: "{{WINDOW_NAME}} and {{WINDOW_NAME}} again".to_string(),
kind: TemplateKind::RustModule,
};
let names = WindowNameVariants {
snake: "settings".to_string(),
pascal: "Settings".to_string(),
title: "Settings".to_string(),
};
let rendered = template.render(&names);
assert_eq!(rendered, "settings and settings again");
}
}