pub enum BaseInfo {
None,
NoRoot(Url),
Full(Url, String),
}Expand description
Information used for resolving relative URLs within a particular
input source. There should be a 1:1 correspondence between each
BaseInfo and its originating InputSource. The main entry
point for constructing is BaseInfo::from_source_url.
Once constructed, BaseInfo::parse_url_text can be used to
parse and resolve a (possibly relative) URL obtained from within
the associated InputSource.
A BaseInfo may be built from input sources which cannot resolve
relative links—for instance, stdin. It may also be built from input
sources which can resolve locally-relative links, but not root-relative
links.
Variants§
None
No base information is available. This is for sources with no base
information, such as [ResolvedInputSource::Stdin], and for URLs which
cannot be a base, such as data: and tel:. BaseInfo::None
can resolve no relative links; only fully-qualified links will be
parsed successfully.
NoRoot(Url)
A base which cannot resolve root-relative links. This is for
file: URLs where the root directory is not known. As such, you can
traverse relative to the current URL (by traversing the filesystem),
but you cannot jump to the “root”.
Full(Url, String)
A full base made up of origin and path. This can resolve
all kinds of relative links.
All non-file: URLs which can be a base fall into this case. For these,
origin and path are obtained by dividing the source URL into its
origin and path. When joined, ${origin}/${path} should be equivalent
to the source’s original URL.
This also represents file: URLs with a known root. The origin field
records the file: URL which will be used to resolve root-relative links.
The path field is the subpath to a particular input source within the
root. This is retained to resolve locally-relative links.
Implementations§
Source§impl BaseInfo
impl BaseInfo
Sourcepub const fn none() -> Self
pub const fn none() -> Self
Constructs BaseInfo::None.
Sourcepub const fn full(origin: Url, path: String) -> Self
pub const fn full(origin: Url, path: String) -> Self
Constructs BaseInfo::Full with the given fields.
Sourcepub fn from_source_url(url: &Url) -> Self
pub fn from_source_url(url: &Url) -> Self
Constructs a BaseInfo, with the variant being determined by the given URL.
- A
Url::cannot_be_a_baseURL will yieldBaseInfo::None. - A
file:URL will yieldBaseInfo::NoRoot. - For other URLs, a
BaseInfo::Fullwill be constructed from the URL’s origin and path.
Compared to BaseInfo::from_base_url, this function is more lenient in
what it accepts because this function should return a result for all
input source URLs.
Sourcefn split_url_origin_and_path(url: &Url) -> Option<(Url, String)>
fn split_url_origin_and_path(url: &Url) -> Option<(Url, String)>
Split URL into its origin and path, if possible. Will fail and return
None for URLs which cannot be a base.
Sourcepub fn from_path(path: &Path) -> Result<BaseInfo, ErrorKind>
pub fn from_path(path: &Path) -> Result<BaseInfo, ErrorKind>
Constructs a BaseInfo from the given filesystem path, requiring that
the given path be absolute. Assumes that the given path represents a directory.
This constructs a BaseInfo::Full where root-relative links will go to
the given path.
§Errors
Errors if the given path is not an absolute path.
Sourcepub fn use_fs_root_as_origin(&self) -> Cow<'_, Self>
pub fn use_fs_root_as_origin(&self) -> Cow<'_, Self>
If this is a BaseInfo::NoRoot, promote it to a BaseInfo::Full
by using the filesystem root as the “origin” for root-relative links.
Root-relative links will go to the filesystem root.
Generally, this function should be avoided in favour of a more explicit user-provided root directory. The filesystem root is rarely a good place to look for files.
Makes no change to other BaseInfo variants.
§Panics
If unable to split a BaseInfo::NoRoot into origin and path.
Sourcepub fn use_fs_path_as_origin(&self) -> Cow<'_, Self>
pub fn use_fs_path_as_origin(&self) -> Cow<'_, Self>
If this is a BaseInfo::NoRoot, promote it to a BaseInfo::Full
by using the entire filesystem path as the “origin” for root-relative links.
Root-relative links will go to the URL that was previously within NoRoot.
Generally, this function should be avoided in favour of a more explicit user-provided root directory.
Makes no change to other BaseInfo variants.
Sourcepub fn url(&self) -> Option<Url>
pub fn url(&self) -> Option<Url>
Returns the URL for the current BaseInfo, joining the origin and path
if needed.
Sourcepub fn to_file_path(&self) -> Option<PathBuf>
pub fn to_file_path(&self) -> Option<PathBuf>
Returns the filesystem path for the current BaseInfo if the underlying
URL is a file: URL.
Sourcepub const fn is_none(&self) -> bool
pub const fn is_none(&self) -> bool
Returns whether this value is BaseInfo::None.
Sourcepub const fn supports_root_relative(&self) -> bool
pub const fn supports_root_relative(&self) -> bool
Returns whether this BaseInfo variant supports resolving root-relative links.
If true, implies BaseInfo::supports_locally_relative.
Sourcepub const fn supports_locally_relative(&self) -> bool
pub const fn supports_locally_relative(&self) -> bool
Returns whether this BaseInfo variant supports resolving locally-relative links.
Sourcepub const fn or_fallback<'a>(&'a self, fallback: &'a Self) -> &'a Self
pub const fn or_fallback<'a>(&'a self, fallback: &'a Self) -> &'a Self
Returns the BaseInfo which has more information
between self and the given fallback.
BaseInfo::Full is preferred over BaseInfo::NoRoot
which is preferred over BaseInfo::None. If both self
and fallback are the same variant, then self will be preferred.
Sourcepub fn parse_url_text(&self, text: &str) -> Result<Url, ErrorKind>
pub fn parse_url_text(&self, text: &str) -> Result<Url, ErrorKind>
Parses the given URL text into a fully-qualified URL, including
resolving relative links if supported by the current BaseInfo.
To resolve relative links, this uses Url::join and ReqwestUrlExt::join_rooted
for BaseInfo::NoRoot and BaseInfo::Full, respectively.
§Errors
Returns an error if the text is an invalid URL, or if the text is a
relative link and this BaseInfo variant cannot resolve
the relative link.
Sourcepub fn parse_url_text_with_root_dir(
&self,
text: &str,
root_dir: Option<&Url>,
) -> Result<Url, ErrorKind>
pub fn parse_url_text_with_root_dir( &self, text: &str, root_dir: Option<&Url>, ) -> Result<Url, ErrorKind>
Parses the given URL text into a fully-qualified URL, including
resolving relative links if supported by the current BaseInfo
and applying the given root-dir if necessary.
The root-dir is applied if the current BaseInfo is BaseInfo::None
or has a file: URL and if the given text is a root-relative link.
In these cases, the given root_dir will take effect instead of the
original BaseInfo.
§Errors
Propagates errors from BaseInfo::parse_url_text.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for BaseInfo
impl<'de> Deserialize<'de> for BaseInfo
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl TryFrom<&str> for BaseInfo
impl TryFrom<&str> for BaseInfo
Source§fn try_from(value: &str) -> Result<Self, ErrorKind>
fn try_from(value: &str) -> Result<Self, ErrorKind>
Attempts to parse a base from the given string which may be
a URL or a filesystem path. In both cases, the string must
represent a valid base (i.e., not resulting in BaseInfo::None).
Otherwise, an error will be returned.
Note that this makes a distinction between filesystem paths as paths
and filesystem paths as URLs. When specified as a path, they will
become BaseInfo::Full but when specified as a URL, they will
become BaseInfo::NoRoot.
Additionally, the empty string is accepted and will be parsed to
BaseInfo::None.
impl Eq for BaseInfo
impl StructuralPartialEq for BaseInfo
Auto Trait Implementations§
impl Freeze for BaseInfo
impl RefUnwindSafe for BaseInfo
impl Send for BaseInfo
impl Sync for BaseInfo
impl Unpin for BaseInfo
impl UnwindSafe for BaseInfo
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> FromResponse for Twhere
T: DeserializeOwned,
impl<T> FromResponse for Twhere
T: DeserializeOwned,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more