Skip to content

Commit

Permalink
change Mime struct, implement Display trait
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Jan 18, 2025
1 parent 09b2c62 commit d3133f5
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/client/connection/response/meta/mime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ pub use error::Error;
use glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags};

/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
pub struct Mime {
pub value: String,
pub struct Mime(String);

impl std::fmt::Display for Mime {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl Mime {
// Constructors

/// 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)
pub fn from_utf8(buffer: &[u8]) -> Result<Option<Self>, Error> {
Expand All @@ -37,10 +43,17 @@ impl Mime {
return Ok(None);
}
match parse(subject) {
Some(value) => Ok(Some(Self { value })),
Some(value) => Ok(Some(Self(value))),
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**
Expand Down

0 comments on commit d3133f5

Please sign in to comment.