mecha10-templates 0.6.2

Mecha10 project templates — code generation, init, and template services
Documentation
//! Small helpers shared across template-generation modules.

use crate::utils::to_pascal_case;

/// Build the `(name, PascalName, PascalName)` argument triplet used by
/// several `format!`-based templates (Rust event types, Python model
/// classes) whose generated source references the raw name once and the
/// PascalCase name twice (e.g. once in a type/class declaration, once in a
/// constructor call).
pub(crate) fn pascal_triplet(name: &str) -> (String, String, String) {
    let pascal = to_pascal_case(name);
    (name.to_string(), pascal.clone(), pascal)
}

/// Build a timestamped `{Message,State}` type definition — the shape shared by
/// `message_type` and `state_type` in `templates::types`: a struct with a single
/// `timestamp: f64` field, a `new()` constructor that stamps the current time, and
/// a `Default` impl delegating to `new()`. `kind` is the capitalized label used in
/// the doc header (e.g. "Message", "State"); its lowercase form is used in the
/// generated `// TODO` comment.
pub(crate) fn timestamped_type_template(name: &str, kind: &str) -> String {
    let pascal = to_pascal_case(name);
    let kind_lower = kind.to_lowercase();
    format!(
        r#"//! {name} {kind} Type
//!
//! Auto-generated {kind_lower} definition.

use serde::{{Deserialize, Serialize}};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct {pascal} {{
    pub timestamp: f64,
    // TODO: Add {kind_lower} fields
}}

impl {pascal} {{
    pub fn new() -> Self {{
        Self {{
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs_f64(),
        }}
    }}
}}

impl Default for {pascal} {{
    fn default() -> Self {{
        Self::new()
    }}
}}
"#,
    )
}