implement Uri reference getter

This commit is contained in:
yggverse 2025-01-22 16:27:28 +02:00
parent ea9d7e4c5d
commit caa61bb808

View file

@ -6,7 +6,10 @@ pub use error::Error;
pub use gemini::Gemini; pub use gemini::Gemini;
pub use titan::Titan; pub use titan::Titan;
// Local dependencies
use gio::NetworkAddress; use gio::NetworkAddress;
use glib::Uri;
/// Single `Request` implementation for different protocols /// Single `Request` implementation for different protocols
pub enum Request { pub enum Request {
@ -17,7 +20,7 @@ pub enum Request {
impl Request { impl Request {
// Getters // Getters
/// Get header string for `Self` /// Generate header string for `Self`
pub fn header(&self) -> String { pub fn header(&self) -> String {
match self { match self {
Self::Gemini(ref this) => this.header(), Self::Gemini(ref this) => this.header(),
@ -25,15 +28,17 @@ impl Request {
} }
} }
/// Get reference to `Self` [Uri](https://docs.gtk.org/glib/struct.Uri.html)
pub fn uri(&self) -> &Uri {
match self {
Self::Gemini(ref this) => &this.uri,
Self::Titan(ref this) => &this.uri,
}
}
/// Get [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) for `Self` /// Get [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) for `Self`
pub fn to_network_address(&self, default_port: u16) -> Result<NetworkAddress, Error> { pub fn to_network_address(&self, default_port: u16) -> Result<NetworkAddress, Error> {
match crate::gio::network_address::from_uri( match crate::gio::network_address::from_uri(self.uri(), default_port) {
&match self {
Self::Gemini(ref request) => request.uri.clone(),
Self::Titan(ref request) => request.uri.clone(),
},
default_port,
) {
Ok(network_address) => Ok(network_address), Ok(network_address) => Ok(network_address),
Err(e) => Err(Error::NetworkAddress(e)), Err(e) => Err(Error::NetworkAddress(e)),
} }