Files
pad_scanner/lib/services/code_parser.dart
2026-08-12 14:48:23 +08:00

58 lines
1.8 KiB
Dart

// lib/services/code_parser.dart
class CodeParser {
static final _zongpaiRegex = RegExp(r'^\d{2}(B|C|T)\d+$|^\d{2}(BW|CW)\d{4}$');
static final _locationRegex = RegExp(r'^[A-Z0-9]+-[A-Z0-9]+-[A-Z0-9]+$');
static final _transitRegex = RegExp(r'^TRANS-');
static final _attachmentRegex = RegExp(
r'^FJ-(\d{2}(B|C|T)\d+|\d{2}(BW|CW)\d{4})$',
);
static ParseResult parse(String code) {
final trimmed = code.trim();
if (trimmed.isEmpty) {
return ParseResult(type: CodeType.invalid, value: code);
}
final normalized = trimmed.toUpperCase();
if (_attachmentRegex.hasMatch(normalized)) {
final zongpai = normalized.substring(3); // strip "FJ-"
return ParseResult(type: CodeType.attachmentCode, value: zongpai);
}
if (_transitRegex.hasMatch(normalized)) {
return ParseResult(type: CodeType.locationTransit, value: normalized);
}
if (_zongpaiRegex.hasMatch(normalized)) {
return ParseResult(type: CodeType.zongpaiNo, value: normalized);
}
if (_locationRegex.hasMatch(normalized)) {
if (normalized.startsWith('B')) {
return ParseResult(
type: CodeType.locationTempStorage,
value: normalized,
);
}
return ParseResult(type: CodeType.locationNormal, value: normalized);
}
return ParseResult(type: CodeType.invalid, value: code);
}
static bool isLocation(CodeType type) =>
type == CodeType.locationNormal ||
type == CodeType.locationTransit ||
type == CodeType.locationTempStorage;
}
enum CodeType {
zongpaiNo,
attachmentCode,
locationNormal,
locationTransit,
locationTempStorage,
invalid,
}
class ParseResult {
final CodeType type;
final String value;
ParseResult({required this.type, required this.value});
}