pub struct Malformed<Knowledge, S = SimpleSpan> { /* private fields */ }Expand description
A zero-copy error type representing a malformed token, literal, or syntax construct.
This type tracks the position of a syntactically incorrect construct, along with optional knowledge about what kind of construct was attempted.
§Type Parameter
Knowledge: Provides context about what was being parsed (typically a knowledge marker type likeIntLiteral,StringLiteral, etc.). Must implementDisplayHumanfor error messages.
§Common Use Cases
- Malformed numbers:
123abc,0x(missing hex digits),12.34.56 - Malformed strings: Unterminated strings, invalid escape sequences
- Malformed identifiers: Mixed invalid characters
- Malformed operators: Incomplete multi-character operators
§Design
The knowledge field is optional, allowing this type to be used both with and
without specific context about what was being parsed. When knowledge is present,
error messages can include “did you mean X?” suggestions.
§Examples
§Basic Usage
use tokora::{error::Malformed, SimpleSpan};
// Malformed token at position 10-15
let error: Malformed<()> = Malformed::new(SimpleSpan::new(10, 15));
assert_eq!(error.span(), SimpleSpan::new(10, 15));
assert_eq!(error.to_string(), "malformed at 10..15");§With Knowledge Context
use tokora::{error::Malformed, SimpleSpan, utils::{knowledge::IntLiteral}};
// Found "123abc" when parsing integer
let error = Malformed::int(SimpleSpan::new(5, 11));
assert_eq!(error.to_string(), "malformed at 5..11, did you mean int literal?");Implementations§
Source§impl<S> Malformed<BooleanLiteral, S>
impl<S> Malformed<BooleanLiteral, S>
Sourcepub const fn boolean(span: S) -> Self
pub const fn boolean(span: S) -> Self
Create a new Malformed knowledge for a boolean literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::BooleanLiteral};
let error = Malformed::boolean(SimpleSpan::new(10, 14));
assert_eq!(error.span(), SimpleSpan::new(10, 14));
assert_eq!(error.knowledge(), Some(&BooleanLiteral::default()));Source§impl<S> Malformed<NullLiteral, S>
impl<S> Malformed<NullLiteral, S>
Sourcepub const fn null(span: S) -> Self
pub const fn null(span: S) -> Self
Create a new Malformed knowledge for a null literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::NullLiteral};
let error = Malformed::null(SimpleSpan::new(20, 24));
assert_eq!(error.span(), SimpleSpan::new(20, 24));
assert_eq!(error.knowledge(), Some(&NullLiteral::default()));Source§impl<S> Malformed<EnumLiteral, S>
impl<S> Malformed<EnumLiteral, S>
Sourcepub const fn enumeration(span: S) -> Self
pub const fn enumeration(span: S) -> Self
Create a new Malformed knowledge for an enum literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::EnumLiteral};
let error = Malformed::enumeration(SimpleSpan::new(30, 40));
assert_eq!(error.span(), SimpleSpan::new(30, 40));
assert_eq!(error.knowledge(), Some(&EnumLiteral::default()));Source§impl<S> Malformed<EnumValueLiteral, S>
impl<S> Malformed<EnumValueLiteral, S>
Sourcepub const fn enum_value(span: S) -> Self
pub const fn enum_value(span: S) -> Self
Create a new Malformed knowledge for an enum value literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::EnumValueLiteral};
let error = Malformed::enum_value(SimpleSpan::new(45, 49));
assert_eq!(error.span(), SimpleSpan::new(45, 49));
assert_eq!(error.knowledge(), Some(&EnumValueLiteral::default()));Source§impl<S> Malformed<DecimalLiteral, S>
impl<S> Malformed<DecimalLiteral, S>
Sourcepub const fn decimal(span: S) -> Self
pub const fn decimal(span: S) -> Self
Create a new Malformed knowledge for a decimal literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::DecimalLiteral};
let error = Malformed::decimal(SimpleSpan::new(150, 160));
assert_eq!(error.span(), SimpleSpan::new(150, 160));
assert_eq!(error.knowledge(), Some(&DecimalLiteral::default()));Source§impl<S> Malformed<OctalLiteral, S>
impl<S> Malformed<OctalLiteral, S>
Sourcepub const fn octal(span: S) -> Self
pub const fn octal(span: S) -> Self
Create a new Malformed knowledge for an octal literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::OctalLiteral};
let error = Malformed::octal(SimpleSpan::new(50, 60));
assert_eq!(error.span(), SimpleSpan::new(50, 60));
assert_eq!(error.knowledge(), Some(&OctalLiteral::default()));Source§impl<S> Malformed<StringLiteral, S>
impl<S> Malformed<StringLiteral, S>
Sourcepub const fn string(span: S) -> Self
pub const fn string(span: S) -> Self
Create a new Malformed knowledge for a string literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::StringLiteral};
let error = Malformed::string(SimpleSpan::new(70, 80));
assert_eq!(error.span(), SimpleSpan::new(70, 80));
assert_eq!(error.knowledge(), Some(&StringLiteral::default()));Source§impl<S> Malformed<HexLiteral, S>
impl<S> Malformed<HexLiteral, S>
Sourcepub const fn hex(span: S) -> Self
pub const fn hex(span: S) -> Self
Create a new Malformed knowledge for a hex literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::HexLiteral};
let error = Malformed::hex(SimpleSpan::new(90, 100));
assert_eq!(error.span(), SimpleSpan::new(90, 100));
assert_eq!(error.knowledge(), Some(&HexLiteral::default()));Source§impl<S> Malformed<IntLiteral, S>
impl<S> Malformed<IntLiteral, S>
Sourcepub const fn int(span: S) -> Self
pub const fn int(span: S) -> Self
Create a new Malformed knowledge for an int literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::IntLiteral};
let error = Malformed::int(SimpleSpan::new(105, 110));
assert_eq!(error.span(), SimpleSpan::new(105, 110));
assert_eq!(error.knowledge(), Some(&IntLiteral::default()));Source§impl<S> Malformed<BinaryLiteral, S>
impl<S> Malformed<BinaryLiteral, S>
Sourcepub const fn binary(span: S) -> Self
pub const fn binary(span: S) -> Self
Create a new Malformed knowledge for a binary literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::BinaryLiteral};
let error = Malformed::binary(SimpleSpan::new(115, 120));
assert_eq!(error.span(), SimpleSpan::new(115, 120));
assert_eq!(error.knowledge(), Some(&BinaryLiteral::default()));Source§impl<S> Malformed<FloatLiteral, S>
impl<S> Malformed<FloatLiteral, S>
Sourcepub const fn float(span: S) -> Self
pub const fn float(span: S) -> Self
Create a new Malformed knowledge for a float literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::FloatLiteral};
let error = Malformed::float(SimpleSpan::new(125, 130));
assert_eq!(error.span(), SimpleSpan::new(125, 130));
assert_eq!(error.knowledge(), Some(&FloatLiteral::default()));Source§impl<S> Malformed<HexFloatLiteral, S>
impl<S> Malformed<HexFloatLiteral, S>
Sourcepub const fn hex_float(span: S) -> Self
pub const fn hex_float(span: S) -> Self
Create a new Malformed knowledge for a hex float literal from a SimpleSpan
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::knowledge::HexFloatLiteral};
let error = Malformed::hex_float(SimpleSpan::new(135, 140));
assert_eq!(error.span(), SimpleSpan::new(135, 140));
assert_eq!(error.knowledge(), Some(&HexFloatLiteral::default()));Source§impl<Knowledge, S> Malformed<Knowledge, S>
impl<Knowledge, S> Malformed<Knowledge, S>
Sourcepub const fn new(span: S) -> Self
pub const fn new(span: S) -> Self
Creates a new malformed error without specific knowledge context.
§Examples
use tokora::{error::Malformed, SimpleSpan};
let error: Malformed<()> = Malformed::new(SimpleSpan::new(10, 15));
assert_eq!(error.span(), SimpleSpan::new(10, 15));
assert_eq!(error.knowledge(), None);Sourcepub const fn with_knowledge(span: S, knowledge: Knowledge) -> Self
pub const fn with_knowledge(span: S, knowledge: Knowledge) -> Self
Creates a new malformed error with knowledge about what was being parsed.
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::{knowledge::IntLiteral}};
let error = Malformed::with_knowledge(SimpleSpan::new(5, 10), IntLiteral::default());
assert_eq!(error.knowledge().is_some(), true);Sourcepub const fn span(&self) -> Swhere
S: Copy,
pub const fn span(&self) -> Swhere
S: Copy,
Returns the span of the malformed construct.
§Examples
use tokora::{error::Malformed, SimpleSpan};
let error: Malformed<()> = Malformed::new(SimpleSpan::new(20, 25));
assert_eq!(error.span(), SimpleSpan::new(20, 25));Sourcepub const fn span_mut(&mut self) -> &mut S
pub const fn span_mut(&mut self) -> &mut S
Returns a mutable reference to the span of the malformed construct.
Sourcepub const fn knowledge(&self) -> Option<&Knowledge>
pub const fn knowledge(&self) -> Option<&Knowledge>
Returns the knowledge about what was being parsed, if available.
§Examples
use tokora::{error::Malformed, SimpleSpan, utils::{knowledge::FloatLiteral}};
let error = Malformed::float(SimpleSpan::new(10, 15));
assert!(error.knowledge().is_some());Sourcepub fn into_components(self) -> (S, Option<Knowledge>)
pub fn into_components(self) -> (S, Option<Knowledge>)
Consumes the error and returns its components.
§Examples
use tokora::{error::Malformed, SimpleSpan};
let error: Malformed<()> = Malformed::new(SimpleSpan::new(15, 20));
let (span, knowledge) = error.into_components();
assert_eq!(span, SimpleSpan::new(15, 20));
assert_eq!(knowledge, None);Sourcepub fn bump(&mut self, offset: &S::Offset) -> &mut Selfwhere
S: Span,
pub fn bump(&mut self, offset: &S::Offset) -> &mut Selfwhere
S: Span,
Bumps the span by the given offset.
This adjusts both the start and end positions of the span, which is useful when adjusting error positions after processing or when combining errors from different parsing contexts.
§Examples
use tokora::{error::Malformed, SimpleSpan};
let mut error: Malformed<()> = Malformed::new(SimpleSpan::new(10, 15));
error.bump(&100);
assert_eq!(error.span(), SimpleSpan::new(110, 115));Trait Implementations§
impl<Knowledge: Copy, S: Copy> Copy for Malformed<Knowledge, S>
Source§impl<Knowledge, S> Display for Malformed<Knowledge, S>where
Knowledge: DisplayHuman,
S: Display,
impl<Knowledge, S> Display for Malformed<Knowledge, S>where
Knowledge: DisplayHuman,
S: Display,
impl<Knowledge: Eq, S: Eq> Eq for Malformed<Knowledge, S>
Source§impl<Knowledge, S> Error for Malformed<Knowledge, S>
impl<Knowledge, S> Error for Malformed<Knowledge, S>
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()