diff --git a/src/app/browser/window/tab/item/page/client.rs b/src/app/browser/window/tab/item/page/client.rs index 54846de7..e9b04b0e 100644 --- a/src/app/browser/window/tab/item/page/client.rs +++ b/src/app/browser/window/tab/item/page/client.rs @@ -1,9 +1,9 @@ -mod protocol; +mod feature; mod redirect; mod status; // Children dependencies -use protocol::Protocol; +use feature::Feature; use redirect::Redirect; use status::Status; @@ -88,9 +88,10 @@ impl Client { } // Route request by protocol - match Protocol::from_string(query) { - Protocol::Gemini { uri } | Protocol::Titan { uri } => todo!("{uri}"), - Protocol::Undefined => todo!(), + match Feature::from_string(query) { + Feature::Default { request } + | Feature::Download { request } + | Feature::Source { request } => request.send(), // @TODO } } } diff --git a/src/app/browser/window/tab/item/page/client/protocol/feature.rs b/src/app/browser/window/tab/item/page/client/feature.rs similarity index 70% rename from src/app/browser/window/tab/item/page/client/protocol/feature.rs rename to src/app/browser/window/tab/item/page/client/feature.rs index e720f199..f1ee4e1d 100644 --- a/src/app/browser/window/tab/item/page/client/protocol/feature.rs +++ b/src/app/browser/window/tab/item/page/client/feature.rs @@ -1,12 +1,17 @@ +mod request; + +// Local dependencies +use request::Request; + /// Special features enumeration /// * may not be available for some protocols pub enum Feature { /// Common feature for protocol selected (e.g. browser view) - Default { request: String }, + Default { request: Request }, /// Download request with externally selected method (e.g. to file) - Download { request: String }, + Download { request: Request }, /// View request as the source (like `source-view`) - Source { request: String }, + Source { request: Request }, } impl Feature { @@ -17,18 +22,18 @@ impl Feature { pub fn from_string(request: &str) -> Self { if let Some(postfix) = request.strip_prefix("download:") { return Self::Download { - request: postfix.to_string(), + request: Request::from_string(postfix), }; } if let Some(postfix) = request.strip_prefix("source:") { return Self::Source { - request: postfix.to_string(), + request: Request::from_string(postfix), }; } Self::Default { - request: request.to_string(), + request: Request::from_string(request), } } } diff --git a/src/app/browser/window/tab/item/page/client/feature/request.rs b/src/app/browser/window/tab/item/page/client/feature/request.rs new file mode 100644 index 00000000..98400da6 --- /dev/null +++ b/src/app/browser/window/tab/item/page/client/feature/request.rs @@ -0,0 +1,41 @@ +mod uri; + +// Global dependencies +use gtk::glib::{Uri, UriFlags}; + +pub enum Request { + Gemini { uri: Uri }, + Titan { uri: Uri }, + Undefined, +} + +impl Request { + // Constructors + + /// Create new `Self` from parsable request string + pub fn from_string(request: &str) -> Self { + match Uri::parse(request, UriFlags::NONE) { + Ok(uri) => match uri.scheme().as_str() { + "gemini" => Self::Gemini { uri }, + "titan" => Self::Titan { uri }, + _ => Self::Undefined, + }, + // Search request if the request could not be parsed as the valid [URI](https://docs.gtk.org/glib/struct.Uri.html) + // * @TODO implement DNS resolver lookup before assign this option + Err(_) => Self::Gemini { + uri: uri::tgls(request), + }, + } + } + + // Actions + + /// Send request using protocol driver constructed + pub fn send(&self) { + match self { + Request::Gemini { uri } => todo!("{uri}"), + Request::Titan { uri } => todo!("{uri}"), + Request::Undefined => todo!(), + } + } +} diff --git a/src/app/browser/window/tab/item/page/client/protocol/uri.rs b/src/app/browser/window/tab/item/page/client/feature/request/uri.rs similarity index 69% rename from src/app/browser/window/tab/item/page/client/protocol/uri.rs rename to src/app/browser/window/tab/item/page/client/feature/request/uri.rs index d6554a48..d4fd854f 100644 --- a/src/app/browser/window/tab/item/page/client/protocol/uri.rs +++ b/src/app/browser/window/tab/item/page/client/feature/request/uri.rs @@ -4,7 +4,7 @@ use gtk::glib::{Uri, UriFlags}; /// Build TGLS [Uri](https://docs.gtk.org/glib/struct.Uri.html) -pub fn tgls(request: &str) -> Uri { +pub fn tgls(query: &str) -> Uri { Uri::build( UriFlags::NONE, "gemini", @@ -12,7 +12,7 @@ pub fn tgls(request: &str) -> Uri { Some("tlgs.one"), 1965, "search", - Some(&Uri::escape_string(request, None, false)), // @TODO is `escape_string` really wanted in `build` context? + Some(&Uri::escape_string(query, None, false)), // @TODO is `escape_string` really wanted in `build` context? None, ) } diff --git a/src/app/browser/window/tab/item/page/client/protocol.rs b/src/app/browser/window/tab/item/page/client/protocol.rs deleted file mode 100644 index 7a0f501b..00000000 --- a/src/app/browser/window/tab/item/page/client/protocol.rs +++ /dev/null @@ -1,34 +0,0 @@ -mod feature; -mod uri; - -use feature::Feature; - -use gtk::glib::{Uri, UriFlags}; - -pub enum Protocol { - Gemini { /*feature: Feature,*/ uri: Uri }, - Titan { /*feature: Feature,*/ uri: Uri }, - Undefined, -} - -impl Protocol { - // Constructors - - /// Create new `Self` from parsable request string - pub fn from_string(request: &str) -> Self { - match Feature::from_string(request) { - Feature::Default { request } - | Feature::Download { request } - | Feature::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), - }, - }, - } - } -}