return connection in result

This commit is contained in:
yggverse 2025-02-03 01:08:31 +02:00
parent 8df7af44b5
commit 788b792167
3 changed files with 42 additions and 36 deletions

View file

@ -60,7 +60,7 @@ impl Client {
priority: Priority, priority: Priority,
cancellable: Cancellable, cancellable: Cancellable,
certificate: Option<TlsCertificate>, certificate: Option<TlsCertificate>,
callback: impl FnOnce(Result<Response, Error>) + 'static, callback: impl FnOnce(Result<(Response, Connection), Error>) + 'static,
) { ) {
// Begin new connection // Begin new connection
// * [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) required for valid // * [NetworkAddress](https://docs.gtk.org/gio/class.NetworkAddress.html) required for valid

View file

@ -58,7 +58,7 @@ impl Connection {
request: Request, request: Request,
priority: Priority, priority: Priority,
cancellable: Cancellable, cancellable: Cancellable,
callback: impl FnOnce(Result<Response, Error>) + 'static, callback: impl FnOnce(Result<(Response, Self), Error>) + 'static,
) { ) {
let output_stream = self.stream().output_stream(); let output_stream = self.stream().output_stream();
output_stream.clone().write_async( output_stream.clone().write_async(
@ -67,14 +67,17 @@ impl Connection {
Some(&cancellable.clone()), Some(&cancellable.clone()),
move |result| match result { move |result| match result {
Ok(_) => match request { Ok(_) => match request {
Request::Gemini { .. } => { Request::Gemini { .. } => Response::from_connection_async(
Response::from_connection_async(self, priority, cancellable, |result| { self,
priority,
cancellable,
|result, connection| {
callback(match result { callback(match result {
Ok(response) => Ok(response), Ok(response) => Ok((response, connection)),
Err(e) => Err(Error::Response(e)), Err(e) => Err(Error::Response(e)),
}) })
}) },
} ),
Request::Titan { data, .. } => output_stream.write_bytes_async( Request::Titan { data, .. } => output_stream.write_bytes_async(
&data, &data,
priority, priority,
@ -84,9 +87,9 @@ impl Connection {
self, self,
priority, priority,
cancellable, cancellable,
|result| { |result, connection| {
callback(match result { callback(match result {
Ok(response) => Ok(response), Ok(response) => Ok((response, connection)),
Err(e) => Err(Error::Response(e)), Err(e) => Err(Error::Response(e)),
}) })
}, },

View file

@ -36,7 +36,7 @@ impl Response {
connection: Connection, connection: Connection,
priority: Priority, priority: Priority,
cancellable: Cancellable, cancellable: Cancellable,
callback: impl FnOnce(Result<Self, Error>) + 'static, callback: impl FnOnce(Result<Self, Error>, Connection) + 'static,
) { ) {
from_stream_async( from_stream_async(
Vec::with_capacity(HEADER_LEN), Vec::with_capacity(HEADER_LEN),
@ -44,7 +44,8 @@ impl Response {
cancellable, cancellable,
priority, priority,
|result| { |result| {
callback(match result { callback(
match result {
Ok(buffer) => match buffer.first() { Ok(buffer) => match buffer.first() {
Some(byte) => match byte { Some(byte) => match byte {
1 => match Input::from_utf8(&buffer) { 1 => match Input::from_utf8(&buffer) {
@ -72,7 +73,9 @@ impl Response {
None => Err(Error::Protocol), None => Err(Error::Protocol),
}, },
Err(e) => Err(e), Err(e) => Err(e),
}) },
connection,
)
}, },
); );
} }