change Mime struct, implement Display trait

This commit is contained in:
yggverse 2025-01-18 02:08:19 +02:00
parent 09b2c626a3
commit d3133f50f7

View file

@ -6,11 +6,17 @@ pub use error::Error;
use glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags}; use glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags};
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters /// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
pub struct Mime { pub struct Mime(String);
pub value: String,
impl std::fmt::Display for Mime {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
} }
impl Mime { impl Mime {
// Constructors
/// Create new `Self` from UTF-8 buffer (that includes **header**) /// Create new `Self` from UTF-8 buffer (that includes **header**)
/// * return `None` for non 2* [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes) /// * return `None` for non 2* [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes)
pub fn from_utf8(buffer: &[u8]) -> Result<Option<Self>, Error> { pub fn from_utf8(buffer: &[u8]) -> Result<Option<Self>, Error> {
@ -37,10 +43,17 @@ impl Mime {
return Ok(None); return Ok(None);
} }
match parse(subject) { match parse(subject) {
Some(value) => Ok(Some(Self { value })), Some(value) => Ok(Some(Self(value))),
None => Err(Error::Undefined), None => Err(Error::Undefined),
} }
} }
// Getters
/// Get `Self` as `&str`
pub fn as_str(&self) -> &str {
&self.0
}
} }
/// Extract MIME type from from string that includes **header** /// Extract MIME type from from string that includes **header**