mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-04-02 01:55:35 +00:00
remove extra conversions
This commit is contained in:
parent
b5e864e807
commit
c5aada49b4
2 changed files with 13 additions and 15 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
|
|
||||||
use glib::{GString, Regex, RegexCompileFlags, RegexMatchFlags};
|
use glib::{Regex, RegexCompileFlags, RegexMatchFlags};
|
||||||
|
|
||||||
/// MIME type holder for `Response` (by [Gemtext specification](https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters))
|
/// MIME type holder for `Response` (by [Gemtext specification](https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters))
|
||||||
/// * the value stored in lowercase
|
/// * the value stored in lowercase
|
||||||
|
|
@ -29,22 +29,22 @@ impl Mime {
|
||||||
|
|
||||||
// Parse meta bytes only
|
// Parse meta bytes only
|
||||||
match buffer.get(..if len > MAX_LEN { MAX_LEN } else { len }) {
|
match buffer.get(..if len > MAX_LEN { MAX_LEN } else { len }) {
|
||||||
Some(value) => match GString::from_utf8(value.into()) {
|
Some(utf8) => match std::str::from_utf8(utf8) {
|
||||||
Ok(string) => Self::from_string(&string),
|
Ok(s) => Self::from_string(s),
|
||||||
Err(e) => Err(Error::Decode(e)),
|
Err(e) => Err(Error::Decode(e)),
|
||||||
},
|
},
|
||||||
None => Err(Error::Protocol),
|
None => Err(Error::Protocol),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create new `Self` from string that includes **header**
|
/// Create new `Self` from `str::str` 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_string(subject: &str) -> Result<Option<Self>, Error> {
|
pub fn from_string(s: &str) -> Result<Option<Self>, Error> {
|
||||||
if !subject.starts_with("2") {
|
if !s.starts_with("2") {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
match parse(subject) {
|
match parse(s) {
|
||||||
Some(value) => Ok(Some(Self(value.to_lowercase()))),
|
Some(v) => Ok(Some(Self(v.to_lowercase()))),
|
||||||
None => Err(Error::Undefined),
|
None => Err(Error::Undefined),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -58,10 +58,10 @@ impl Mime {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract MIME type from from string that includes **header**
|
/// Extract MIME type from from string that includes **header**
|
||||||
pub fn parse(value: &str) -> Option<String> {
|
pub fn parse(s: &str) -> Option<String> {
|
||||||
Regex::split_simple(
|
Regex::split_simple(
|
||||||
r"^2\d{1}\s([^\/]+\/[^\s;]+)",
|
r"^2\d{1}\s([^\/]+\/[^\s;]+)",
|
||||||
value,
|
s,
|
||||||
RegexCompileFlags::DEFAULT,
|
RegexCompileFlags::DEFAULT,
|
||||||
RegexMatchFlags::DEFAULT,
|
RegexMatchFlags::DEFAULT,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,12 @@
|
||||||
use std::fmt::{Display, Formatter, Result};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Decode(std::string::FromUtf8Error),
|
Decode(std::str::Utf8Error),
|
||||||
Protocol,
|
Protocol,
|
||||||
Undefined,
|
Undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl std::fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Decode(e) => {
|
Self::Decode(e) => {
|
||||||
write!(f, "Decode error: {e}")
|
write!(f, "Decode error: {e}")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue