Skip to main content

Malformed

Struct Malformed 

Source
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 like IntLiteral, StringLiteral, etc.). Must implement DisplayHuman for 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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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>

Source

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);
Source

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);
Source

pub const fn span(&self) -> S
where 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));
Source

pub const fn span_ref(&self) -> &S

Returns a reference to the span of the malformed construct.

Source

pub const fn span_mut(&mut self) -> &mut S

Returns a mutable reference to the span of the malformed construct.

Source

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());
Source

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);
Source

pub fn bump(&mut self, offset: &S::Offset) -> &mut Self
where 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§

Source§

impl<Knowledge: Clone, S: Clone> Clone for Malformed<Knowledge, S>

Source§

fn clone(&self) -> Malformed<Knowledge, S>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Knowledge: Copy, S: Copy> Copy for Malformed<Knowledge, S>

Source§

impl<Knowledge: Debug, S: Debug> Debug for Malformed<Knowledge, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Knowledge, S> Display for Malformed<Knowledge, S>
where Knowledge: DisplayHuman, S: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Knowledge: Eq, S: Eq> Eq for Malformed<Knowledge, S>

Source§

impl<Knowledge, S> Error for Malformed<Knowledge, S>
where Knowledge: DisplayHuman + Debug, S: Debug + Display,

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<Knowledge: Hash, S: Hash> Hash for Malformed<Knowledge, S>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Knowledge: PartialEq, S: PartialEq> PartialEq for Malformed<Knowledge, S>

Source§

fn eq(&self, other: &Malformed<Knowledge, S>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Knowledge: PartialEq, S: PartialEq> StructuralPartialEq for Malformed<Knowledge, S>

Auto Trait Implementations§

§

impl<Knowledge, S> Freeze for Malformed<Knowledge, S>
where S: Freeze, Option<Knowledge>: Freeze,

§

impl<Knowledge, S> RefUnwindSafe for Malformed<Knowledge, S>
where S: RefUnwindSafe, Option<Knowledge>: RefUnwindSafe,

§

impl<Knowledge, S> Send for Malformed<Knowledge, S>
where S: Send, Option<Knowledge>: Send,

§

impl<Knowledge, S> Sync for Malformed<Knowledge, S>
where S: Sync, Option<Knowledge>: Sync,

§

impl<Knowledge, S> Unpin for Malformed<Knowledge, S>
where S: Unpin, Option<Knowledge>: Unpin,

§

impl<Knowledge, S> UnsafeUnpin for Malformed<Knowledge, S>
where S: UnsafeUnpin, Option<Knowledge>: UnsafeUnpin,

§

impl<Knowledge, S> UnwindSafe for Malformed<Knowledge, S>
where S: UnwindSafe, Option<Knowledge>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.