pub struct BibParseError {
kind: BibParseErrorKind,
offset: usize,
}Expand description
A recoverable BibTeX parse error: a BibParseErrorKind plus the byte
offset into the original input where the problem was detected.
The offset is a byte index, matching the convention mos-core
SourceSpans use, so a future citation slice can turn one of these into a
compiler Diagnostic without re-deriving positions. Use
line_col for a 1-based line/column pair.
§Examples
use mos_bib::{BibParseErrorKind, parse_bibtex};
let err = parse_bibtex("article{x}").unwrap_err();
assert_eq!(err.kind(), BibParseErrorKind::ExpectedAt);
assert_eq!(err.offset(), 0);Fields§
§kind: BibParseErrorKind§offset: usizeImplementations§
Source§impl BibParseError
impl BibParseError
Sourcepub(crate) const fn new(kind: BibParseErrorKind, offset: usize) -> Self
pub(crate) const fn new(kind: BibParseErrorKind, offset: usize) -> Self
Construct an error of kind at byte offset. Crate-internal: the
parser is the only place that mints these.
Sourcepub const fn kind(&self) -> BibParseErrorKind
pub const fn kind(&self) -> BibParseErrorKind
The kind of parse failure.
Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
The byte offset into the parsed input where the error was detected.
Sourcepub fn line_col(&self, src: &str) -> (usize, usize)
pub fn line_col(&self, src: &str) -> (usize, usize)
The 1-based (line, column) of this error within src.
src must be the input passed to parse_bibtex;
columns count Unicode scalar values. Use to_diagnostic
or From<BibParseError> for CoreError to bridge into mos-core
diagnostics.
§Examples
use mos_bib::parse_bibtex;
let src = "@article{ok}\n@bad";
let err = parse_bibtex(src).unwrap_err();
assert_eq!(err.line_col(src), (2, 5));Sourcepub fn to_diagnostic(self, file: impl Into<PathBuf>) -> Diagnostic
pub fn to_diagnostic(self, file: impl Into<PathBuf>) -> Diagnostic
Convert this error into a mos-core [Diagnostic] anchored in file.
The diagnostic carries the MOS0043 code and a zero-width
[SourceSpan] at offset, so a malformed .bib
reported here renders through the standard compiler pipeline. The
infallible From<BibParseError> for CoreError conversion is the
span-less equivalent for boundaries without a source path.
§Examples
use mos_bib::parse_bibtex;
let err = parse_bibtex("oops").unwrap_err();
let diagnostic = err.to_diagnostic("refs.bib");
assert_eq!(diagnostic.def().code().to_string(), "MOS0043");Trait Implementations§
Source§impl Clone for BibParseError
impl Clone for BibParseError
Source§fn clone(&self) -> BibParseError
fn clone(&self) -> BibParseError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for BibParseError
Source§impl Debug for BibParseError
impl Debug for BibParseError
Source§impl Display for BibParseError
impl Display for BibParseError
impl Eq for BibParseError
Source§impl Error for BibParseError
impl Error for BibParseError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<BibParseError> for CoreError
impl From<BibParseError> for CoreError
Source§fn from(err: BibParseError) -> Self
fn from(err: BibParseError) -> Self
Source§impl PartialEq for BibParseError
impl PartialEq for BibParseError
Source§fn eq(&self, other: &BibParseError) -> bool
fn eq(&self, other: &BibParseError) -> bool
self and other values to be equal, and is used by ==.