implement Titan protocol features

This commit is contained in:
yggverse 2025-01-13 21:22:03 +02:00
parent 66a0de6a8e
commit 29b835411d
8 changed files with 204 additions and 27 deletions

View file

@ -0,0 +1,10 @@
pub mod gemini;
pub mod titan;
pub use gemini::Gemini;
pub use titan::Titan;
pub enum Request {
Gemini(Gemini),
Titan(Titan),
}

View file

@ -0,0 +1,22 @@
use glib::{Bytes, Uri};
/// [Gemini](https://geminiprotocol.net/docs/protocol-specification.gmi) protocol enum object for `Request`
pub struct Gemini {
pub uri: Uri,
}
impl Gemini {
// Constructors
/// Build valid new `Self`
pub fn build(uri: Uri) -> Self {
Self { uri } // @TODO validate
}
// Getters
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
pub fn to_bytes(&self) -> Bytes {
Bytes::from(format!("{}\r\n", self.uri).as_bytes())
}
}

View file

@ -0,0 +1,53 @@
use glib::{Bytes, Uri};
/// [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) protocol enum object for `Request`
pub struct Titan {
pub uri: Uri,
pub size: usize,
pub mime: String,
pub token: Option<String>,
pub data: Vec<u8>,
}
impl Titan {
// Constructors
/// Build valid new `Self`
pub fn build(
uri: Uri,
size: usize,
mime: String,
token: Option<String>,
data: Vec<u8>,
) -> Self {
Self {
uri,
size,
mime,
token,
data,
} // @TODO validate
}
// Getters
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
pub fn to_bytes(&self) -> Bytes {
// Build header
let mut header = format!("{};size={};mime={}", self.uri, self.size, self.mime);
if let Some(ref token) = self.token {
header.push_str(&format!(";token={token}"));
}
header.push_str("\r\n");
let header_bytes = header.into_bytes();
// Build request
let mut bytes: Vec<u8> = Vec::with_capacity(self.size + header_bytes.len());
bytes.extend(header_bytes);
bytes.extend(&self.data);
// Wrap result
Bytes::from(&bytes)
}
}