sz-rust-cli 1.4.0

SZ-Rust 命令行工具:项目脚手架、数据库迁移、调度器管理
Documentation
// Generated by sz-rust-cli make:plugin --template {{ template_type }} --name {{ plugin_name }}
// Generation time: {{ generated_at }}
// Template version: {{ template_version }}
// DO NOT EDIT MANUALLY — regenerate with `sz-rust-cli make:plugin --force ...`

//! Workflow model for table `{{ table_name }}` with state machine

use sz_rust_core::model::Model;
use serde::{Deserialize, Serialize};

/// Workflow state enum
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum {{ class_name }}State {
    {%- for state in states %}
    {{ state.name | pascal_case }},
    {%- endfor %}
}

impl Default for {{ class_name }}State {
    fn default() -> Self {
        {{ class_name }}State::{{ initial_state | pascal_case }}
    }
}

impl std::fmt::Display for {{ class_name }}State {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            {%- for state in states %}
            {{ class_name }}State::{{ state.name | pascal_case }} => write!(f, "{{ state.name }}"),
            {%- endfor %}
        }
    }
}

/// Workflow model struct for `{{ table_name }}`
#[derive(Debug, Clone)]
pub struct {{ class_name }} {
    {%- for field in fields %}
    pub {{ field.name }}: {{ field.rust_type }},
    {%- endfor %}
    /// Current workflow state
    pub current_state: {{ class_name }}State,
}

impl Model for {{ class_name }} {
    type Id = {{ primary_key_type }};

    fn table_name() -> &'static str {
        "{{ table_name }}"
    }

    fn primary_key() -> &'static str {
        "{{ primary_key_name }}"
    }
}

/// State transition rules
pub fn can_transition(from: {{ class_name }}State, to: {{ class_name }}State) -> bool {
    matches!((from, to),
        {%- for transition in transitions %}
        ({{ class_name }}State::{{ transition.from | pascal_case }}, {{ class_name }}State::{{ transition.to | pascal_case }}) |
        {%- endfor %}
    )
}