make data optional on empty

This commit is contained in:
yggverse 2024-11-01 05:21:47 +02:00
parent a0827e0425
commit 10205f3147
2 changed files with 13 additions and 12 deletions

View file

@ -22,9 +22,9 @@ use glib::Priority;
pub const MAX_LEN: usize = 0x400; // 1024 pub const MAX_LEN: usize = 0x400; // 1024
pub struct Meta { pub struct Meta {
data: Data,
mime: Option<Mime>,
status: Status, status: Status,
data: Option<Data>,
mime: Option<Mime>,
// @TODO // @TODO
// charset: Charset, // charset: Charset,
// language: Language, // language: Language,
@ -127,7 +127,7 @@ impl Meta {
&self.status &self.status
} }
pub fn data(&self) -> &Data { pub fn data(&self) -> &Option<Data> {
&self.data &self.data
} }

View file

@ -13,12 +13,15 @@ pub const MAX_LEN: usize = 0x400; // 1024
/// * placeholder for 10, 11 status /// * placeholder for 10, 11 status
/// * URL for 30, 31 status /// * URL for 30, 31 status
pub struct Data { pub struct Data {
value: Option<GString>, value: GString,
} }
impl Data { impl Data {
/// Parse Meta from UTF-8 /// Parse meta data from UTF-8 buffer
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> { ///
/// * result could be `None` for some [status codes](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes)
/// that does not expect any data in header
pub fn from_utf8(buffer: &[u8]) -> Result<Option<Self>, Error> {
// Init bytes buffer // Init bytes buffer
let mut bytes: Vec<u8> = Vec::with_capacity(MAX_LEN); let mut bytes: Vec<u8> = Vec::with_capacity(MAX_LEN);
@ -40,11 +43,9 @@ impl Data {
// Assumes the bytes are valid UTF-8 // Assumes the bytes are valid UTF-8
match GString::from_utf8(bytes) { match GString::from_utf8(bytes) {
Ok(value) => Ok(Self { Ok(value) => Ok(match value.is_empty() {
value: match value.is_empty() { false => Some(Self { value }),
false => Some(value), true => None,
true => None,
},
}), }),
Err(_) => Err(Error::Decode), Err(_) => Err(Error::Decode),
} }
@ -53,7 +54,7 @@ impl Data {
} }
} }
pub fn value(&self) -> &Option<GString> { pub fn value(&self) -> &GString {
&self.value &self.value
} }
} }