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.
45use crate::parse::{MAX_PACKAGE_PATH_SEGMENT_BYTES, MAX_RESOURCE_PATH_SEGMENT_BYTES};
6use thiserror::Error;
78#[derive(PartialEq, Debug, Error)]
9pub enum ParseError {
10#[error("missing scheme")]
11MissingScheme,
1213#[error("invalid scheme")]
14InvalidScheme,
1516#[error("cannot have a scheme")]
17CannotContainScheme,
1819#[error("invalid host")]
20InvalidHost,
2122#[error("empty host")]
23EmptyHost,
2425#[error("missing host")]
26MissingHost,
2728#[error("host must be empty to imply absolute path")]
29HostMustBeEmpty,
3031#[error("invalid path segment")]
32InvalidPathSegment(#[source] PackagePathSegmentError),
3334#[error("invalid name")]
35InvalidName(#[source] PackagePathSegmentError),
3637#[error("URL path must start with '/'")]
38PathMustHaveLeadingSlash,
3940#[error("missing name")]
41MissingName,
4243#[error("invalid variant")]
44InvalidVariant(#[source] PackagePathSegmentError),
4546#[error("missing hash")]
47MissingHash,
4849#[error("missing resource")]
50MissingResource,
5152#[error("invalid hash")]
53InvalidHash(#[source] fuchsia_hash::ParseHashError),
5455#[error("uppercase hex characters in hash")]
56UpperCaseHash,
5758#[error("multiple hash query parameters")]
59MultipleHashes,
6061#[error("cannot contain hash")]
62CannotContainHash,
6364#[error("path must be root")]
65PathMustBeRoot,
6667#[error("resource path failed to percent decode")]
68ResourcePathPercentDecode(#[source] std::str::Utf8Error),
6970#[error("invalid resource path")]
71InvalidResourcePath(#[source] ResourcePathError),
7273#[error("cannot contain a resource path (a URL fragment)")]
74CannotContainResource,
7576#[error("extra path segments")]
77ExtraPathSegments,
7879#[error("extra query parameters")]
80ExtraQueryParameters,
8182#[error("cannot contain port")]
83CannotContainPort,
8485#[error("cannot contain username")]
86CannotContainUsername,
8788#[error("cannot contain password")]
89CannotContainPassword,
9091#[error("cannot contain query parameters")]
92CannotContainQueryParameters,
9394#[error("relative path URL cannot specify a package hash")]
95RelativePathCannotSpecifyHash,
9697#[error("relative path URL cannot specify a variant")]
98RelativePathCannotSpecifyVariant,
99100#[error("relative URL could not be parsed into a relative package path, Some({0:?}) != {1:?}")]
101InvalidRelativePath(String, Option<String>),
102103#[error(
104"relative URL with absolute path is not supported (relative path cannot start with `/`)"
105)]
106AbsolutePathNotSupported,
107108#[error("invalid repository URI")]
109InvalidRepository,
110111#[error("url parse error")]
112UrlParseError(#[from] url::ParseError),
113}
114115#[derive(PartialEq, Eq, Debug, Error)]
116pub enum PackagePathSegmentError {
117#[error("empty segment")]
118Empty,
119120#[error(
121"segment too long. should be at most {MAX_PACKAGE_PATH_SEGMENT_BYTES} bytes, was {0} bytes"
122)]
123TooLong(usize),
124125#[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:?}")]
126InvalidCharacter { character: char },
127128#[error("package path segments cannot be a single period")]
129DotSegment,
130131#[error("package path segments cannot be two periods")]
132DotDotSegment,
133}
134135#[derive(Clone, Debug, PartialEq, Eq, Error)]
136pub enum ResourcePathError {
137#[error("object names must be at least 1 byte")]
138NameEmpty,
139140#[error("object names must be at most {} bytes", MAX_RESOURCE_PATH_SEGMENT_BYTES)]
141NameTooLong,
142143#[error("object names cannot contain the NULL byte")]
144NameContainsNull,
145146#[error("object names cannot be '.'")]
147NameIsDot,
148149#[error("object names cannot be '..'")]
150NameIsDotDot,
151152#[error("object paths cannot start with '/'")]
153PathStartsWithSlash,
154155#[error("object paths cannot end with '/'")]
156PathEndsWithSlash,
157158#[error("object paths must be at least 1 byte")]
159PathIsEmpty,
160161// 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'")]
163NameContainsNewline,
164}