From 8f910672e209e89ea14e8290d8c89b44662b0606 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 1 Dec 2024 04:35:19 +0200 Subject: [PATCH] require Priority, Cancellable arguments, remove extra members --- src/client.rs | 33 +++++++------------------ src/client/connection.rs | 41 ++++---------------------------- src/client/connection/error.rs | 12 ---------- src/client/response.rs | 4 ++-- src/client/response/data/text.rs | 18 +++++--------- src/client/response/meta.rs | 18 +++++--------- src/gio/memory_input_stream.rs | 6 ++--- 7 files changed, 30 insertions(+), 102 deletions(-) diff --git a/src/client.rs b/src/client.rs index cb10c0f..0638661 100644 --- a/src/client.rs +++ b/src/client.rs @@ -71,8 +71,8 @@ impl Client { pub fn request_async( &self, uri: Uri, - priority: Option, - cancellable: Option, + priority: Priority, + cancellable: Cancellable, certificate: Option, callback: impl Fn(Result) + 'static, ) { @@ -86,20 +86,12 @@ impl Client { match crate::gio::network_address::from_uri(&uri, crate::DEFAULT_PORT) { Ok(network_address) => self.socket.connect_async( &network_address.clone(), - match cancellable { - Some(ref cancellable) => Some(cancellable.clone()), - None => None::, - } - .as_ref(), + Some(&cancellable.clone()), move |result| match result { Ok(socket_connection) => { // Wrap required connection dependencies into the struct holder - match Connection::new( - socket_connection, - certificate, - Some(network_address), - cancellable.clone(), - ) { + match Connection::new(socket_connection, certificate, Some(network_address)) + { Ok(connection) => { // Begin new request request_async( @@ -126,21 +118,14 @@ impl Client { pub fn request_async( connection: Connection, query: String, - priority: Option, - cancellable: Option, + priority: Priority, + cancellable: Cancellable, callback: impl Fn(Result) + 'static, ) { connection.stream().output_stream().write_bytes_async( &Bytes::from(format!("{query}\r\n").as_bytes()), - match priority { - Some(priority) => priority, - None => Priority::DEFAULT, - }, - match cancellable { - Some(ref cancellable) => Some(cancellable.clone()), - None => None::, - } - .as_ref(), + priority, + Some(&cancellable.clone()), move |result| match result { Ok(_) => { Response::from_request_async(connection, priority, cancellable, move |result| { diff --git a/src/client/connection.rs b/src/client/connection.rs index ee2d4e1..004e5ff 100644 --- a/src/client/connection.rs +++ b/src/client/connection.rs @@ -2,13 +2,12 @@ pub mod error; pub use error::Error; use gio::{ - prelude::{CancellableExt, IOStreamExt, TlsConnectionExt}, - Cancellable, IOStream, NetworkAddress, SocketConnection, TlsCertificate, TlsClientConnection, + prelude::TlsConnectionExt, IOStream, NetworkAddress, SocketConnection, TlsCertificate, + TlsClientConnection, }; use glib::object::{Cast, IsA, ObjectExt}; pub struct Connection { - pub cancellable: Option, pub socket_connection: SocketConnection, pub tls_client_connection: TlsClientConnection, } @@ -21,17 +20,11 @@ impl Connection { socket_connection: SocketConnection, certificate: Option, server_identity: Option, - cancellable: Option, ) -> Result { - if socket_connection.is_closed() { - return Err(Error::Closed); - } - Ok(Self { - cancellable, socket_connection: socket_connection.clone(), tls_client_connection: match TlsClientConnection::new( - &socket_connection.clone(), + &socket_connection, server_identity.as_ref(), ) { Ok(tls_client_connection) => { @@ -58,38 +51,12 @@ impl Connection { }) } - // Actions - - /// Apply `cancel` action to `Self` [Cancellable](https://docs.gtk.org/gio/method.Cancellable.cancel.html) - /// * return `Error` on `Cancellable` not found - pub fn cancel(&self) -> Result<(), Error> { - match self.cancellable { - Some(ref cancellable) => { - cancellable.cancel(); - Ok(()) - } - None => Err(Error::Cancel), - } - } - - /// Close owned [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html) - /// * return `Ok(false)` if `Cancellable` not defined - pub fn close(&self) -> Result { - match self.cancellable { - Some(ref cancellable) => match self.socket_connection.close(Some(cancellable)) { - Ok(()) => Ok(true), - Err(e) => Err(Error::SocketConnection(e)), - }, - None => Ok(false), - } - } - // Getters /// Get [IOStream](https://docs.gtk.org/gio/class.IOStream.html) /// for [SocketConnection](https://docs.gtk.org/gio/class.SocketConnection.html) /// or [TlsClientConnection](https://docs.gtk.org/gio/iface.TlsClientConnection.html) (if available) - /// * compatible with user (certificate) and guest (certificate-less) connection types + /// * compatible with user (certificate) and guest (certificate-less) connection type /// * useful also to keep `Connection` active in async I/O context pub fn stream(&self) -> impl IsA { // * do not replace with `tls_client_connection.base_io_stream()` diff --git a/src/client/connection/error.rs b/src/client/connection/error.rs index 45cdd87..ab6ff8e 100644 --- a/src/client/connection/error.rs +++ b/src/client/connection/error.rs @@ -2,24 +2,12 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug)] pub enum Error { - Cancel, - Closed, - Rehandshake(glib::Error), - SocketConnection(glib::Error), TlsClientConnection(glib::Error), } impl Display for Error { fn fmt(&self, f: &mut Formatter) -> Result { match self { - Self::Cancel => write!(f, "Cancellable not found"), - Self::Closed => write!(f, "Connection closed"), - Self::Rehandshake(e) => { - write!(f, "Rehandshake error: {e}") - } - Self::SocketConnection(e) => { - write!(f, "Socket connection error: {e}") - } Self::TlsClientConnection(e) => { write!(f, "TLS client connection error: {e}") } diff --git a/src/client/response.rs b/src/client/response.rs index b4ebe87..be2f7d8 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -21,8 +21,8 @@ impl Response { pub fn from_request_async( connection: Connection, - priority: Option, - cancellable: Option, + priority: Priority, + cancellable: Cancellable, callback: impl FnOnce(Result) + 'static, ) { Meta::from_stream_async(connection.stream(), priority, cancellable, |result| { diff --git a/src/client/response/data/text.rs b/src/client/response/data/text.rs index 4de4654..2d3cf2c 100644 --- a/src/client/response/data/text.rs +++ b/src/client/response/data/text.rs @@ -51,21 +51,15 @@ impl Text { /// Asynchronously create new `Self` from [IOStream](https://docs.gtk.org/gio/class.IOStream.html) pub fn from_stream_async( stream: impl IsA, - priority: Option, - cancellable: Option, + priority: Priority, + cancellable: Cancellable, on_complete: impl FnOnce(Result) + 'static, ) { read_all_from_stream_async( Vec::with_capacity(BUFFER_CAPACITY), stream, - match cancellable { - Some(value) => Some(value), - None => None::, - }, - match priority { - Some(value) => value, - None => Priority::DEFAULT, - }, + cancellable, + priority, |result| match result { Ok(buffer) => on_complete(Self::from_utf8(&buffer)), Err(e) => on_complete(Err(e)), @@ -83,14 +77,14 @@ impl Text { pub fn read_all_from_stream_async( mut buffer: Vec, stream: impl IsA, - cancelable: Option, + cancelable: Cancellable, priority: Priority, callback: impl FnOnce(Result, Error>) + 'static, ) { stream.input_stream().read_bytes_async( BUFFER_CAPACITY, priority, - cancelable.clone().as_ref(), + Some(&cancelable.clone()), move |result| match result { Ok(bytes) => { // No bytes were read, end of stream diff --git a/src/client/response/meta.rs b/src/client/response/meta.rs index 42bd4dd..26388ad 100644 --- a/src/client/response/meta.rs +++ b/src/client/response/meta.rs @@ -78,21 +78,15 @@ impl Meta { /// Asynchronously create new `Self` from [IOStream](https://docs.gtk.org/gio/class.IOStream.html) pub fn from_stream_async( stream: impl IsA, - priority: Option, - cancellable: Option, + priority: Priority, + cancellable: Cancellable, on_complete: impl FnOnce(Result) + 'static, ) { read_from_stream_async( Vec::with_capacity(MAX_LEN), stream, - match cancellable { - Some(value) => Some(value), - None => None::, - }, - match priority { - Some(value) => value, - None => Priority::DEFAULT, - }, + cancellable, + priority, |result| match result { Ok(buffer) => on_complete(Self::from_utf8(&buffer)), Err(e) => on_complete(Err(e)), @@ -110,14 +104,14 @@ impl Meta { pub fn read_from_stream_async( mut buffer: Vec, stream: impl IsA, - cancellable: Option, + cancellable: Cancellable, priority: Priority, on_complete: impl FnOnce(Result, Error>) + 'static, ) { stream.input_stream().read_async( vec![0], priority, - cancellable.clone().as_ref(), + Some(&cancellable.clone()), move |result| match result { Ok((mut bytes, size)) => { // Expect valid header length diff --git a/src/gio/memory_input_stream.rs b/src/gio/memory_input_stream.rs index fc175e0..dbb0bc3 100644 --- a/src/gio/memory_input_stream.rs +++ b/src/gio/memory_input_stream.rs @@ -15,7 +15,7 @@ use glib::{object::IsA, Bytes, Priority}; /// * calculate bytes processed on chunk load pub fn from_stream_async( base_io_stream: impl IsA, - cancelable: Option, + cancelable: Cancellable, priority: Priority, bytes_in_chunk: usize, bytes_total_limit: usize, @@ -38,7 +38,7 @@ pub fn from_stream_async( pub fn read_all_from_stream_async( memory_input_stream: MemoryInputStream, base_io_stream: impl IsA, - cancelable: Option, + cancelable: Cancellable, priority: Priority, bytes: (usize, usize, usize), callback: ( @@ -52,7 +52,7 @@ pub fn read_all_from_stream_async( base_io_stream.input_stream().read_bytes_async( bytes_in_chunk, priority, - cancelable.clone().as_ref(), + Some(&cancelable.clone()), move |result| match result { Ok(bytes) => { // Update bytes total