feat: add CodeParser for barcode classification per PRD §4

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-05-09 14:20:35 +08:00
parent 236cfae485
commit f030fb2476
2 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
// 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 ParseResult parse(String code) {
if (code.isEmpty) {
return ParseResult(type: CodeType.invalid, value: code);
}
if (_transitRegex.hasMatch(code)) {
return ParseResult(type: CodeType.locationTransit, value: code);
}
if (_zongpaiRegex.hasMatch(code)) {
return ParseResult(type: CodeType.zongpaiNo, value: code);
}
if (_locationRegex.hasMatch(code)) {
return ParseResult(type: CodeType.locationNormal, value: code);
}
return ParseResult(type: CodeType.invalid, value: code);
}
static bool isLocation(CodeType type) =>
type == CodeType.locationNormal || type == CodeType.locationTransit;
}
enum CodeType { zongpaiNo, locationNormal, locationTransit, invalid }
class ParseResult {
final CodeType type;
final String value;
ParseResult({required this.type, required this.value});
}