error
import std::encoding::error; · source
Types
DecodeError
type struct DecodeError with [Eq, Debug] {
position: i64;
kind: DecodeErrorKind;
}
The error every decoder in std::encoding returns: what was wrong with the input, and the byte offset in the input where the decoder found it. describe is the problem without the position, and Display adds it: invalid character at byte 2.
import std::encoding::error::{ DecodeErrorKind };
import std::encoding::percent::{ PercentEncoding };
import std::fmt;
match (PercentEncoding::decode(Str::new("a%2"))) {
Result::Ok(text) => { }
Result::Err(e) => {
if (e.kind == DecodeErrorKind::InvalidEscape) { fmt::eprintln(f"{e}"); } // invalid percent escape at byte 1
}
}
Properties
| Property | Type | Notes |
|---|---|---|
position | i64 | The byte offset in the input where decoding failed. |
kind | DecodeErrorKind | What was wrong there. |
Constructors
static new(kind: DecodeErrorKind, position: i64) -> DecodeError;
static new(kind: DecodeErrorKind, position: i64) -> DecodeError;An error of kind found at byte position of the input.
Methods
describe(&this) -> Str;
describe(&this) -> Str;What went wrong, without the position; Display adds it.
Trait implementations
implement trait Error for struct DecodeError
implement trait Display for struct DecodeError
DecodeErrorKind
type enum DecodeErrorKind with [Eq, Debug] {
InvalidCharacter;
InvalidLength;
InvalidPadding;
InvalidEscape;
InvalidUtf8;
}
Variants
| Variant | Notes |
|---|---|
InvalidCharacter | A byte that is not part of the encoding's alphabet. |
InvalidLength | An input whose length no complete encoding can have. |
InvalidPadding | Padding where the encoding allows none, or more of it than it allows. |
InvalidEscape | A % that two hexadecimal digits do not follow. |
InvalidUtf8 | Decoded bytes that are not well-formed UTF-8, where text was wanted. |
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 DecodeError with [Eq, Debug] {
position: i64;
kind: DecodeErrorKind;
}A decoding failure: its kind, and the byte offset in the input text at which the decoder found it.
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 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.
DecodeError.position: i64The byte offset in the input where decoding failed.
DecodeError.kind: DecodeErrorKindWhat was wrong there.
type enum DecodeErrorKind with [Eq, Debug] {
InvalidCharacter;
InvalidLength;
InvalidPadding;
InvalidEscape;
InvalidUtf8;
}What a decoder found wrong with its input.
std::encoding::errorimport 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::error;The error every decoder in std::encoding returns: what was wrong with the input, and the byte offset in the input where it was found.
namespace std::encoding::percent;Percent-encoding (RFC 3986): a byte a URI cannot carry as itself is written % and two hexadecimal digits.
type struct PercentEncoding {}The percent-encoding codec. Stateless; its operations are statics.
std::encoding::percentmatch (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
static PercentEncoding::decode(text: Str) -> Result<String, DecodeError>text with every %XX escape replaced by its byte. Refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).
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.
static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
Result::Ok(T)text: Stringpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
Result::Err(E)e: DecodeErrorif (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
e: DecodeErrortype 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 DecodeErrorKind with [Eq, Debug] {
InvalidCharacter;
InvalidLength;
InvalidPadding;
InvalidEscape;
InvalidUtf8;
}What a decoder found wrong with its input.
DecodeErrorKind::InvalidCharacterA byte that is not part of the encoding's alphabet.
DecodeErrorKind::InvalidLengthAn input whose length no complete encoding can have.
DecodeErrorKind::InvalidPaddingPadding where the encoding allows none, or more of it than it allows.
DecodeErrorKind::InvalidEscapeA % that two hexadecimal digits do not follow.
DecodeErrorKind::InvalidUtf8Decoded bytes that are not well-formed UTF-8, where text was wanted.
DecodeError::describe(&this) -> StrWhat went wrong, without the position; Display adds it.
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.
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 DecodeError::new(kind: DecodeErrorKind, position: i64) -> DecodeErrorAn error of kind found at byte position of the input.
(parameter) kind: DecodeErrorKind(parameter) position: i64type struct DecodeError with [Eq, Debug] {
position: i64;
kind: DecodeErrorKind;
}A decoding failure: its kind, and the byte offset in the input text at which the decoder found it.
std::encoding::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.
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 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.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referenceDecodeErrorKind::InvalidCharacterA byte that is not part of the encoding's alphabet.
std::encoding::errorDecodeErrorKind::InvalidLengthAn input whose length no complete encoding can have.
std::encoding::errorDecodeErrorKind::InvalidPaddingPadding where the encoding allows none, or more of it than it allows.
std::encoding::errorDecodeErrorKind::InvalidUtf8Decoded bytes that are not well-formed UTF-8, where text was wanted.
std::encoding::error