chessbaseLinux/crates/chess-core/src/diagnostic.rs
vrubelroman 34a4a6ad15
Some checks failed
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
Release / version (push) Has been cancelled
Release / linux (push) Has been cancelled
Release / flatpak (push) Has been cancelled
Release / windows (push) Has been cancelled
Release / publish (push) Has been cancelled
ChessPenguin 0.4.0: large ChessBase databases with a filter panel
Mega Database (11.7 million games) opens in under a second: a .cbh with
20,000 records or more is read in place, a game is converted to PGN when
opened. Filter panel on the right (player with suggestions, colour,
opponent, tournament, annotator, years, Elo rule, result, ECO, moves,
comments, variations), applied live in the background with cancellation.

Fixes: a ChessBase game unreadable from its first move (Chess960) crashed
the import; databases mixing UTF-8 and single-byte texts were decoded as
Cyrillic.

Docs: decision 12, design (docs/design/large-databases.md), format research
and acceptance criteria, QA report 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 01:02:36 +03:00

89 lines
2.9 KiB
Rust

//! Import diagnostics.
use std::fmt;
use crate::game::NodeId;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
/// Tolerated deviation, data fully preserved (e.g. FEN fullmove 0).
Info,
/// Data preserved but changed in form or placement (e.g. stray text kept as comment).
Warning,
/// Data could not be interpreted as chess (e.g. illegal move); raw text kept.
Error,
}
/// Machine-readable diagnostic kind; UI localizes by code, `message` gives details.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DiagnosticCode {
/// UTF-8 decoding produced replacement characters.
DecodeError,
MalformedTag,
DuplicateTag,
/// FEN with fullmove number 0, normalized to 1 (original tag kept).
FenFullmoveZero,
/// FEN tag without `SetUp "1"`; FEN used anyway.
FenWithoutSetUp,
/// `SetUp "1"` without FEN; standard start position used.
SetUpWithoutFen,
/// FEN repaired (invalid castling rights/en passant square dropped, etc.).
FenRepaired,
/// FEN cannot be used; movetext preserved as raw text.
InvalidFen,
UnsupportedVariant,
/// Illegal or ambiguous SAN, or `--` while in check; the rest of the
/// branch is kept as raw text.
IllegalMove,
/// Text outside comments that is not PGN; kept as a comment.
UnrecognizedText,
/// `}` outside a comment; dropped.
StrayClosingBrace,
UnterminatedComment,
UnmatchedCloseParen,
UnclosedVariation,
/// `(` before any move; content kept as raw text.
VariationWithoutMove,
EmptyVariation,
/// `%` escape line, ignored per PGN standard.
EscapeLine,
ResultInVariation,
ResultMismatch,
MissingResult,
/// Letters replaced by ASCII sequences in the source (`~~`, `><`, `o^`)
/// were restored in comments; counts in `message`.
LettersRestored,
/// Annotation could not be placed in its original position.
CommentMoved,
/// The game was converted from another format (ChessBase) and could only be
/// read in part; details in `message`.
PartlyConverted,
}
/// Position in the source. Offsets are bytes of the original file.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct SourceLocation {
pub byte_offset: usize,
/// 1-based line in the original file.
pub line: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
pub severity: Severity,
pub code: DiagnosticCode,
pub message: String,
pub location: Option<SourceLocation>,
/// Node the diagnostic is attached to (e.g. where recovered text was stored).
pub node: Option<NodeId>,
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} {:?}", self.severity, self.code)?;
if let Some(loc) = self.location {
write!(f, " at line {} (byte {})", loc.line, loc.byte_offset)?;
}
write!(f, ": {}", self.message)
}
}