I thoroughly apologise to the people who want to use this crate seeing the nightmare of updates torrenting out today but the lack of documentation on the gemini module makes me extremely angry and you like documentation too so here you go.

This commit is contained in:
abbie 2022-03-12 21:05:43 +00:00
parent 1825e0caeb
commit f3279b6188
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87
2 changed files with 19 additions and 19 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "packeteer" name = "packeteer"
description = "An attempt at a Rust library that can be used to assist in programmatically analysing, serving and handling received protocol packets." description = "An attempt at a Rust library that can be used to assist in programmatically analysing, serving and handling received protocol packets."
version = "0.2.1" version = "0.2.2"
edition = "2021" edition = "2021"
authors = ["Celeste <colean@colean.cc>"] authors = ["Celeste <colean@colean.cc>"]
license = "LGPL-3.0-or-later" license = "LGPL-3.0-or-later"

View file

@ -3,15 +3,15 @@
use super::{Url, wrap_url, unwrap_url}; use super::{Url, wrap_url, unwrap_url};
use std::str::FromStr; use std::str::FromStr;
// A structure for Gemini requests. /// A structure for Gemini requests.
// Generated with packeteer::gemini::generate_request /// Generated with packeteer::gemini::generate_request
#[derive(Debug)] #[derive(Debug)]
pub struct GeminiRequest { pub struct GeminiRequest {
pub url: Url, pub url: Url,
} }
// A structure for Gemini responses. /// A structure for Gemini responses.
// Generated with packeteer::gemini::generate_response /// Generated with packeteer::gemini::generate_response
#[derive(Debug)] #[derive(Debug)]
pub struct GeminiResponse { pub struct GeminiResponse {
pub code: i32, pub code: i32,
@ -19,15 +19,15 @@ pub struct GeminiResponse {
pub body: String, pub body: String,
} }
// A function for generating GeminiRequest structures. /// A function for generating GeminiRequest structures.
pub fn generate_request(url: &str) -> GeminiRequest { pub fn generate_request(url: &str) -> GeminiRequest {
let wurl = wrap_url(url); let wurl = wrap_url(url);
let gem = GeminiRequest { url: wurl }; let gem = GeminiRequest { url: wurl };
return gem return gem
} }
// A function for generating GeminiResponse structures. /// A function for generating GeminiResponse structures.
// Any invalid status code (valid status codes are 69 > x >= 10) will automatically be switched to 40 (Temporary failure). /// Any invalid status code (valid status codes are 69 > x >= 10) will automatically be switched to 40 (Temporary failure).
pub fn generate_response(code: i32, body: &str) -> GeminiResponse { pub fn generate_response(code: i32, body: &str) -> GeminiResponse {
let mut xcode = code; let mut xcode = code;
if code < 10 { xcode = 40 }; if code < 10 { xcode = 40 };
@ -36,26 +36,26 @@ pub fn generate_response(code: i32, body: &str) -> GeminiResponse {
return res return res
} }
// A function for converting GeminiRequest structures into a Gemini request string. /// A function for converting GeminiRequest structures into a Gemini request string.
// This is extremely simple since GeminiRequest is basically storing a Url. /// This is extremely simple since GeminiRequest is basically storing a Url.
// Unpacking functions are simply taking the data stored in the structure and concatenating it into a string so nothing changes apart from the conversion. /// Unpacking functions are simply taking the data stored in the structure and concatenating it into a string so nothing changes apart from the conversion.
pub fn unpack_request(req: GeminiRequest) -> String { pub fn unpack_request(req: GeminiRequest) -> String {
let url = unwrap_url(req.url); let url = unwrap_url(req.url);
let string = format!("{}\r\n", url); let string = format!("{}\r\n", url);
return string.to_string() return string.to_string()
} }
// A function for converting GeminiResponse structures into split header and body strings. /// A function for converting GeminiResponse structures into split header and body strings.
// The header and body are split as the Gemini specification seperates them. /// The header and body are split as the Gemini specification seperates them.
// Unpacking functions are simply taking the data stored in the structure and concatenating it into a string so nothing changes apart from the conversion. /// Unpacking functions are simply taking the data stored in the structure and concatenating it into a string so nothing changes apart from the conversion.
pub fn unpack_response(res: GeminiResponse) -> (String, String) { pub fn unpack_response(res: GeminiResponse) -> (String, String) {
let string = format!("{} {}\r\n", res.code, res.meta); let string = format!("{} {}\r\n", res.code, res.meta);
return (string.to_string(), res.body) return (string.to_string(), res.body)
} }
// A function for converting Gemini request strings into GeminiRequest. /// A function for converting Gemini request strings into GeminiRequest.
// This is essentially equivalent to packeteer::gemini::generate_request apart from the fact it automatically removes the character return and line feed after the URL to allow for conversion from raw Gemini requests. /// This is essentially equivalent to packeteer::gemini::generate_request apart from the fact it automatically removes the character return and line feed after the URL to allow for conversion from raw Gemini requests.
// Construction functions aim to be as transparent as possible so apart from turning it into a structure, the request stays relatively unchanged. /// Construction functions aim to be as transparent as possible so apart from turning it into a structure, the request stays relatively unchanged.
pub fn construct_request(req: &str) -> GeminiRequest { pub fn construct_request(req: &str) -> GeminiRequest {
let url: Vec<&str> = req.split("\r\n").collect(); let url: Vec<&str> = req.split("\r\n").collect();
let wurl = wrap_url(url[0]); let wurl = wrap_url(url[0]);
@ -63,8 +63,8 @@ pub fn construct_request(req: &str) -> GeminiRequest {
return gem return gem
} }
// A function for converting Gemini header and body strings into GeminiResponse. /// A function for converting Gemini header and body strings into GeminiResponse.
// Construction functions aim to be as transparent as possible so apart from turning it into a structure, the response stays relatively unchanged. /// Construction functions aim to be as transparent as possible so apart from turning it into a structure, the response stays relatively unchanged.
pub fn construct_response(res: &str, body: &str) -> GeminiResponse { pub fn construct_response(res: &str, body: &str) -> GeminiResponse {
let split1: Vec<&str> = res.split("\r\n").collect(); let split1: Vec<&str> = res.split("\r\n").collect();
let split2: Vec<&str> = split1[0].split(" ").collect(); let split2: Vec<&str> = split1[0].split(" ").collect();