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 struct Meta {
data: Data,
mime: Option<Mime>,
status: Status,
data: Option<Data>,
mime: Option<Mime>,
// @TODO
// charset: Charset,
// language: Language,
@ -127,7 +127,7 @@ impl Meta {
&self.status
}
pub fn data(&self) -> &Data {
pub fn data(&self) -> &Option<Data> {
&self.data
}

View file

@ -13,12 +13,15 @@ pub const MAX_LEN: usize = 0x400; // 1024
/// * placeholder for 10, 11 status
/// * URL for 30, 31 status
pub struct Data {
value: Option<GString>,
value: GString,
}
impl Data {
/// Parse Meta from UTF-8
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> {
/// Parse meta data from UTF-8 buffer
///
/// * 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
let mut bytes: Vec<u8> = Vec::with_capacity(MAX_LEN);
@ -40,11 +43,9 @@ impl Data {
// Assumes the bytes are valid UTF-8
match GString::from_utf8(bytes) {
Ok(value) => Ok(Self {
value: match value.is_empty() {
false => Some(value),
true => None,
},
Ok(value) => Ok(match value.is_empty() {
false => Some(Self { value }),
true => None,
}),
Err(_) => Err(Error::Decode),
}
@ -53,7 +54,7 @@ impl Data {
}
}
pub fn value(&self) -> &Option<GString> {
pub fn value(&self) -> &GString {
&self.value
}
}