use thiserror::Error;
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum PcgError {
#[error("[PCG-CONFIG-001] 配置错误: {message}")]
Config {
message: String,
field_path: Option<String>,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
},
#[error("[PCG-CONSTRAINT-001] 约束不满足: {message}")]
Constraint {
message: String,
constraint_type: Option<String>,
conflict_details: Option<String>,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_id: Option<String>,
},
#[error("[PCG-TOPOLOGY-001] 拓扑生成错误: {message}")]
Topology {
message: String,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_count: Option<usize>,
target_room_count: Option<usize>,
},
#[error("[PCG-LAYOUT-001] 布局求解错误: {message}")]
Layout {
message: String,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_id: Option<String>,
collision_details: Option<String>,
},
#[error("[PCG-TERRAIN-001] 地形生成错误: {message}")]
Terrain {
message: String,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_id: Option<String>,
strategy: Option<String>,
connectivity_failed: Option<bool>,
},
#[error("[PCG-SPAWN-001] 点位生成错误: {message}")]
Spawn {
message: String,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_id: Option<String>,
spawn_type: Option<String>,
candidate_count: Option<usize>,
target_count: Option<usize>,
},
#[error("[PCG-EXPORT-001] 导出错误: {message}")]
Export {
message: String,
format: Option<String>,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
source_error: Option<String>,
},
#[error("[PCG-BUDGET-001] 预算耗尽: {message}")]
BudgetExhausted {
message: String,
budget_type: String,
consumed: Option<u64>,
limit: Option<u64>,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
},
#[error("[PCG-ITERATION-001] 迭代次数超限: {message}")]
IterationLimit {
message: String,
current_iteration: u32,
max_iteration: u32,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
room_id: Option<String>,
},
#[error("[PCG-CORRUPTED-001] 数据损坏: {message}")]
CorruptedData {
message: String,
data_type: Option<String>,
expected_version: Option<String>,
actual_version: Option<String>,
stage: Option<String>,
seed: Option<u64>,
trace_id: Option<String>,
chunk_id: Option<String>,
},
#[error("[PCG-CAPABILITY-001] 能力不可用: {message}")]
CapabilityUnavailable {
message: String,
capability: String,
required_version: Option<String>,
current_version: Option<String>,
stage: Option<String>,
trace_id: Option<String>,
},
}
pub type PcgResult<T> = Result<T, Box<PcgError>>;
impl PcgError {
pub fn error_code(&self) -> &'static str {
match self {
PcgError::Config { .. } => "PCG-CONFIG-001",
PcgError::Constraint { .. } => "PCG-CONSTRAINT-001",
PcgError::Topology { .. } => "PCG-TOPOLOGY-001",
PcgError::Layout { .. } => "PCG-LAYOUT-001",
PcgError::Terrain { .. } => "PCG-TERRAIN-001",
PcgError::Spawn { .. } => "PCG-SPAWN-001",
PcgError::Export { .. } => "PCG-EXPORT-001",
PcgError::BudgetExhausted { .. } => "PCG-BUDGET-001",
PcgError::IterationLimit { .. } => "PCG-ITERATION-001",
PcgError::CorruptedData { .. } => "PCG-CORRUPTED-001",
PcgError::CapabilityUnavailable { .. } => "PCG-CAPABILITY-001",
}
}
pub fn stage(&self) -> Option<&str> {
match self {
PcgError::Config { stage, .. }
| PcgError::Constraint { stage, .. }
| PcgError::Topology { stage, .. }
| PcgError::Layout { stage, .. }
| PcgError::Terrain { stage, .. }
| PcgError::Spawn { stage, .. }
| PcgError::Export { stage, .. }
| PcgError::BudgetExhausted { stage, .. }
| PcgError::IterationLimit { stage, .. }
| PcgError::CorruptedData { stage, .. }
| PcgError::CapabilityUnavailable { stage, .. } => stage.as_deref(),
}
}
pub fn seed(&self) -> Option<u64> {
match self {
PcgError::Config { seed, .. }
| PcgError::Constraint { seed, .. }
| PcgError::Topology { seed, .. }
| PcgError::Layout { seed, .. }
| PcgError::Terrain { seed, .. }
| PcgError::Spawn { seed, .. }
| PcgError::Export { seed, .. }
| PcgError::BudgetExhausted { seed, .. }
| PcgError::IterationLimit { seed, .. }
| PcgError::CorruptedData { seed, .. } => *seed,
PcgError::CapabilityUnavailable { .. } => None,
}
}
pub fn trace_id(&self) -> Option<&str> {
match self {
PcgError::Config { trace_id, .. }
| PcgError::Constraint { trace_id, .. }
| PcgError::Topology { trace_id, .. }
| PcgError::Layout { trace_id, .. }
| PcgError::Terrain { trace_id, .. }
| PcgError::Spawn { trace_id, .. }
| PcgError::Export { trace_id, .. }
| PcgError::BudgetExhausted { trace_id, .. }
| PcgError::IterationLimit { trace_id, .. }
| PcgError::CorruptedData { trace_id, .. }
| PcgError::CapabilityUnavailable { trace_id, .. } => trace_id.as_deref(),
}
}
pub fn config(message: impl Into<String>) -> Box<Self> {
Box::new(Self::config_unboxed(message))
}
pub fn config_unboxed(message: impl Into<String>) -> Self {
PcgError::Config {
message: message.into(),
field_path: None,
stage: None,
seed: None,
trace_id: None,
}
}
pub fn config_with_field(
message: impl Into<String>,
field_path: impl Into<String>,
) -> Box<Self> {
Box::new(Self::config_with_field_unboxed(message, field_path))
}
pub fn config_with_field_unboxed(
message: impl Into<String>,
field_path: impl Into<String>,
) -> Self {
PcgError::Config {
message: message.into(),
field_path: Some(field_path.into()),
stage: None,
seed: None,
trace_id: None,
}
}
pub fn constraint(message: impl Into<String>) -> Box<Self> {
Box::new(Self::constraint_unboxed(message))
}
pub fn constraint_unboxed(message: impl Into<String>) -> Self {
PcgError::Constraint {
message: message.into(),
constraint_type: None,
conflict_details: None,
stage: None,
seed: None,
trace_id: None,
room_id: None,
}
}
pub fn topology(message: impl Into<String>) -> Box<Self> {
Box::new(Self::topology_unboxed(message))
}
pub fn topology_unboxed(message: impl Into<String>) -> Self {
PcgError::Topology {
message: message.into(),
stage: None,
seed: None,
trace_id: None,
room_count: None,
target_room_count: None,
}
}
pub fn layout(message: impl Into<String>) -> Box<Self> {
Box::new(Self::layout_unboxed(message))
}
pub fn layout_unboxed(message: impl Into<String>) -> Self {
PcgError::Layout {
message: message.into(),
stage: None,
seed: None,
trace_id: None,
room_id: None,
collision_details: None,
}
}
pub fn terrain(message: impl Into<String>) -> Box<Self> {
Box::new(Self::terrain_unboxed(message))
}
pub fn terrain_unboxed(message: impl Into<String>) -> Self {
PcgError::Terrain {
message: message.into(),
stage: None,
seed: None,
trace_id: None,
room_id: None,
strategy: None,
connectivity_failed: None,
}
}
pub fn spawn(message: impl Into<String>) -> Box<Self> {
Box::new(Self::spawn_unboxed(message))
}
pub fn spawn_unboxed(message: impl Into<String>) -> Self {
PcgError::Spawn {
message: message.into(),
stage: None,
seed: None,
trace_id: None,
room_id: None,
spawn_type: None,
candidate_count: None,
target_count: None,
}
}
pub fn export(message: impl Into<String>) -> Box<Self> {
Box::new(Self::export_unboxed(message))
}
pub fn export_unboxed(message: impl Into<String>) -> Self {
PcgError::Export {
message: message.into(),
format: None,
stage: None,
seed: None,
trace_id: None,
source_error: None,
}
}
pub fn export_with_format(
message: impl Into<String>,
format: impl Into<String>,
source_error: Option<String>,
) -> Box<Self> {
Box::new(PcgError::Export {
message: message.into(),
format: Some(format.into()),
stage: Some("export".to_string()),
seed: None,
trace_id: None,
source_error,
})
}
pub fn export_err(
message: impl Into<String>,
format: impl Into<String>,
source: impl std::error::Error,
) -> Box<Self> {
Box::new(Self::export_err_unboxed(message, format, source))
}
pub fn export_err_unboxed(
message: impl Into<String>,
format: impl Into<String>,
source: impl std::error::Error,
) -> Self {
PcgError::Export {
message: message.into(),
format: Some(format.into()),
stage: Some("export".to_string()),
seed: None,
trace_id: None,
source_error: Some(source.to_string()),
}
}
pub fn budget_exhausted(
message: impl Into<String>,
budget_type: impl Into<String>,
) -> Box<Self> {
Box::new(Self::budget_exhausted_unboxed(message, budget_type))
}
pub fn budget_exhausted_unboxed(
message: impl Into<String>,
budget_type: impl Into<String>,
) -> Self {
PcgError::BudgetExhausted {
message: message.into(),
budget_type: budget_type.into(),
consumed: None,
limit: None,
stage: None,
seed: None,
trace_id: None,
}
}
pub fn iteration_limit(message: impl Into<String>, current: u32, max: u32) -> Box<Self> {
Box::new(Self::iteration_limit_unboxed(message, current, max))
}
pub fn iteration_limit_unboxed(message: impl Into<String>, current: u32, max: u32) -> Self {
PcgError::IterationLimit {
message: message.into(),
current_iteration: current,
max_iteration: max,
stage: None,
seed: None,
trace_id: None,
room_id: None,
}
}
pub fn corrupted_data(message: impl Into<String>) -> Box<Self> {
Box::new(Self::corrupted_data_unboxed(message))
}
pub fn corrupted_data_unboxed(message: impl Into<String>) -> Self {
PcgError::CorruptedData {
message: message.into(),
data_type: None,
expected_version: None,
actual_version: None,
stage: None,
seed: None,
trace_id: None,
chunk_id: None,
}
}
pub fn corrupted_data_with_type(
message: impl Into<String>,
data_type: impl Into<String>,
) -> Box<Self> {
Box::new(PcgError::CorruptedData {
message: message.into(),
data_type: Some(data_type.into()),
expected_version: None,
actual_version: None,
stage: None,
seed: None,
trace_id: None,
chunk_id: None,
})
}
pub fn corrupted_data_with_version(
message: impl Into<String>,
data_type: impl Into<String>,
expected_version: &str,
actual_version: &str,
) -> Box<Self> {
Box::new(PcgError::CorruptedData {
message: message.into(),
data_type: Some(data_type.into()),
expected_version: Some(expected_version.to_string()),
actual_version: Some(actual_version.to_string()),
stage: None,
seed: None,
trace_id: None,
chunk_id: None,
})
}
pub fn capability_unavailable(
message: impl Into<String>,
capability: impl Into<String>,
) -> Box<Self> {
Box::new(Self::capability_unavailable_unboxed(message, capability))
}
pub fn capability_unavailable_unboxed(
message: impl Into<String>,
capability: impl Into<String>,
) -> Self {
PcgError::CapabilityUnavailable {
message: message.into(),
capability: capability.into(),
required_version: None,
current_version: None,
stage: None,
trace_id: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_code() {
let err = *PcgError::config("测试配置错误");
assert_eq!(err.error_code(), "PCG-CONFIG-001");
let err = *PcgError::constraint("测试约束错误");
assert_eq!(err.error_code(), "PCG-CONSTRAINT-001");
let err = *PcgError::topology("测试拓扑错误");
assert_eq!(err.error_code(), "PCG-TOPOLOGY-001");
}
#[test]
fn test_config_error_with_context() {
let err = PcgError::Config {
message: "房间数量超出范围".to_string(),
field_path: Some("room_count.max".to_string()),
stage: Some("config_validation".to_string()),
seed: Some(12345),
trace_id: Some("trace-001".to_string()),
};
assert_eq!(err.error_code(), "PCG-CONFIG-001");
assert_eq!(err.stage(), Some("config_validation"));
assert_eq!(err.seed(), Some(12345));
assert_eq!(err.trace_id(), Some("trace-001"));
}
#[test]
fn test_constraint_error_with_room_id() {
let err = PcgError::Constraint {
message: "锚点冲突".to_string(),
constraint_type: Some("anchor".to_string()),
conflict_details: Some("Boss 房间与商店房间锚点重叠".to_string()),
stage: Some("constraint_check".to_string()),
seed: Some(67890),
trace_id: Some("trace-002".to_string()),
room_id: Some("room-5".to_string()),
};
assert_eq!(err.error_code(), "PCG-CONSTRAINT-001");
assert_eq!(err.seed(), Some(67890));
}
#[test]
fn test_budget_exhausted_error() {
let err = *PcgError::budget_exhausted("时间预算耗尽", "time");
assert_eq!(err.error_code(), "PCG-BUDGET-001");
}
#[test]
fn test_iteration_limit_error() {
let err = *PcgError::iteration_limit("布局求解失败", 100, 100);
assert_eq!(err.error_code(), "PCG-ITERATION-001");
if let PcgError::IterationLimit {
current_iteration,
max_iteration,
..
} = err
{
assert_eq!(current_iteration, 100);
assert_eq!(max_iteration, 100);
} else {
panic!("错误类型不匹配");
}
}
#[test]
fn test_capability_unavailable_error() {
let err = *PcgError::capability_unavailable("运行时分块模式不可用", "runtime_chunked");
assert_eq!(err.error_code(), "PCG-CAPABILITY-001");
}
#[test]
fn test_error_display() {
let err = *PcgError::config("测试错误");
let display = format!("{}", err);
assert!(display.contains("PCG-CONFIG-001"));
assert!(display.contains("测试错误"));
}
#[test]
fn test_convenience_methods() {
let err = *PcgError::config_with_field("字段错误", "test.field");
if let PcgError::Config { field_path, .. } = err {
assert_eq!(field_path, Some("test.field".to_string()));
} else {
panic!("错误类型不匹配");
}
}
}