error
import std::json::error; · source
Types
JsonError
type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;
describe(&this) -> Str;
}
Modelled as a kind plus a position plus a message. Branch on kind for programmatic decisions; msg carries the human-readable detail.
line and column are 1-indexed, matching editor conventions; offset is a 0-indexed byte offset into the source. describe() is the standard accessor every stdlib error exposes.
Properties
| Property | Type | Notes |
|---|---|---|
kind | JsonErrorKind | |
line | u64 | 1-indexed line of the offending byte. |
column | u64 | 1-indexed column of the offending byte. |
offset | u64 | 0-indexed byte offset into the source. |
msg | String | Human-readable detail. Owned; call drop before discarding. |
Constructors
static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;
Methods
describe(&this) -> Str;
describe(&this) -> Str;A short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for JsonError delegate to it. Returns the human-readable msg; the kind slug and the line/column/offset position remain available as fields/methods. Borrows through this, so the view is valid for the error's lifetime.
JsonErrorKind
type enum JsonErrorKind {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}
implement enum JsonErrorKind {
label(&this) -> string;
}
The closed set of parser failures. label() gives a short diagnostic slug: "unexpected_eof", "invalid_number", and so on.
Variants
| Variant | Notes |
|---|---|
UnexpectedEof | Reached end of input mid-value. |
UnexpectedChar | Encountered a byte that does not begin a valid token. |
InvalidNumber | A number literal could not be parsed (overflow, malformed exponent, lone -, leading zeros, etc.). |
InvalidEscape | A \X escape inside a string is not one of the recognized escape sequences. |
InvalidUnicode | A \uXXXX sequence is malformed or names an unpaired surrogate. |
DepthExceeded | Object/array nesting exceeded parser::MAX_DEPTH. |
TrailingData | Garbage after the top-level value. |
KeyNotString | Object key was not a string. |
OutOfMemory | The parser's working buffer allocation failed (only fires on a genuine malloc returning NULL, e.g. under memory pressure). |
Methods
label(&this) -> string;
label(&this) -> string;Short slug for diagnostics. "unexpected_eof", "invalid_number", etc.
Trait implementations
implement trait Drop for struct JsonError
implement trait Display for struct JsonError
std::fmt::display
implement trait Debug for struct JsonError
std::fmt::display
JsonError owns its message, which is why it carries a drop.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}JsonError.kind: JsonErrorKindtype enum JsonErrorKind {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}Closed set of failure kinds the JSON parser produces. New kinds are rare; most failures fold into one of these.
std::json::errorJsonError.line: u641-indexed line of the offending byte.
JsonError.column: u641-indexed column of the offending byte.
JsonError.offset: u640-indexed byte offset into the source.
JsonError.msg: StringHuman-readable detail. Owned; call drop before discarding.
type struct String<A = GlobalAlloc> {
buffer: RawBuffer<u8, A>;
length: u64;
}String: owned, heap-allocated, length-typed UTF-8.
std::collections::stringstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static JsonError::new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError(parameter) kind: JsonErrorKind(parameter) line: u64(parameter) column: u64(parameter) offset: u64(parameter) msg: Stringtype struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}JsonError::describe(&this) -> StrA short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for JsonError delegate to it.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype enum Name {
Unit,
Payload(T),
Named { x: i32 },
}An algebraic data type. Variants may be bare or carry a tuple or struct payload, and a match on an enum must cover every variant. Name::Variant names a variant; methods are added with implement enum Name { ... }.
type enum JsonErrorKind {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}Closed set of failure kinds the JSON parser produces. New kinds are rare; most failures fold into one of these.
JsonErrorKind::UnexpectedEofReached end of input mid-value.
JsonErrorKind::UnexpectedCharEncountered a byte that does not begin a valid token.
JsonErrorKind::InvalidNumberA number literal could not be parsed (overflow, malformed exponent, lone -, leading zeros, etc.).
JsonErrorKind::InvalidEscapeA \X escape inside a string is not one of the recognized escape sequences.
JsonErrorKind::InvalidUnicodeA \uXXXX sequence is malformed or names an unpaired surrogate.
JsonErrorKind::DepthExceededObject/array nesting exceeded parser::MAX_DEPTH.
JsonErrorKind::TrailingDataGarbage after the top-level value.
JsonErrorKind::KeyNotStringObject key was not a string.
JsonErrorKind::OutOfMemoryThe parser's working buffer allocation failed (only fires on a genuine malloc returning NULL, e.g. under memory pressure).
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement enum Name { ... }Names the enum an inherent block extends - the only way to give an enum methods.
language referenceJsonErrorKind::label(&this) -> stringShort slug for diagnostics. "unexpected_eof", "invalid_number", etc.
primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
JsonError.kind: JsonErrorKindJsonError::describe(&this) -> StrA short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for JsonError delegate to it.
static JsonError::new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonErrortype trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}JsonErrorKind::label(&this) -> stringShort slug for diagnostics. "unexpected_eof", "invalid_number", etc.
JsonErrorKind::InvalidNumberA number literal could not be parsed (overflow, malformed exponent, lone -, leading zeros, etc.).
JsonErrorKind::InvalidEscapeA \X escape inside a string is not one of the recognized escape sequences.
JsonErrorKind::InvalidUnicodeA \uXXXX sequence is malformed or names an unpaired surrogate.
JsonErrorKind::OutOfMemoryThe parser's working buffer allocation failed (only fires on a genuine malloc returning NULL, e.g. under memory pressure).
intrinsic function malloc(size: u64) -> void*implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference