delegate from_uri method to gio::network_address wrapper

This commit is contained in:
yggverse 2024-11-27 15:55:39 +02:00
parent 239786da6a
commit 5737b89278
4 changed files with 35 additions and 22 deletions

View file

@ -0,0 +1,24 @@
pub mod error;
pub use error::Error;
use gio::NetworkAddress;
use glib::Uri;
/// Create new valid [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) from [Uri](https://docs.gtk.org/glib/struct.Uri.html)
///
/// Useful as:
/// * shared [SocketConnectable](https://docs.gtk.org/gio/iface.SocketConnectable.html) interface
/// * [SNI](https://geminiprotocol.net/docs/protocol-specification.gmi#server-name-indication) record for TLS connections
pub fn from_uri(uri: &Uri, default_port: u16) -> Result<NetworkAddress, Error> {
Ok(NetworkAddress::new(
&match uri.host() {
Some(host) => host,
None => return Err(Error::Host(uri.to_string())),
},
if uri.port().is_positive() {
uri.port() as u16
} else {
default_port
},
))
}