dump unsupported mime type string

This commit is contained in:
yggverse 2024-12-12 10:00:41 +02:00
parent 76235da239
commit 2c88f12f2d
2 changed files with 22 additions and 6 deletions

View file

@ -8,7 +8,7 @@
pub mod error; pub mod error;
pub use error::Error; pub use error::Error;
use glib::{GString, Uri}; use glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags, Uri};
use std::path::Path; use std::path::Path;
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters /// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
@ -80,7 +80,7 @@ impl Mime {
Some("flac") => Ok(Self::AudioFlac), Some("flac") => Ok(Self::AudioFlac),
Some("mp3") => Ok(Self::AudioMpeg), Some("mp3") => Ok(Self::AudioMpeg),
Some("oga" | "ogg" | "opus" | "spx") => Ok(Self::AudioOgg), Some("oga" | "ogg" | "opus" | "spx") => Ok(Self::AudioOgg),
_ => Err(Error::Undefined), _ => Err(Error::Undefined(None)),
} // @TODO extension to lowercase } // @TODO extension to lowercase
} }
@ -155,7 +155,16 @@ impl Mime {
// Some type exist, but not defined yet (on status code is 2*) // Some type exist, but not defined yet (on status code is 2*)
if value.starts_with("2") && value.contains("/") { if value.starts_with("2") && value.contains("/") {
return Err(Error::Undefined); return Err(Error::Undefined(
Regex::split_simple(
r"^2\d{1}\s([^\/]+\/[^\s]+)",
value,
RegexCompileFlags::DEFAULT,
RegexMatchFlags::DEFAULT,
)
.get(1)
.map(|this| this.to_string()),
));
} }
// Done // Done

View file

@ -4,7 +4,7 @@ use std::fmt::{Display, Formatter, Result};
pub enum Error { pub enum Error {
Decode(std::string::FromUtf8Error), Decode(std::string::FromUtf8Error),
Protocol, Protocol,
Undefined, Undefined(Option<String>),
} }
impl Display for Error { impl Display for Error {
@ -16,8 +16,15 @@ impl Display for Error {
Self::Protocol => { Self::Protocol => {
write!(f, "Protocol error") write!(f, "Protocol error")
} }
Self::Undefined => { Self::Undefined(e) => {
write!(f, "Undefined") write!(
f,
"{}",
match e {
Some(value) => format!("`{value}` undefined"),
None => "Could not parse value".to_string(),
}
)
} }
} }
} }