0.6 rust重构01
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "event-log"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
core-domain = { path = "../core-domain" }
|
||||
@@ -0,0 +1,8 @@
|
||||
# event-log
|
||||
|
||||
阶段 1 事件日志 crate。
|
||||
|
||||
职责说明请先看:
|
||||
- ../../design/phase1-e-command-log-v0.md
|
||||
- ../../design/phase1-e-domain-event-v0.md
|
||||
- ../../design/phase1-e-event-generation-rules-v0.md
|
||||
@@ -0,0 +1,29 @@
|
||||
use core_domain::Timestamp;
|
||||
use core_domain::{RefLink, WorkspaceId};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum CommandLogStatus {
|
||||
Pending,
|
||||
Succeeded,
|
||||
Failed,
|
||||
RolledBack,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CommandLogRecord {
|
||||
pub command_log_id: String,
|
||||
pub command_name: String,
|
||||
pub actor_type: String,
|
||||
pub actor_id: String,
|
||||
pub source: String,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub target_objects: Vec<RefLink>,
|
||||
pub payload_summary: String,
|
||||
pub refs_json: String,
|
||||
pub idempotency_key: Option<String>,
|
||||
pub status: CommandLogStatus,
|
||||
pub created_at: Timestamp,
|
||||
pub finished_at: Option<Timestamp>,
|
||||
pub trace_id: String,
|
||||
pub request_id: String,
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use core_domain::Timestamp;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EventStatus {
|
||||
Pending,
|
||||
Committed,
|
||||
Rejected,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DomainEventRecord {
|
||||
pub event_id: String,
|
||||
pub workspace_id: String,
|
||||
pub aggregate_type: String,
|
||||
pub aggregate_id: String,
|
||||
pub event_type: String,
|
||||
pub event_version: u32,
|
||||
pub payload_json: String,
|
||||
pub command_log_id: String,
|
||||
pub actor_type: String,
|
||||
pub created_at: Timestamp,
|
||||
pub status: EventStatus,
|
||||
pub trace_id: String,
|
||||
pub request_id: String,
|
||||
pub command_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EventGenerationOutcome {
|
||||
pub command_log_id: String,
|
||||
pub emitted_event_ids: Vec<String>,
|
||||
pub generated_for_replay: bool,
|
||||
pub generated_for_audit: bool,
|
||||
pub generated_for_index_catchup: bool,
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
pub mod command_log;
|
||||
pub mod domain_event;
|
||||
pub mod rules;
|
||||
|
||||
pub use command_log::{CommandLogRecord, CommandLogStatus};
|
||||
pub use domain_event::{DomainEventRecord, EventGenerationOutcome, EventStatus};
|
||||
pub use rules::{should_emit_domain_event, EventGenerationRule};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn failed_command_does_not_emit_success_event() {
|
||||
let should_emit = should_emit_domain_event(&EventGenerationRule {
|
||||
command_succeeded: false,
|
||||
changed_domain_state: false,
|
||||
rolled_back: false,
|
||||
});
|
||||
|
||||
assert!(!should_emit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EventGenerationRule {
|
||||
pub command_succeeded: bool,
|
||||
pub changed_domain_state: bool,
|
||||
pub rolled_back: bool,
|
||||
}
|
||||
|
||||
pub fn should_emit_domain_event(rule: &EventGenerationRule) -> bool {
|
||||
rule.command_succeeded && rule.changed_domain_state && !rule.rolled_back
|
||||
}
|
||||
Reference in New Issue
Block a user