0.6 rust重构01
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "core-domain"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,6 @@
|
||||
# core-domain
|
||||
|
||||
阶段 1 骨架 crate。
|
||||
|
||||
职责说明请先看:
|
||||
- ../../design/phase1-b-crate-responsibilities-v0.md
|
||||
@@ -0,0 +1,43 @@
|
||||
use crate::ids::{AssetId, AssetVersionId, BlockId, PageId, TaskId};
|
||||
use crate::time::Timestamp;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ActorRef {
|
||||
pub actor_type: String,
|
||||
pub actor_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum SourceKind {
|
||||
Web,
|
||||
Cli,
|
||||
Agent,
|
||||
Job,
|
||||
Import,
|
||||
System,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ChangeReason {
|
||||
pub code: String,
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RefLink {
|
||||
Page(PageId),
|
||||
Block(BlockId),
|
||||
Asset(AssetId),
|
||||
AssetVersion(AssetVersionId),
|
||||
Task(TaskId),
|
||||
External(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AuditInfo {
|
||||
pub created_at: Timestamp,
|
||||
pub updated_at: Timestamp,
|
||||
pub created_by: Option<ActorRef>,
|
||||
pub updated_by: Option<ActorRef>,
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
use crate::audit::{ActorRef, AuditInfo, ChangeReason, RefLink, SourceKind};
|
||||
use crate::ids::{
|
||||
AgentSessionId, AssetId, AssetVersionId, BlockId, CommandLogId, EventId, PageId, ReferenceId,
|
||||
TaskId, WorkspaceId,
|
||||
};
|
||||
use crate::time::Timestamp;
|
||||
use crate::types::{Locale, Revision};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Workspace {
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub slug: String,
|
||||
pub title: String,
|
||||
pub owner_actor_id: String,
|
||||
pub default_locale: Locale,
|
||||
pub storage_policy: String,
|
||||
pub sync_policy: String,
|
||||
pub feature_flags: Vec<String>,
|
||||
pub archived_at: Option<Timestamp>,
|
||||
pub audit: AuditInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PageType {
|
||||
Note,
|
||||
Doc,
|
||||
DatabaseRecord,
|
||||
Inbox,
|
||||
Template,
|
||||
System,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PageStatus {
|
||||
Active,
|
||||
Archived,
|
||||
Deleted,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Page {
|
||||
pub page_id: PageId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub parent_page_id: Option<PageId>,
|
||||
pub title: String,
|
||||
pub slug: String,
|
||||
pub icon: Option<String>,
|
||||
pub cover_asset_id: Option<AssetId>,
|
||||
pub body_root_block_id: Option<BlockId>,
|
||||
pub page_type: PageType,
|
||||
pub status: PageStatus,
|
||||
pub last_edited_at: Option<Timestamp>,
|
||||
pub last_edited_by: Option<ActorRef>,
|
||||
pub current_revision: Revision,
|
||||
pub audit: AuditInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum BlockType {
|
||||
Paragraph,
|
||||
Heading,
|
||||
BulletedListItem,
|
||||
NumberedListItem,
|
||||
Todo,
|
||||
Quote,
|
||||
CodeBlock,
|
||||
Divider,
|
||||
Callout,
|
||||
PageReference,
|
||||
BlockReference,
|
||||
EmbedAsset,
|
||||
EmbedView,
|
||||
EmbedOnlyoffice,
|
||||
EmbedMindmap,
|
||||
EmbedTable,
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Block {
|
||||
pub block_id: BlockId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub page_id: PageId,
|
||||
pub parent_block_id: Option<BlockId>,
|
||||
pub prev_block_id: Option<BlockId>,
|
||||
pub next_block_id: Option<BlockId>,
|
||||
pub sort_key: String,
|
||||
pub block_type: BlockType,
|
||||
pub content: String,
|
||||
pub props: Vec<(String, String)>,
|
||||
pub annotations: Vec<String>,
|
||||
pub status: String,
|
||||
pub revision: Revision,
|
||||
pub deleted_at: Option<Timestamp>,
|
||||
pub audit: AuditInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AssetKind {
|
||||
Image,
|
||||
Office,
|
||||
Pdf,
|
||||
Audio,
|
||||
Video,
|
||||
Export,
|
||||
OcrDerived,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AssetStatus {
|
||||
Active,
|
||||
Archived,
|
||||
Deleted,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Asset {
|
||||
pub asset_id: AssetId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub storage_key: String,
|
||||
pub original_name: String,
|
||||
pub mime_type: String,
|
||||
pub size_bytes: u64,
|
||||
pub checksum: Option<String>,
|
||||
pub origin: String,
|
||||
pub asset_kind: AssetKind,
|
||||
pub status: AssetStatus,
|
||||
pub latest_version_id: Option<AssetVersionId>,
|
||||
pub audit: AuditInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AssetVersion {
|
||||
pub asset_version_id: AssetVersionId,
|
||||
pub asset_id: AssetId,
|
||||
pub version_no: u32,
|
||||
pub storage_key: String,
|
||||
pub checksum: Option<String>,
|
||||
pub derived_from_version_id: Option<AssetVersionId>,
|
||||
pub change_reason: Option<ChangeReason>,
|
||||
pub created_at: Timestamp,
|
||||
pub created_by: Option<ActorRef>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ReferenceKind {
|
||||
PageToPage,
|
||||
BlockToBlock,
|
||||
BlockToPage,
|
||||
BlockToAssetFragment,
|
||||
TaskToObject,
|
||||
External,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Reference {
|
||||
pub reference_id: ReferenceId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub source_object_type: String,
|
||||
pub source_object_id: String,
|
||||
pub target_object_type: String,
|
||||
pub target_object_id: String,
|
||||
pub anchor: String,
|
||||
pub label: Option<String>,
|
||||
pub snippet: Option<String>,
|
||||
pub confidence: Option<String>,
|
||||
pub ref_kind: ReferenceKind,
|
||||
pub created_at: Timestamp,
|
||||
pub created_by: Option<ActorRef>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TaskType {
|
||||
Import,
|
||||
Ocr,
|
||||
Summary,
|
||||
Reindex,
|
||||
Sync,
|
||||
Transform,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TaskStatus {
|
||||
Pending,
|
||||
Running,
|
||||
Succeeded,
|
||||
Failed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TaskPriority {
|
||||
Low,
|
||||
Normal,
|
||||
High,
|
||||
Critical,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Task {
|
||||
pub task_id: TaskId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub title: String,
|
||||
pub description: Option<String>,
|
||||
pub task_type: TaskType,
|
||||
pub status: TaskStatus,
|
||||
pub priority: TaskPriority,
|
||||
pub assignee_actor_id: Option<String>,
|
||||
pub source_page_id: Option<PageId>,
|
||||
pub source_block_id: Option<BlockId>,
|
||||
pub input_payload: Option<String>,
|
||||
pub output_payload: Option<String>,
|
||||
pub due_at: Option<Timestamp>,
|
||||
pub completed_at: Option<Timestamp>,
|
||||
pub audit: AuditInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AgentSession {
|
||||
pub agent_session_id: AgentSessionId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub provider: String,
|
||||
pub model: String,
|
||||
pub initiator_actor_id: Option<String>,
|
||||
pub status: String,
|
||||
pub started_at: Timestamp,
|
||||
pub updated_at: Timestamp,
|
||||
pub ended_at: Option<Timestamp>,
|
||||
pub tool_policy: Option<String>,
|
||||
pub confirmation_policy: Option<String>,
|
||||
pub summary: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum CommandStatus {
|
||||
Pending,
|
||||
Succeeded,
|
||||
Failed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CommandLog {
|
||||
pub command_log_id: CommandLogId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub command_name: String,
|
||||
pub actor: ActorRef,
|
||||
pub source: SourceKind,
|
||||
pub target_objects: Vec<RefLink>,
|
||||
pub payload_summary: String,
|
||||
pub refs: Vec<RefLink>,
|
||||
pub idempotency_key: Option<String>,
|
||||
pub dry_run: bool,
|
||||
pub status: CommandStatus,
|
||||
pub created_at: Timestamp,
|
||||
pub finished_at: Option<Timestamp>,
|
||||
pub error_code: Option<String>,
|
||||
pub error_message: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EventType {
|
||||
PageCreated,
|
||||
PageUpdated,
|
||||
BlockInserted,
|
||||
BlockMoved,
|
||||
BlockUpdated,
|
||||
BlockDeleted,
|
||||
AssetVersionAdded,
|
||||
ReferenceCreated,
|
||||
TaskUpdated,
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DomainEvent {
|
||||
pub event_id: EventId,
|
||||
pub workspace_id: WorkspaceId,
|
||||
pub command_log_id: CommandLogId,
|
||||
pub aggregate_type: String,
|
||||
pub aggregate_id: String,
|
||||
pub event_type: EventType,
|
||||
pub before_revision: Option<Revision>,
|
||||
pub after_revision: Option<Revision>,
|
||||
pub payload_json: String,
|
||||
pub created_at: Timestamp,
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
macro_rules! define_id {
|
||||
($name:ident) => {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
pub struct $name(pub String);
|
||||
|
||||
impl $name {
|
||||
pub fn new(value: impl Into<String>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
define_id!(WorkspaceId);
|
||||
define_id!(PageId);
|
||||
define_id!(BlockId);
|
||||
define_id!(AssetId);
|
||||
define_id!(AssetVersionId);
|
||||
define_id!(ReferenceId);
|
||||
define_id!(TaskId);
|
||||
define_id!(AgentSessionId);
|
||||
define_id!(CommandLogId);
|
||||
define_id!(EventId);
|
||||
@@ -0,0 +1,34 @@
|
||||
pub mod audit;
|
||||
pub mod entities;
|
||||
pub mod ids;
|
||||
pub mod time;
|
||||
pub mod types;
|
||||
|
||||
pub use audit::{ActorRef, AuditInfo, ChangeReason, RefLink, SourceKind};
|
||||
pub use entities::{
|
||||
AgentSession, Asset, AssetKind, AssetStatus, AssetVersion, Block, BlockType, CommandLog,
|
||||
CommandStatus, DomainEvent, EventType, Page, PageStatus, PageType, Reference, ReferenceKind,
|
||||
Task, TaskPriority, TaskStatus, TaskType, Workspace,
|
||||
};
|
||||
pub use ids::{
|
||||
AgentSessionId, AssetId, AssetVersionId, BlockId, CommandLogId, EventId, PageId, ReferenceId,
|
||||
TaskId, WorkspaceId,
|
||||
};
|
||||
pub use time::Timestamp;
|
||||
pub use types::{Locale, Revision};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn revision_initial_is_one() {
|
||||
assert_eq!(Revision::initial().0, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workspace_id_keeps_original_value() {
|
||||
let workspace_id = WorkspaceId::new("ws_demo");
|
||||
assert_eq!(workspace_id.as_str(), "ws_demo");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Timestamp(pub String);
|
||||
|
||||
impl Timestamp {
|
||||
pub fn new(value: impl Into<String>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Revision(pub u64);
|
||||
|
||||
impl Revision {
|
||||
pub fn initial() -> Self {
|
||||
Self(1)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Locale(pub String);
|
||||
|
||||
impl Locale {
|
||||
pub fn new(value: impl Into<String>) -> Self {
|
||||
Self(value.into())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user