identity_kit/did/error.rs
1/// Why a string is not a valid DID, DID URL or DID method.
2///
3/// Positions are byte offsets into the input that was parsed.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5#[non_exhaustive]
6pub enum DidError {
7 /// The input does not start with the lowercase `did:` scheme.
8 #[error("missing `did:` scheme prefix")]
9 MissingScheme,
10 /// The method name is empty or contains a character other than `a-z` and `0-9`.
11 #[error("invalid method name: unexpected character at byte {position}")]
12 InvalidMethodName {
13 /// Byte offset of the offending character.
14 position: usize,
15 },
16 /// The method-specific id is missing, empty, or ends with `:`.
17 #[error("method-specific id is empty or ends with `:`")]
18 EmptyMethodSpecificId,
19 /// The namespace of a [`DidMethod`](super::DidMethod) is empty, has a second segment, or
20 /// contains a character that is not an `idchar`.
21 #[error("invalid method namespace: unexpected character or end of input at byte {position}")]
22 InvalidNamespace {
23 /// Byte offset of the offending character, or of the end of the input.
24 position: usize,
25 },
26 /// A character that is not allowed at this position of a DID.
27 #[error("invalid character {ch:?} at byte {position}")]
28 InvalidChar {
29 /// Byte offset of the offending character.
30 position: usize,
31 /// The offending character.
32 ch: char,
33 },
34 /// A `%` that is not followed by exactly two hexadecimal digits.
35 #[error("incomplete or non-hex percent-encoding at byte {position}")]
36 InvalidPercentEncoding {
37 /// Byte offset of the `%`.
38 position: usize,
39 },
40 /// The input is longer than the configured maximum.
41 #[error("input exceeds the maximum length of {max} bytes")]
42 TooLong {
43 /// The maximum number of bytes accepted.
44 max: usize,
45 },
46 /// The DID URL path is not a valid RFC 3986 `path-abempty`.
47 #[error("invalid DID URL path")]
48 InvalidPath,
49 /// The DID URL query contains a character not allowed by RFC 3986.
50 #[error("invalid DID URL query")]
51 InvalidQuery,
52 /// The DID URL fragment contains a character not allowed by RFC 3986.
53 #[error("invalid DID URL fragment")]
54 InvalidFragment,
55 /// The key of a `did:jwk` holds a JSON number with no finite double value, such as
56 /// `1e400`, which the JSON Canonicalization Scheme cannot encode. `serde_json` only keeps
57 /// such a number when its `arbitrary_precision` feature is on.
58 #[error("the JSON number {number} has no finite double value, so it cannot be canonicalized")]
59 UnsupportedNumber {
60 /// The number as written.
61 number: String,
62 },
63}