titanite 0.3.0

Client/Server Library for Gemini protocol with Titan support
Documentation
pub mod meta;
pub use meta::Meta;

/// [Titan](gemini://transjovian.org/titan/page/The%20Titan%20Specification) request
pub struct Titan<'a> {
    pub data: &'a [u8],
    pub meta: Meta,
}

impl<'a> Titan<'a> {
    pub fn from_bytes(buffer: &'a [u8]) -> Result<Self> {
        let meta = Meta::from_bytes(buffer)?;
        let data = buffer.get(meta.to_bytes().len()..).unwrap_or(&[]);
        if meta.size != data.len() {
            bail!("Data size mismatch ({}:{})", meta.size, data.len())
        }
        Ok(Self { data, meta })
    }
    pub fn into_bytes(self) -> Vec<u8> {
        let mut bytes = self.meta.into_bytes();
        bytes.extend(self.data);
        bytes
    }
}

#[test]
fn test() {
    const BYTES: &[u8] =
        "titan://geminiprotocol.net/raw/path;size=4;mime=text/plain;token=token?key1=value1&key2=value2\r\nDATA"
            .as_bytes();

    let bytes = Titan::from_bytes(BYTES).unwrap().into_bytes();

    // println!("{:?}", std::str::from_utf8(&bytes));
    // println!("{:?}", std::str::from_utf8(&BYTES));

    assert_eq!(bytes, BYTES);
}

use anyhow::{bail, Result};