draft new api version

This commit is contained in:
yggverse 2024-10-26 23:22:26 +03:00
parent 8a5f1e2a57
commit 3cde80b6a8
22 changed files with 323 additions and 747 deletions

View file

@ -1,18 +1,18 @@
pub mod error;
pub use error::Error;
use glib::GString;
use glib::{Bytes, GString};
pub struct Body {
buffer: Vec<u8>,
}
impl Body {
/// Construct from response buffer
pub fn from_response(response: &[u8] /* @TODO */) -> Result<Self, Error> {
let start = Self::start(response)?;
// Constructors
pub fn from_response(bytes: &Bytes) -> Result<Self, Error> {
let start = Self::start(bytes)?;
let buffer = match response.get(start..) {
let buffer = match bytes.get(start..) {
Some(result) => result,
None => return Err(Error::Buffer),
};
@ -23,7 +23,7 @@ impl Body {
}
// Getters
pub fn buffer(&self) -> &Vec<u8> {
pub fn buffer(&self) -> &[u8] {
&self.buffer
}

View file

@ -8,8 +8,10 @@ pub use meta::Meta;
pub use mime::Mime;
pub use status::Status;
use glib::Bytes;
pub struct Header {
status: Option<Status>,
status: Status,
meta: Option<Meta>,
mime: Option<Mime>,
// @TODO
@ -18,35 +20,41 @@ pub struct Header {
}
impl Header {
/// Construct from response buffer
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
pub fn from_response(response: &[u8] /* @TODO */) -> Result<Self, Error> {
let end = Self::end(response)?;
// Constructors
pub fn from_response(bytes: &Bytes) -> Result<Self, Error> {
// Get header slice of bytes
let end = Self::end(bytes)?;
let buffer = match response.get(..end) {
Some(result) => result,
let bytes = Bytes::from(match bytes.get(..end) {
Some(buffer) => buffer,
None => return Err(Error::Buffer),
};
});
let meta = match Meta::from_header(buffer) {
Ok(result) => Some(result),
Err(_) => None,
};
// Status is required, parse to continue
let status = match Status::from_header(&bytes) {
Ok(status) => Ok(status),
Err(reason) => Err(match reason {
status::Error::Decode => Error::StatusDecode,
status::Error::Undefined => Error::StatusUndefined,
}),
}?;
let mime = mime::from_header(buffer); // optional
// let charset = charset::from_header(buffer); @TODO
// let language = language::from_header(buffer); @TODO
let status = match status::from_header(buffer) {
Ok(result) => Some(result),
Err(_) => None,
};
Ok(Self { status, meta, mime })
// Done
Ok(Self {
status,
meta: match Meta::from_header(&bytes) {
Ok(meta) => Some(meta),
Err(_) => None,
},
mime: match Mime::from_header(&bytes) {
Ok(mime) => Some(mime),
Err(_) => None,
},
})
}
// Getters
pub fn status(&self) -> &Option<Status> {
pub fn status(&self) -> &Status {
&self.status
}
@ -59,8 +67,10 @@ impl Header {
}
// Tools
fn end(buffer: &[u8]) -> Result<usize, Error> {
for (offset, &byte) in buffer.iter().enumerate() {
/// Get last header byte (until \r)
fn end(bytes: &Bytes) -> Result<usize, Error> {
for (offset, &byte) in bytes.iter().enumerate() {
if byte == b'\r' {
return Ok(offset);
}

View file

@ -1,5 +1,6 @@
pub enum Error {
Buffer,
Format,
Status,
StatusDecode,
StatusUndefined,
}

View file

@ -1,15 +1,18 @@
pub mod error;
pub use error::Error;
use glib::GString;
use glib::{Bytes, GString};
/// Entire meta buffer, but [status code](https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes).
///
/// Usefult to grab placeholder text on 10, 11, 31 codes processing
pub struct Meta {
buffer: Vec<u8>,
}
impl Meta {
pub fn from_header(buffer: &[u8] /* @TODO */) -> Result<Self, Error> {
let buffer = match buffer.get(2..) {
pub fn from_header(bytes: &Bytes) -> Result<Self, Error> {
let buffer = match bytes.get(3..) {
Some(bytes) => bytes.to_vec(),
None => return Err(Error::Undefined),
};
@ -23,4 +26,8 @@ impl Meta {
Err(_) => Err(Error::Undefined),
}
}
pub fn buffer(&self) -> &[u8] {
&self.buffer
}
}

View file

@ -1,6 +1,10 @@
use glib::{GString, Uri};
pub mod error;
pub use error::Error;
use glib::{Bytes, GString, Uri};
use std::path::Path;
/// https://geminiprotocol.net/docs/gemtext-specification.gmi#media-type-parameters
pub enum Mime {
TextGemini,
TextPlain,
@ -10,53 +14,58 @@ pub enum Mime {
ImageWebp,
} // @TODO
pub fn from_header(buffer: &[u8] /* @TODO */) -> Option<Mime> {
from_string(&match GString::from_utf8(buffer.to_vec()) {
Ok(result) => result,
Err(_) => return None, // @TODO error handler?
})
}
impl Mime {
pub fn from_header(bytes: &Bytes) -> Result<Self, Error> {
match bytes.get(..) {
Some(bytes) => match GString::from_utf8(bytes.to_vec()) {
Ok(string) => Self::from_string(string.as_str()),
Err(_) => Err(Error::Decode),
},
None => Err(Error::Undefined),
}
}
pub fn from_path(path: &Path) -> Option<Mime> {
match path.extension().and_then(|extension| extension.to_str()) {
Some("gmi") | Some("gemini") => Some(Mime::TextGemini),
Some("txt") => Some(Mime::TextPlain),
Some("png") => Some(Mime::ImagePng),
Some("gif") => Some(Mime::ImageGif),
Some("jpeg") | Some("jpg") => Some(Mime::ImageJpeg),
Some("webp") => Some(Mime::ImageWebp),
_ => None,
pub fn from_path(path: &Path) -> Result<Self, Error> {
match path.extension().and_then(|extension| extension.to_str()) {
Some("gmi" | "gemini") => Ok(Self::TextGemini),
Some("txt") => Ok(Self::TextPlain),
Some("png") => Ok(Self::ImagePng),
Some("gif") => Ok(Self::ImageGif),
Some("jpeg" | "jpg") => Ok(Self::ImageJpeg),
Some("webp") => Ok(Self::ImageWebp),
_ => Err(Error::Undefined),
}
}
pub fn from_string(value: &str) -> Result<Self, Error> {
if value.contains("text/gemini") {
return Ok(Self::TextGemini);
}
if value.contains("text/plain") {
return Ok(Self::TextPlain);
}
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);
}
Err(Error::Undefined)
}
pub fn from_uri(uri: &Uri) -> Result<Self, Error> {
Self::from_path(Path::new(&uri.to_string()))
}
}
pub fn from_string(value: &str) -> Option<Mime> {
if value.contains("text/gemini") {
return Some(Mime::TextGemini);
}
if value.contains("text/plain") {
return Some(Mime::TextPlain);
}
if value.contains("image/gif") {
return Some(Mime::ImageGif);
}
if value.contains("image/jpeg") {
return Some(Mime::ImageJpeg);
}
if value.contains("image/webp") {
return Some(Mime::ImageWebp);
}
if value.contains("image/png") {
return Some(Mime::ImagePng);
}
None
}
pub fn from_uri(uri: &Uri) -> Option<Mime> {
from_path(Path::new(&uri.to_string()))
}

View file

@ -0,0 +1,4 @@
pub enum Error {
Decode,
Undefined,
}

View file

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