mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 17:15:31 +00:00
34 lines
953 B
Rust
34 lines
953 B
Rust
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 mime: String,
|
|
pub token: Option<String>,
|
|
pub data: Vec<u8>,
|
|
}
|
|
|
|
impl Titan {
|
|
// Getters
|
|
|
|
/// Copy `Self` to [Bytes](https://docs.gtk.org/glib/struct.Bytes.html)
|
|
pub fn to_bytes(&self) -> Bytes {
|
|
// Calculate data size
|
|
let size = self.data.len();
|
|
|
|
// Build header
|
|
let mut header = format!("{};size={size};mime={}", self.uri, self.mime);
|
|
if let Some(ref token) = self.token {
|
|
header.push_str(&format!(";token={token}"));
|
|
}
|
|
header.push_str("\r\n");
|
|
|
|
// Build request
|
|
let mut bytes: Vec<u8> = Vec::with_capacity(size + 1024); // @TODO
|
|
bytes.extend(header.into_bytes());
|
|
bytes.extend(&self.data);
|
|
|
|
// Wrap result
|
|
Bytes::from(&bytes)
|
|
}
|
|
}
|