rename header to meta

This commit is contained in:
yggverse 2025-02-23 10:19:39 +02:00
parent 7c62a393ed
commit 2d096515d4
6 changed files with 38 additions and 38 deletions

View file

@ -1,27 +1,27 @@
pub mod header;
pub use header::Header;
pub mod meta;
pub use meta::Meta;
/// [Success](https://geminiprotocol.net/docs/protocol-specification.gmi#success)
pub struct Default<'a> {
pub data: &'a [u8],
pub header: Header,
pub meta: Meta,
}
impl<'a> Default<'a> {
/// Build `Self` from UTF-8 header bytes
/// Build `Self` from UTF-8 meta bytes
/// * expected buffer includes leading status code, message, CRLF
pub fn from_bytes(buffer: &'a [u8]) -> Result<Self> {
let header = Header::from_bytes(buffer)?;
let meta = Meta::from_bytes(buffer)?;
Ok(Self {
data: buffer.get(header.to_bytes().len()..).unwrap_or(&[]),
header,
data: buffer.get(meta.to_bytes().len()..).unwrap_or(&[]),
meta,
})
}
/// Convert `Self` into UTF-8 bytes presentation
pub fn into_bytes(self) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend(self.header.into_bytes());
bytes.extend(self.meta.into_bytes());
bytes.extend(self.data);
bytes
}
@ -32,7 +32,7 @@ fn test() {
const BYTES: &[u8] = "20 text/gemini\r\nDATA".as_bytes();
let default = Default::from_bytes(BYTES).unwrap();
assert_eq!(default.header.mime, "text/gemini".to_string());
assert_eq!(default.meta.mime, "text/gemini".to_string());
assert_eq!(default.into_bytes(), BYTES);
}

View file

@ -1,11 +1,11 @@
pub const CODE: &[u8] = b"20";
pub struct Header {
pub struct Meta {
pub mime: String,
}
impl Header {
/// Build `Self` from UTF-8 header bytes
impl Meta {
/// Build `Self` from UTF-8 meta bytes
/// * expected buffer includes leading status code, message, CRLF
pub fn from_bytes(buffer: &[u8]) -> Result<Self> {
use crate::Header;
@ -46,10 +46,10 @@ impl Header {
#[test]
fn test() {
const BYTES: &[u8] = "20 text/gemini\r\nDATA".as_bytes();
let header = Header::from_bytes(BYTES).unwrap();
let meta = Meta::from_bytes(BYTES).unwrap();
assert_eq!(header.mime, "text/gemini".to_string());
assert_eq!(header.into_bytes(), BYTES[..BYTES.len() - 4]); // skip DATA
assert_eq!(meta.mime, "text/gemini".to_string());
assert_eq!(meta.into_bytes(), BYTES[..BYTES.len() - 4]); // skip DATA
}
use anyhow::{bail, Result};