-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revise: better handling for URL contents in inputs
- Loading branch information
1 parent
f39208c
commit 2aa95b8
Showing
2 changed files
with
46 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! Contents of an input. | ||
use url::Url; | ||
|
||
/// An error related to an input's [`Contents`]. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// An error parsing a [`Url`](url::Url). | ||
ParseUrl(url::ParseError), | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Error::ParseUrl(err) => write!(f, "parse url error: {err}"), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
/// A [`Result`](std::result::Result) with an [`Error`]. | ||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
/// The source of an input. | ||
#[derive(Clone, Debug)] | ||
pub enum Contents { | ||
/// Contents sourced from a URL. | ||
Url(Url), | ||
|
||
/// Contents provided as a string literal. | ||
Literal(String), | ||
} | ||
|
||
impl Contents { | ||
/// Attempts to create a URL contents from a string slice. | ||
pub fn url_from_str(url: impl AsRef<str>) -> Result<Self> { | ||
url.as_ref().parse().map(Self::Url).map_err(Error::ParseUrl) | ||
} | ||
} |