image.domain\production_utils\production_utils.mbt
76.7% · 69 / 90 points · 441 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 生产工具 production_utils/production_utils - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// production_utils.mbt:生产级工具函数 - 7
// 提供统一的输入验证、错误处理、性能监控、内存安全等功能 - 8
- 9
///| - 10
/// 图像验证结果 - 11
#derive(Eq, Default, @debug.Debug) - 12
pub(all) struct ImageValidationResult { - 13
is_valid : Bool - 14
errors : Array[String] - 15
warnings : Array[String] - 16
width : Int32 - 17
height : Int32 - 18
channels : Int32 - 19
data_size : Int32 - 20
expected_data_size : Int32 - 21
} - 22
- 23
///| - 24
pub extend ImageValidationResult with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend ImageValidationResult with Eq::{not_equal, equal} - 28
- 29
///| - 30
pub extend ImageValidationResult with Default::{default} - 31
- 32
///| - 33
/// 验证图像有效性 - 34
pub fn validate_image(img : @types.Image) -> ImageValidationResult { - 35●
let errors : Array[String] = [] - 36
let warnings : Array[String] = [] - 37
- 38
// 验证尺寸 - 39
if img.width <= 0 { - 40●
errors.push("宽度必须大于0") - 41
} - 42
if img.height <= 0 { - 43○
errors.push("高度必须大于0") - 44
} - 45
if img.width > 65535 { - 46○
warnings.push("宽度超过65535,可能导致内存问题") - 47
} - 48
if img.height > 65535 { - 49○
warnings.push("高度超过65535,可能导致内存问题") - 50
} - 51
- 52
// 验证通道数 - 53
if img.channels < 1 || img.channels > 4 { - 54●
errors.push("通道数必须在1-4之间") - 55
} - 56
- 57
// 验证数据大小 - 58
let expected_size = img.width * img.height * img.channels - 59●
let actual_size = img.data.length() - 60
if actual_size != expected_size { - 61●
errors.push( - 62
"数据大小不匹配:期望 " + - 63●
expected_size.to_string() + - 64
",实际 " + - 65●
actual_size.to_string(), - 66
) - 67
} - 68
- 69
// 检查潜在的整数溢出 - 70
if img.width > 0 && img.height > 0 { - 71●
let pixel_count = img.width * img.height - 72
if pixel_count / img.width != img.height { - 73○
errors.push("像素计数溢出") - 74
} - 75
} - 76
- 77
ImageValidationResult::{ - 78●
is_valid: errors.length() == 0, - 79
errors, - 80
warnings, - 81
width: img.width, - 82
height: img.height, - 83
channels: img.channels, - 84
data_size: actual_size, - 85
expected_data_size: expected_size, - 86
} - 87
} - 88
- 89
///| - 90
/// 验证图像并在无效时抛出错误 - 91
pub fn validate_image_or_raise( - 92
img : @types.Image, - 93
) -> Unit raise @types.LoadError { - 94●
let result = validate_image(img) - 95
if !result.is_valid { - 96●
let mut error_msg = "图像验证失败:" - 97
for err in result.errors { - 98●
error_msg = error_msg + err + "; " - 99
} - 100
raise @types.LoadError::DecodeFailed(error_msg) - 101
} - 102
} - 103
- 104
///| - 105
/// 验证图像通道数 - 106
pub fn validate_channels( - 107
img : @types.Image, - 108
expected_channels : Array[Int32], - 109
) -> Bool { - 110●
for ch in expected_channels { - 111●
if img.channels == ch { - 112●
return true - 113
} - 114
} - 115
false - 116
} - 117
- 118
///| - 119
/// 验证图像通道数并在无效时抛出错误 - 120
pub fn validate_channels_or_raise( - 121
img : @types.Image, - 122
expected_channels : Array[Int32], - 123
function_name : String, - 124
) -> Unit raise @types.LoadError { - 125●
if !validate_channels(img, expected_channels) { - 126●
let mut expected_str = "" - 127●
for i in 0..<expected_channels.length() { - 128●
if i > 0 { - 129●
expected_str = expected_str + ", " - 130
} - 131●
expected_str = expected_str + expected_channels[i].to_string() - 132
} - 133
raise @types.LoadError::DecodeFailed( - 134
function_name + - 135
" 需要通道数为 [" + - 136
expected_str + - 137
"],实际为 " + - 138●
img.channels.to_string(), - 139
) - 140
} - 141
} - 142
- 143
///| - 144
/// 验证参数范围 - 145
pub fn validate_range( - 146
value : Double, - 147
min : Double, - 148
max : Double, - 149
param_name : String, - 150
) -> Result[Unit, String] { - 151●
if value < min || value > max { - 152●
Result::Err( - 153
param_name + - 154
" 必须在 [" + - 155●
min.to_string() + - 156
", " + - 157●
max.to_string() + - 158
"] 范围内,实际为 " + - 159●
value.to_string(), - 160
) - 161
} else { - 162●
Result::Ok(()) - 163
} - 164
} - 165
- 166
///| - 167
/// 验证参数范围并在无效时抛出错误 - 168
pub fn validate_range_or_raise( - 169
value : Double, - 170
min : Double, - 171
max : Double, - 172
param_name : String, - 173
) -> Unit raise @types.LoadError { - 174●
let result = validate_range(value, min, max, param_name) - 175●
match result { - 176●
Result::Err(msg) => raise @types.LoadError::DecodeFailed(msg) - 177●
Result::Ok(_) => () - 178
} - 179
} - 180
- 181
///| - 182
/// 验证整数参数范围 - 183
pub fn validate_int_range( - 184
value : Int32, - 185
min : Int32, - 186
max : Int32, - 187
param_name : String, - 188
) -> Result[Unit, String] { - 189●
if value < min || value > max { - 190●
Result::Err( - 191
param_name + - 192
" 必须在 [" + - 193●
min.to_string() + - 194
", " + - 195●
max.to_string() + - 196
"] 范围内,实际为 " + - 197●
value.to_string(), - 198
) - 199
} else { - 200●
Result::Ok(()) - 201
} - 202
} - 203
- 204
///| - 205
/// 验证整数参数范围并在无效时抛出错误 - 206
pub fn validate_int_range_or_raise( - 207
value : Int32, - 208
min : Int32, - 209
max : Int32, - 210
param_name : String, - 211
) -> Unit raise @types.LoadError { - 212●
let result = validate_int_range(value, min, max, param_name) - 213●
match result { - 214●
Result::Err(msg) => raise @types.LoadError::DecodeFailed(msg) - 215●
Result::Ok(_) => () - 216
} - 217
} - 218
- 219
///| - 220
pub let clamp_pixel_value : (Int32) -> Int32 = @safe_math.clamp_pixel_value - 221
- 222
///| - 223
pub fn clamp_pixel_value_double(value : Double) -> Double { - 224●
@safe_math.clamp_double(value, 0.0, 255.0) - 225
} - 226
- 227
///| - 228
pub let is_valid_index : (Int32, Int32) -> Bool = @safe_math.is_valid_index - 229
- 230
///| - 231
pub let is_valid_2d_index : (Int32, Int32, Int32, Int32) -> Bool = @safe_math.is_valid_2d_index - 232
- 233
///| - 234
/// 性能计时器 - 235
#derive(Eq, Default, @debug.Debug) - 236
pub(all) struct PerformanceTimer { - 237
start_time_ms : Double - 238
end_time_ms : Double - 239
elapsed_ms : Double - 240
operation_name : String - 241
} - 242
- 243
///| - 244
pub extend PerformanceTimer with @debug.Debug::{to_repr} - 245
- 246
///| - 247
pub extend PerformanceTimer with Eq::{not_equal, equal} - 248
- 249
///| - 250
pub extend PerformanceTimer with Default::{default} - 251
- 252
///| - 253
/// 创建性能计时器 - 254
pub fn create_performance_timer(operation_name : String) -> PerformanceTimer { - 255●
PerformanceTimer::{ - 256
start_time_ms: 0.0, - 257
end_time_ms: 0.0, - 258
elapsed_ms: 0.0, - 259
operation_name, - 260
} - 261
} - 262
- 263
///| - 264
/// 内存使用统计 - 265
#derive(Eq, Default, @debug.Debug) - 266
pub(all) struct MemoryUsageStats { - 267
allocated_bytes : Int32 - 268
freed_bytes : Int32 - 269
current_usage_bytes : Int32 - 270
peak_usage_bytes : Int32 - 271
allocation_count : Int32 - 272
free_count : Int32 - 273
} - 274
- 275
///| - 276
pub extend MemoryUsageStats with @debug.Debug::{to_repr} - 277
- 278
///| - 279
pub extend MemoryUsageStats with Eq::{not_equal, equal} - 280
- 281
///| - 282
pub extend MemoryUsageStats with Default::{default} - 283
- 284
///| - 285
/// 创建内存使用统计 - 286
pub fn create_memory_stats() -> MemoryUsageStats { - 287●
MemoryUsageStats::{ - 288
allocated_bytes: 0, - 289
freed_bytes: 0, - 290
current_usage_bytes: 0, - 291
peak_usage_bytes: 0, - 292
allocation_count: 0, - 293
free_count: 0, - 294
} - 295
} - 296
- 297
///| - 298
/// 错误码枚举 - 299
/// `#non_exhaustive` 表示未来可能添加新构造器 - 300
#derive(Eq) - 301
#non_exhaustive - 302
pub(all) enum ProductionErrorCode { - 303
InvalidInput - 304
OutOfMemory - 305
IndexOutOfBounds - 306
UnsupportedFormat - 307
OperationFailed - 308
Timeout - 309
InternalError - 310
Unknown - 311
} - 312
- 313
///| - 314
pub extend ProductionErrorCode with Eq::{not_equal, equal} - 315
- 316
///| - 317
/// 生产级错误 - 318
pub(all) struct ProductionError { - 319
code : ProductionErrorCode - 320
message : String - 321
function_name : String - 322
timestamp_ms : Double - 323
recoverable : Bool - 324
} - 325
- 326
///| - 327
/// 创建生产级错误 - 328
pub fn create_production_error( - 329
code : ProductionErrorCode, - 330
message : String, - 331
function_name : String, - 332
) -> ProductionError { - 333●
ProductionError::{ - 334
code, - 335
message, - 336
function_name, - 337
timestamp_ms: 0.0, - 338
recoverable: code != ProductionErrorCode::InternalError, - 339
} - 340
} - 341
- 342
///| - 343
/// 重试配置 - 344
#derive(Eq, Default, @debug.Debug) - 345
pub(all) struct RetryConfig { - 346
max_retries : Int32 - 347
initial_delay_ms : Double - 348
max_delay_ms : Double - 349
backoff_factor : Double - 350
retry_on_timeout : Bool - 351
retry_on_internal_error : Bool - 352
} - 353
- 354
///| - 355
pub extend RetryConfig with @debug.Debug::{to_repr} - 356
- 357
///| - 358
pub extend RetryConfig with Eq::{not_equal, equal} - 359
- 360
///| - 361
pub extend RetryConfig with Default::{default} - 362
- 363
///| - 364
/// 创建默认重试配置 - 365
pub fn create_default_retry_config() -> RetryConfig { - 366●
RetryConfig::{ - 367
max_retries: 3, - 368
initial_delay_ms: 100.0, - 369
max_delay_ms: 5000.0, - 370
backoff_factor: 2.0, - 371
retry_on_timeout: true, - 372
retry_on_internal_error: false, - 373
} - 374
} - 375
- 376
///| - 377
/// 计算重试延迟 - 378
pub fn calculate_retry_delay(config : RetryConfig, attempt : Int32) -> Double { - 379●
let mut factor = 1.0 - 380
for _ in 0..<attempt { - 381●
factor = factor * config.backoff_factor - 382
} - 383
let base_delay = config.initial_delay_ms * factor - 384
if base_delay > config.max_delay_ms { - 385○
config.max_delay_ms - 386
} else { - 387●
base_delay - 388
} - 389
} - 390
- 391
///| - 392
/// 验证结果转字符串 - 393
pub fn validation_result_to_string(result : ImageValidationResult) -> String { - 394●
let mut report = "## 图像验证报告\n\n" - 395
report = report + "| 指标 | 值 |\n|------|------|\n" - 396
report = report + - 397
"| 验证结果 | " + - 398◐
(if result.is_valid { "通过" } else { "失败" }) + - 399
" |\n" - 400●
report = report + "| 宽度 | " + result.width.to_string() + " |\n" - 401●
report = report + "| 高度 | " + result.height.to_string() + " |\n" - 402●
report = report + "| 通道数 | " + result.channels.to_string() + " |\n" - 403●
report = report + "| 数据大小 | " + result.data_size.to_string() + " |\n" - 404
report = report + - 405
"| 期望数据大小 | " + - 406●
result.expected_data_size.to_string() + - 407
" |\n" - 408
- 409●
if result.errors.length() > 0 { - 410○
report = report + "\n### 错误\n\n" - 411
for err in result.errors { - 412○
report = report + "- " + err + "\n" - 413
} - 414
} - 415
- 416●
if result.warnings.length() > 0 { - 417○
report = report + "\n### 警告\n\n" - 418
for warn in result.warnings { - 419○
report = report + "- " + warn + "\n" - 420
} - 421
} - 422
- 423
report - 424
} - 425
- 426
///| - 427
/// 生产级错误转字符串 - 428
pub fn production_error_to_string(err : ProductionError) -> String { - 429●
let code_str = match err.code { - 430●
ProductionErrorCode::InvalidInput => "无效输入" - 431○
ProductionErrorCode::OutOfMemory => "内存不足" - 432○
ProductionErrorCode::IndexOutOfBounds => "索引越界" - 433○
ProductionErrorCode::UnsupportedFormat => "不支持的格式" - 434○
ProductionErrorCode::OperationFailed => "操作失败" - 435○
ProductionErrorCode::Timeout => "超时" - 436○
ProductionErrorCode::InternalError => "内部错误" - 437○
_ => "未知错误" - 438
} - 439
- 440
"[" + code_str + "] " + err.function_name + ": " + err.message - 441
}