begin new multi-protocol parser implementation for Client

This commit is contained in:
yggverse 2025-01-15 06:45:54 +02:00
parent b43f0b68f8
commit 042ace98d3
4 changed files with 88 additions and 0 deletions

View File

@ -1,7 +1,9 @@
mod protocol;
mod redirect;
mod status;
// Children dependencies
use protocol::Protocol;
use redirect::Redirect;
use status::Status;
@ -84,5 +86,11 @@ impl Client {
.replace(Status::failure_redirect_limit(redirect::LIMIT, true));
// @TODO return;
}
// Route request by protocol
match Protocol::from_string(query) {
Protocol::Gemini { uri } | Protocol::Titan { uri } => todo!("{uri}"),
Protocol::Undefined => todo!(),
}
}
}

View File

@ -0,0 +1,34 @@
mod mode;
mod uri;
use mode::Mode;
use gtk::glib::{Uri, UriFlags};
pub enum Protocol {
Gemini { /*mode: Mode,*/ uri: Uri },
Titan { /*mode: Mode,*/ uri: Uri },
Undefined,
}
impl Protocol {
// Constructors
/// Create new `Self` from parsable request string
pub fn from_string(request: &str) -> Self {
match Mode::from_string(request) {
Mode::Default { request } | Mode::Download { request } | Mode::Source { request } => {
match Uri::parse(&request, UriFlags::NONE) {
Ok(uri) => match uri.scheme().as_str() {
"gemini" => Self::Gemini { uri },
"titan" => Self::Titan { uri },
_ => Self::Undefined,
},
Err(_) => Self::Gemini {
uri: uri::tgls(&request),
},
}
}
}
}
}

View File

@ -0,0 +1,28 @@
pub enum Mode {
Default { request: String },
Download { request: String },
Source { request: String },
}
impl Mode {
// Constructors
/// Parse new `Self` from string
pub fn from_string(request: &str) -> Self {
if let Some(postfix) = request.strip_prefix("download:") {
return Self::Download {
request: postfix.to_string(),
};
}
if let Some(postfix) = request.strip_prefix("source:") {
return Self::Source {
request: postfix.to_string(),
};
}
Self::Default {
request: request.to_string(),
}
}
}

View File

@ -0,0 +1,18 @@
//! Search providers [Uri](https://docs.gtk.org/glib/struct.Uri.html) asset
// Global dependencies
use gtk::glib::{Uri, UriFlags};
/// Build TGLS [Uri](https://docs.gtk.org/glib/struct.Uri.html)
pub fn tgls(request: &str) -> Uri {
Uri::build(
UriFlags::NONE,
"gemini",
None,
Some("tlgs.one"),
1965,
"search",
Some(&Uri::escape_string(request, None, false)), // @TODO is `escape_string` really wanted in `build` context?
None,
)
}