error
import std::encoding::json::error; · source
Types
JsonError
type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}
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, and released with the error. |
Constructors
static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;
static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;An error of kind at the 1-indexed line and column, byte offset from the start of the source, explained by msg, which the error takes ownership of.
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 with [Eq, Clone] {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}
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) -> Str;
label(&this) -> Str;Short, stable diagnostic slug: "unexpected_eof", "invalid_number", etc. A view of static text: nothing to drop.
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
implement trait Error for struct JsonError
std::fmt::display
JsonError owns its message, which is released with the error. An error reading a parsed value into a type is a different one, ValueDecodeError; from_json refuses with a FromTextError holding whichever it was.
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;
}Why JSON text could not be parsed, and where: the failure's kind, its position in the source, and a message for a person to read.
JsonError.kind: JsonErrorKindtype enum JsonErrorKind with [Eq, Clone] {
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::encoding::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, and released with the error.
type struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
type 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 with [Eq, Clone] {
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.
type struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
type trait Eq<Rhs = This> {
equals(&this, other: &Rhs) -> boolean;
}Types that support value equality with a value of Rhs, which is the type itself unless an implementation names another: implement trait Eq for Str compares two Strs, and implement trait Eq<string> for Str a Str with a string.
type trait Clone {
clone(&this) -> This;
}A type whose values can be duplicated into independent copies. A type takes it with with [Clone] on its declaration, or by implementing clone by hand.
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).
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).
namespace std::encoding::json;json: RFC 8259 JSON text, read into and written from the shared data model std::encoding::value::Value.
JsonError.kind: JsonErrorKindJsonError.msg: StringHuman-readable detail. Owned, and released with the error.
std::encoding::json::errorJsonError::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 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) -> JsonErrorAn error of kind at the 1-indexed line and column, byte offset from the start of the source, explained by msg, which the error takes ownership of.
(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;
}Why JSON text could not be parsed, and where: the failure's kind, its position in the source, and a message for a person to read.
std::encoding::json::error&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 {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
type trait Debug {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
}How a value reads to a programmer: what {x:?} writes, and {x:#?} over several lines. with [Debug] on a declaration writes it field by field.
JsonErrorKind::label(&this) -> StrShort, stable diagnostic slug: "unexpected_eof", "invalid_number", etc. A view of static text: nothing to drop.
JsonErrorKind::UnexpectedCharEncountered a byte that does not begin a valid token.
std::encoding::json::errorJsonErrorKind::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::OutOfMemoryThe parser's working buffer allocation failed (only fires on a genuine malloc returning NULL, e.g. under memory pressure).
extern "C" function malloc(size: u64) -> mut u8*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 trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Error : Display, Debug {
source(&this) -> Option<virtual &Error>;
}An error: a message for a person through Display, a fuller form through Debug, and the error it was caused by, if it keeps one.