Generate Post link when the protocol is post.

This commit is contained in:
Tom Alexander 2023-12-23 20:55:32 -05:00
parent 93e0a2fe98
commit 6a086d57de
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -41,14 +41,24 @@ intermediate!(
#[derive(Debug, Clone)]
pub(crate) enum LinkTarget {
Raw(String),
Post { post_id: String },
Post { post_id: Option<String> },
}
impl LinkTarget {
pub(crate) fn from_string<S: AsRef<str>>(input: S) -> Result<LinkTarget, CustomError> {
fn impl_from_string(input: &str) -> Result<LinkTarget, CustomError> {
let parsed = Url::parse(input)?;
Ok(LinkTarget::Raw(input.to_owned()))
match parsed.scheme() {
"post" => {
let post_id = parsed
.path_segments()
.ok_or_else(|| "cannot be base")?
.next()
.map(str::to_owned);
Ok(LinkTarget::Post { post_id })
}
_ => Ok(LinkTarget::Raw(input.to_owned())),
}
}
impl_from_string(input.as_ref())
}