fuchsia_url/
errors.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::parse::{MAX_PACKAGE_PATH_SEGMENT_BYTES, MAX_RESOURCE_PATH_SEGMENT_BYTES};
6use thiserror::Error;
7
8#[derive(PartialEq, Debug, Error)]
9pub enum ParseError {
10    #[error("missing scheme")]
11    MissingScheme,
12
13    #[error("invalid scheme")]
14    InvalidScheme,
15
16    #[error("cannot have a scheme")]
17    CannotContainScheme,
18
19    #[error("invalid host")]
20    InvalidHost,
21
22    #[error("empty host")]
23    EmptyHost,
24
25    #[error("missing host")]
26    MissingHost,
27
28    #[error("host must be empty to imply absolute path")]
29    HostMustBeEmpty,
30
31    #[error("invalid path segment")]
32    InvalidPathSegment(#[source] PackagePathSegmentError),
33
34    #[error("invalid name")]
35    InvalidName(#[source] PackagePathSegmentError),
36
37    #[error("URL path must start with '/'")]
38    PathMustHaveLeadingSlash,
39
40    #[error("missing name")]
41    MissingName,
42
43    #[error("invalid variant")]
44    InvalidVariant(#[source] PackagePathSegmentError),
45
46    #[error("missing hash")]
47    MissingHash,
48
49    #[error("missing resource")]
50    MissingResource,
51
52    #[error("invalid hash")]
53    InvalidHash(#[source] fuchsia_hash::ParseHashError),
54
55    #[error("uppercase hex characters in hash")]
56    UpperCaseHash,
57
58    #[error("multiple hash query parameters")]
59    MultipleHashes,
60
61    #[error("cannot contain hash")]
62    CannotContainHash,
63
64    #[error("path must be root")]
65    PathMustBeRoot,
66
67    #[error("resource path failed to percent decode")]
68    ResourcePathPercentDecode(#[source] std::str::Utf8Error),
69
70    #[error("invalid resource path")]
71    InvalidResourcePath(#[source] ResourcePathError),
72
73    #[error("cannot contain a resource path (a URL fragment)")]
74    CannotContainResource,
75
76    #[error("extra path segments")]
77    ExtraPathSegments,
78
79    #[error("extra query parameters")]
80    ExtraQueryParameters,
81
82    #[error("cannot contain port")]
83    CannotContainPort,
84
85    #[error("cannot contain username")]
86    CannotContainUsername,
87
88    #[error("cannot contain password")]
89    CannotContainPassword,
90
91    #[error("cannot contain query parameters")]
92    CannotContainQueryParameters,
93
94    #[error("relative path URL cannot specify a package hash")]
95    RelativePathCannotSpecifyHash,
96
97    #[error("relative path URL cannot specify a variant")]
98    RelativePathCannotSpecifyVariant,
99
100    #[error("relative URL could not be parsed into a relative package path, Some({0:?}) != {1:?}")]
101    InvalidRelativePath(String, Option<String>),
102
103    #[error(
104        "relative URL with absolute path is not supported (relative path cannot start with `/`)"
105    )]
106    AbsolutePathNotSupported,
107
108    #[error("invalid repository URI")]
109    InvalidRepository,
110
111    #[error("url parse error")]
112    UrlParseError(#[from] url::ParseError),
113}
114
115#[derive(PartialEq, Eq, Debug, Error)]
116pub enum PackagePathSegmentError {
117    #[error("empty segment")]
118    Empty,
119
120    #[error(
121        "segment too long. should be at most {MAX_PACKAGE_PATH_SEGMENT_BYTES} bytes, was {0} bytes"
122    )]
123    TooLong(usize),
124
125    #[error("package path segments must consist of only digits (0 to 9), lower-case letters (a to z), hyphen (-), underscore (_), and period (.). this contained {character:?}")]
126    InvalidCharacter { character: char },
127
128    #[error("package path segments cannot be a single period")]
129    DotSegment,
130
131    #[error("package path segments cannot be two periods")]
132    DotDotSegment,
133}
134
135#[derive(Clone, Debug, PartialEq, Eq, Error)]
136pub enum ResourcePathError {
137    #[error("object names must be at least 1 byte")]
138    NameEmpty,
139
140    #[error("object names must be at most {} bytes", MAX_RESOURCE_PATH_SEGMENT_BYTES)]
141    NameTooLong,
142
143    #[error("object names cannot contain the NULL byte")]
144    NameContainsNull,
145
146    #[error("object names cannot be '.'")]
147    NameIsDot,
148
149    #[error("object names cannot be '..'")]
150    NameIsDotDot,
151
152    #[error("object paths cannot start with '/'")]
153    PathStartsWithSlash,
154
155    #[error("object paths cannot end with '/'")]
156    PathEndsWithSlash,
157
158    #[error("object paths must be at least 1 byte")]
159    PathIsEmpty,
160
161    // TODO(https://fxbug.dev/42096516) allow newline once meta/contents supports it in blob paths
162    #[error(r"object names cannot contain the newline character '\n'")]
163    NameContainsNewline,
164}