Skip to main content

Code

Struct Code 

Source
pub struct Code { /* private fields */ }
Expand description

A diagnostic code such as E0001, T0042, or KRA0013: an uppercase prefix naming the registry that owns it, and a four-digit number within that registry.

A code is the stable identity of a kind of diagnostic. Messages can be reworded between releases; the code does not change, so tools key on it: an editor links it to documentation, a lint configuration silences it, a test asserts on it, and a filter separates lexical errors from parse errors by range without parsing message text. The renderer shows it in the header, rustc-style (error[E0001]: …), and the JSON output carries it as a string.

§Format

One to four ASCII uppercase letters (A–Z) followed by a number from 0 to 9999, always written with exactly four digits: Code::new("E", 1) displays as E0001. The fixed width means a code’s text and its value round-trip exactly (Code::parse accepts precisely what Display writes), and codes of one prefix sort the same as text and as numbers.

§Registries and ranges

The prefix names the registry; the number range within it names the phase. Codes in use across the -lang family:

PrefixOwnerRange
ERuntime errors: OPSE0001–E0099
ERuntime errors: LSBE0100–E0199
TType checking (typeck-lang)T0001–

A language front end takes its own prefix (Kraken uses KRA) and is recommended to split its numbers by phase, so that “is this a lexical error?” is a range check:

RangePhase
0001–0999Lexical: bad characters, unterminated literals, malformed numbers
1000–1999Parse: unexpected or missing tokens, unbalanced delimiters
2000–7999Semantic: name resolution, types, borrow and flow checks
8000–8999Lints and style warnings
9000–9999Internal and tool errors (9999: internal compiler error)

This is a convention, not an enforced rule: the crate accepts any well-formed code. See docs/API.md for the full guidance.

§Examples

use diag_lang::Code;

// A registry declares its codes as constants.
const UNTERMINATED_STRING: Code = Code::new("KRA", 1).unwrap();
const UNEXPECTED_TOKEN: Code = Code::new("KRA", 1000).unwrap();

assert_eq!(UNTERMINATED_STRING.to_string(), "KRA0001");
assert_eq!(UNEXPECTED_TOKEN.prefix(), "KRA");
assert_eq!(UNEXPECTED_TOKEN.number(), 1000);

// The phase is a range check on the number.
let is_lexical = |c: Code| c.prefix() == "KRA" && c.number() < 1000;
assert!(is_lexical(UNTERMINATED_STRING));
assert!(!is_lexical(UNEXPECTED_TOKEN));

// Text and value round-trip exactly.
assert_eq!(Code::parse("KRA0001"), Some(UNTERMINATED_STRING));

Implementations§

Source§

impl Code

Source

pub const fn new(prefix: &str, number: u16) -> Option<Self>

Builds the code prefix + number, or None if either is out of range.

prefix must be one to four ASCII uppercase letters and number at most 9999. The function is const, so a registry can declare its codes as constants with Code::new("E", 1).unwrap(), where a malformed code is a compile error rather than a runtime failure.

§Examples
use diag_lang::Code;

const ARITH_OVERFLOW: Code = Code::new("E", 1).unwrap();
assert_eq!(ARITH_OVERFLOW.to_string(), "E0001");

assert_eq!(Code::new("e", 1), None); // lowercase
assert_eq!(Code::new("", 1), None); // no prefix
assert_eq!(Code::new("ABCDE", 1), None); // prefix too long
assert_eq!(Code::new("E", 10_000), None); // more than four digits
Source

pub const fn parse(text: &str) -> Option<Self>

Parses a code written the way Display writes it: one to four ASCII uppercase letters followed by exactly four digits.

Returns None for anything else, including surrounding whitespace, a lowercase prefix, or a number with more or fewer than four digits, so parse(&code.to_string()) == Some(code) for every code and nothing else parses.

§Examples
use diag_lang::Code;

let code = Code::parse("T0042").unwrap();
assert_eq!((code.prefix(), code.number()), ("T", 42));

assert_eq!(Code::parse("T42"), None); // four digits, always
assert_eq!(Code::parse("T00042"), None);
assert_eq!(Code::parse("0042"), None); // a prefix is required
Source

pub fn prefix(&self) -> &str

Returns the prefix: the registry the code belongs to, such as "E" or "KRA".

§Examples
use diag_lang::Code;

assert_eq!(Code::new("KRA", 13).unwrap().prefix(), "KRA");
Source

pub const fn number(&self) -> u16

Returns the number within the prefix’s registry, 0–9999.

§Examples
use diag_lang::Code;

assert_eq!(Code::new("E", 102).unwrap().number(), 102);

Trait Implementations§

Source§

impl Clone for Code

Source§

fn clone(&self) -> Self

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 Copy for Code

Source§

impl Debug for Code

Source§

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

Writes Code(E0001): the code’s text is more useful than its storage.

Source§

impl Display for Code

Source§

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

Writes the prefix followed by the number as exactly four digits: E0001, KRA9999.

Source§

impl Eq for Code

Source§

impl Hash for Code

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 Ord for Code

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Code

Source§

fn eq(&self, other: &Self) -> bool

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

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Code

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Code

Auto Trait Implementations§

§

impl Freeze for Code

§

impl RefUnwindSafe for Code

§

impl Send for Code

§

impl Sync for Code

§

impl Unpin for Code

§

impl UnsafeUnpin for Code

§

impl UnwindSafe for Code

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<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> 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.