update api version with new implementation

This commit is contained in:
yggverse 2024-10-30 18:28:20 +02:00
parent 47f58f800d
commit 93985095a5
14 changed files with 221 additions and 178 deletions

View file

@ -0,0 +1 @@
// @TODO

View file

@ -0,0 +1,59 @@
pub mod error;
pub use error::Error;
use glib::GString;
pub const MAX_LEN: usize = 0x400; // 1024
/// Meta data holder for response
///
/// Could be created from entire response buffer or just header slice
///
/// Use as:
/// * placeholder for 10, 11 status
/// * URL for 30, 31 status
pub struct Data {
value: Option<GString>,
}
impl Data {
/// Parse Meta from UTF-8
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> {
// Init bytes buffer
let mut bytes: Vec<u8> = Vec::with_capacity(MAX_LEN);
// Calculate len once
let len = buffer.len();
// Skip 3 bytes for status code of `MAX_LEN` expected
match buffer.get(3..if len > MAX_LEN { MAX_LEN - 3 } else { len }) {
Some(slice) => {
for &byte in slice {
// End of header
if byte == b'\r' {
break;
}
// Continue
bytes.push(byte);
}
// 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,
},
}),
Err(_) => Err(Error::Decode),
}
}
None => Err(Error::Protocol),
}
}
pub fn value(&self) -> &Option<GString> {
&self.value
}
}

View file

@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum Error {
Decode,
Protocol,
}

View file

@ -0,0 +1,13 @@
#[derive(Debug)]
pub enum Error {
DataDecode,
DataProtocol,
InputStream,
MimeDecode,
MimeProtocol,
MimeUndefined,
Protocol,
StatusDecode,
StatusProtocol,
StatusUndefined,
}

View file

@ -0,0 +1 @@
// @TODO

View file

@ -0,0 +1,102 @@
pub mod error;
pub use error::Error;
use glib::{GString, Uri};
use std::path::Path;
pub const MAX_LEN: usize = 0x400; // 1024
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
#[derive(Debug)]
pub enum Mime {
// Text
TextGemini,
TextPlain,
// Image
ImageGif,
ImageJpeg,
ImagePng,
ImageWebp,
// Audio
AudioFlac,
AudioMpeg,
AudioOgg,
} // @TODO
impl Mime {
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> {
let len = buffer.len();
match buffer.get(..if len > MAX_LEN { MAX_LEN } else { len }) {
Some(value) => match GString::from_utf8(value.into()) {
Ok(string) => Self::from_string(string.as_str()),
Err(_) => Err(Error::Decode),
},
None => Err(Error::Protocol),
}
}
pub fn from_path(path: &Path) -> Result<Self, Error> {
match path.extension().and_then(|extension| extension.to_str()) {
// Text
Some("gmi" | "gemini") => Ok(Self::TextGemini),
Some("txt") => Ok(Self::TextPlain),
// Image
Some("gif") => Ok(Self::ImageGif),
Some("jpeg" | "jpg") => Ok(Self::ImageJpeg),
Some("png") => Ok(Self::ImagePng),
Some("webp") => Ok(Self::ImageWebp),
// Audio
Some("flac") => Ok(Self::AudioFlac),
Some("mp3") => Ok(Self::AudioMpeg),
Some("oga" | "ogg" | "opus" | "spx") => Ok(Self::AudioOgg),
_ => Err(Error::Undefined),
} // @TODO extension to lowercase
}
pub fn from_string(value: &str) -> Result<Self, Error> {
// Text
if value.contains("text/gemini") {
return Ok(Self::TextGemini);
}
if value.contains("text/plain") {
return Ok(Self::TextPlain);
}
// Image
if value.contains("image/gif") {
return Ok(Self::ImageGif);
}
if value.contains("image/jpeg") {
return Ok(Self::ImageJpeg);
}
if value.contains("image/webp") {
return Ok(Self::ImageWebp);
}
if value.contains("image/png") {
return Ok(Self::ImagePng);
}
// Audio
if value.contains("audio/flac") {
return Ok(Self::AudioFlac);
}
if value.contains("audio/mpeg") {
return Ok(Self::AudioMpeg);
}
if value.contains("audio/ogg") {
return Ok(Self::AudioOgg);
}
Err(Error::Undefined)
}
pub fn from_uri(uri: &Uri) -> Result<Self, Error> {
Self::from_path(Path::new(&uri.to_string()))
}
}

View file

@ -0,0 +1,6 @@
#[derive(Debug)]
pub enum Error {
Decode,
Protocol,
Undefined,
}

View file

@ -0,0 +1,40 @@
pub mod error;
pub use error::Error;
use glib::GString;
/// https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes
#[derive(Debug)]
pub enum Status {
// 10 | 11
Input,
SensitiveInput,
// 20
Success,
// 30 | 31
Redirect,
PermanentRedirect,
} // @TODO
impl Status {
pub fn from_utf8(buffer: &[u8]) -> Result<Self, Error> {
match buffer.get(0..2) {
Some(value) => match GString::from_utf8(value.to_vec()) {
Ok(string) => Self::from_string(string.as_str()),
Err(_) => Err(Error::Decode),
},
None => Err(Error::Protocol),
}
}
pub fn from_string(code: &str) -> Result<Self, Error> {
match code {
"10" => Ok(Self::Input),
"11" => Ok(Self::SensitiveInput),
"20" => Ok(Self::Success),
"30" => Ok(Self::Redirect),
"31" => Ok(Self::PermanentRedirect),
_ => Err(Error::Undefined),
}
}
}

View file

@ -0,0 +1,6 @@
#[derive(Debug)]
pub enum Error {
Decode,
Protocol,
Undefined,
}