From 042ace98d34d9fd16a6c80b1ad123ebaac76088d Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 15 Jan 2025 06:45:54 +0200 Subject: [PATCH] begin new multi-protocol parser implementation for `Client` --- .../browser/window/tab/item/page/client.rs | 8 +++++ .../window/tab/item/page/client/protocol.rs | 34 +++++++++++++++++++ .../tab/item/page/client/protocol/mode.rs | 28 +++++++++++++++ .../tab/item/page/client/protocol/uri.rs | 18 ++++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/app/browser/window/tab/item/page/client/protocol.rs create mode 100644 src/app/browser/window/tab/item/page/client/protocol/mode.rs create mode 100644 src/app/browser/window/tab/item/page/client/protocol/uri.rs diff --git a/src/app/browser/window/tab/item/page/client.rs b/src/app/browser/window/tab/item/page/client.rs index de94f0d3..54846de7 100644 --- a/src/app/browser/window/tab/item/page/client.rs +++ b/src/app/browser/window/tab/item/page/client.rs @@ -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!(), + } } } diff --git a/src/app/browser/window/tab/item/page/client/protocol.rs b/src/app/browser/window/tab/item/page/client/protocol.rs new file mode 100644 index 00000000..4e24a854 --- /dev/null +++ b/src/app/browser/window/tab/item/page/client/protocol.rs @@ -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), + }, + } + } + } + } +} diff --git a/src/app/browser/window/tab/item/page/client/protocol/mode.rs b/src/app/browser/window/tab/item/page/client/protocol/mode.rs new file mode 100644 index 00000000..d991ff87 --- /dev/null +++ b/src/app/browser/window/tab/item/page/client/protocol/mode.rs @@ -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(), + } + } +} diff --git a/src/app/browser/window/tab/item/page/client/protocol/uri.rs b/src/app/browser/window/tab/item/page/client/protocol/uri.rs new file mode 100644 index 00000000..d6554a48 --- /dev/null +++ b/src/app/browser/window/tab/item/page/client/protocol/uri.rs @@ -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, + ) +}