Parsing fixes

This commit is contained in:
Natty 2023-04-22 02:17:24 +02:00
parent b93e36b0e0
commit 3826b9fc2f
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
2 changed files with 8 additions and 4 deletions

View File

@ -14,6 +14,7 @@ pub struct MagnetarNetworking {
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum MagnetarNetworkingProtocol { pub enum MagnetarNetworkingProtocol {
Http, Http,
Https, Https,

View File

@ -13,7 +13,7 @@ impl<S1: AsRef<str>, S2: AsRef<str>> From<(S1, Option<S2>)> for FediverseTag {
fn from((name, host): (S1, Option<S2>)) -> Self { fn from((name, host): (S1, Option<S2>)) -> Self {
Self { Self {
name: name.as_ref().to_owned(), name: name.as_ref().to_owned(),
host: host.as_ref().map(S2::as_ref).map(&str::to_owned), host: host.as_ref().map(S2::as_ref).map(str::to_owned),
} }
} }
} }
@ -40,7 +40,7 @@ pub fn lenient_parse_acct(acct: &Acct) -> anyhow::Result<FediverseTag> {
fn split_tag_inner(tag: impl AsRef<str>) -> (String, Option<String>) { fn split_tag_inner(tag: impl AsRef<str>) -> (String, Option<String>) {
let tag = tag.as_ref(); let tag = tag.as_ref();
let tag = tag.strip_prefix("@").unwrap_or(tag.as_ref()); let tag = tag.strip_prefix('@').unwrap_or(tag.as_ref());
match tag.split_once('@') { match tag.split_once('@') {
Some((name, host)) if name.is_empty() => (host.to_owned(), None), Some((name, host)) if name.is_empty() => (host.to_owned(), None),
@ -50,11 +50,14 @@ fn split_tag_inner(tag: impl AsRef<str>) -> (String, Option<String>) {
} }
fn validate_tag_inner((name, host): (&str, Option<&str>)) -> anyhow::Result<()> { fn validate_tag_inner((name, host): (&str, Option<&str>)) -> anyhow::Result<()> {
if name.chars().any(|c| !c.is_alphanumeric()) { if name
.chars()
.any(|c| !c.is_alphanumeric() && c != '-' && c != '.')
{
return Err(anyhow!("Invalid char in tag: {name}")); return Err(anyhow!("Invalid char in tag: {name}"));
} }
if let Some(ref host_str) = host { if let Some(host_str) = host {
if host_str if host_str
.chars() .chars()
.any(|c| c.is_control() || c.is_whitespace() || c == '/' || c == '#') .any(|c| c.is_control() || c.is_whitespace() || c == '/' || c == '#')