Skip to content

Commit

Permalink
Merge pull request #295 from c410-f3r/misc
Browse files Browse the repository at this point in the history
Generalize errors
  • Loading branch information
c410-f3r authored Dec 26, 2024
2 parents d173d40 + 5a7bcae commit a3c91b9
Show file tree
Hide file tree
Showing 43 changed files with 230 additions and 258 deletions.
5 changes: 4 additions & 1 deletion wtx-instances/generic-examples/grpc-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use wtx_instances::grpc_bindings::wtx::{GenericRequest, GenericResponse};
async fn main() -> wtx::Result<()> {
let mut client = Client::new(ClientFramework::tokio(1).build(), QuickProtobuf);
let mut rrb = ReqResBuffer::empty();
rrb.uri.reset(format_args!("http://127.0.0.1:9000"))?;
rrb.uri.reset(|el| {
el.push_str("http://127.0.0.1:9000");
Ok(())
})?;
let res = client
.send_unary_req(
("wtx", "GenericService", "generic_method"),
Expand Down
2 changes: 1 addition & 1 deletion wtx-ui/src/schema_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) async fn schema_manager(sm: SchemaManager) -> wtx::Result<()> {
&Config::from_uri(&uri)?,
ExecutorBuffer::new(usize::MAX, &mut rng),
&mut rng,
TcpStream::connect(uri.hostname_with_implied_port()).await.map_err(wtx::Error::from)?,
TcpStream::connect(uri.hostname_with_implied_port()).await?,
)
.await?;
handle_commands(executor, &sm).await?;
Expand Down
10 changes: 2 additions & 8 deletions wtx/src/client_api_framework/network/transport/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ where
{
let response = self.pop_response()?;
pkgs_aux.byte_buffer.clear();
pkgs_aux
.byte_buffer
.extend_from_copyable_slice(response.as_ref().lease())
.map_err(Into::into)?;
pkgs_aux.byte_buffer.extend_from_copyable_slice(response.as_ref().lease())?;
Ok(0..pkgs_aux.byte_buffer.len())
}
}
Expand All @@ -127,10 +124,7 @@ where
P: Package<A, DRSR, TP>,
{
manage_before_sending_related(pkg, pkgs_aux, &mut *self).await?;
self
.requests
.push(Cow::Owned(FromBytes::from_bytes(&pkgs_aux.byte_buffer)?))
.map_err(Into::into)?;
self.requests.push(Cow::Owned(FromBytes::from_bytes(&pkgs_aux.byte_buffer)?))?;
pkgs_aux.byte_buffer.clear();
manage_after_sending_related(pkg, pkgs_aux).await?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait SendingRecievingTransport<DRSR>:
{
async {
self.send(pkg, pkgs_aux).await?;
Ok(self.recv(pkgs_aux).await?)
self.recv(pkgs_aux).await
}
}

Expand Down
5 changes: 1 addition & 4 deletions wtx/src/client_api_framework/network/transport/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ where
slice = slice.get(sent..).unwrap_or_default();
}
pkgs_aux.byte_buffer.clear();
pkgs_aux
.byte_buffer
.extend_from_iter((0..pkgs_aux.byte_buffer.capacity()).map(|_| 0))
.map_err(Into::into)?;
pkgs_aux.byte_buffer.extend_from_iter((0..pkgs_aux.byte_buffer.capacity()).map(|_| 0))?;
manage_after_sending_related(pkg, pkgs_aux).await?;
if everything_was_sent {
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
where
A: Api,
{
recv(self.read_frame().await?, pkgs_aux).await.map_err(Into::into)
Ok(recv(self.read_frame().await?, pkgs_aux).await?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion wtx/src/database/client/postgres/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ where
let range_begin = nb._antecedent_end_idx().wrapping_sub(begin);
let range_end = nb._current_end_idx().wrapping_sub(begin_data);
cb(&Record::parse(bytes, range_begin..range_end, stmt.clone(), vb, len)?)?;
rb.push(vb.len()).map_err(Into::into)?;
rb.push(vb.len())?;
}
MessageTy::ReadyForQuery => {
break;
Expand Down
2 changes: 1 addition & 1 deletion wtx/src/database/client/postgres/executor/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
let record_range = range.start.wrapping_add(7)..range.end;
Some((nb._buffer().get(record_range)?, len))
}) {
Record::parse(record_bytes, 0..record_bytes.len(), stmt, vb, len).map_err(From::from)
Ok(Record::parse(record_bytes, 0..record_bytes.len(), stmt, vb, len)?)
} else {
Err(E::from(PostgresError::NoRecord.into()))
}
Expand Down
2 changes: 1 addition & 1 deletion wtx/src/database/client/postgres/executor/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,6 @@ where

#[inline]
fn stmt_id_str(stmt_hash: u64) -> crate::Result<ArrayString<22>> {
Ok(ArrayString::try_from(format_args!("s{stmt_hash}"))?)
ArrayString::try_from(format_args!("s{stmt_hash}"))
}
}
42 changes: 19 additions & 23 deletions wtx/src/database/client/postgres/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,24 @@ where
RV: RecordValues<Postgres<E>>,
{
write(fbw, true, Some(b'B'), |local_fbw| {
local_fbw
._extend_from_slices_each_c(&[portal.as_bytes(), stmt_id_str.as_bytes()])
.map_err(Into::into)?;
local_fbw._extend_from_slices_each_c(&[portal.as_bytes(), stmt_id_str.as_bytes()])?;
let rv_len = rv.len();

write_iter(local_fbw, (0..rv_len).map(|_| 1i16), None, |elem, local_local_fbw| {
local_local_fbw._extend_from_slice(&elem.to_be_bytes())?;
local_local_fbw.extend_from_slice(&elem.to_be_bytes())?;
Ok(())
})?;

{
local_fbw
._extend_from_slice(&i16::try_from(rv_len).map_err(Into::into)?.to_be_bytes())
.map_err(Into::into)?;
local_fbw.extend_from_slice(&i16::try_from(rv_len).map_err(Into::into)?.to_be_bytes())?;
let mut aux = (0usize, 0);
let _ = rv.encode_values(
&mut aux,
&mut EncodeValue::new(local_fbw),
|(counter, start), local_ev| {
*counter = counter.wrapping_add(1);
*start = local_ev.fbw()._len();
let _rslt = local_ev.fbw()._extend_from_slice(&[0; 4]);
let _rslt = local_ev.fbw().extend_from_slice(&[0; 4]);
4
},
|(_, start), local_ev, is_null, elem_len| {
Expand All @@ -67,7 +63,7 @@ where
}

write_iter(local_fbw, &[1i16], None, |elem, local_local_fbw| {
local_local_fbw._extend_from_slice(&elem.to_be_bytes())?;
local_local_fbw.extend_from_slice(&elem.to_be_bytes())?;
Ok(())
})?;

Expand All @@ -91,7 +87,7 @@ pub(crate) fn describe(
#[inline]
pub(crate) fn encrypted_conn(fbw: &mut FilledBufferWriter<'_>) -> crate::Result<()> {
write(fbw, true, None, |local_fbw| {
local_fbw._extend_from_slice(&0b0000_0100_1101_0010_0001_0110_0010_1111i32.to_be_bytes())?;
local_fbw.extend_from_slice(&0b0000_0100_1101_0010_0001_0110_0010_1111i32.to_be_bytes())?;
Ok::<_, crate::Error>(())
})
}
Expand All @@ -104,7 +100,7 @@ pub(crate) fn execute(
) -> crate::Result<()> {
write(fbw, true, Some(b'E'), |local_fbw| {
local_fbw._extend_from_slice_c(portal.as_bytes())?;
local_fbw._extend_from_slice(&max_rows.to_be_bytes())?;
local_fbw.extend_from_slice(&max_rows.to_be_bytes())?;
Ok::<_, crate::Error>(())
})
}
Expand All @@ -115,7 +111,7 @@ pub(crate) fn initial_conn_msg(
fbw: &mut FilledBufferWriter<'_>,
) -> crate::Result<()> {
write(fbw, true, None, |local_fbw| {
local_fbw._extend_from_slice(&0b11_0000_0000_0000_0000i32.to_be_bytes())?;
local_fbw.extend_from_slice(&0b11_0000_0000_0000_0000i32.to_be_bytes())?;
local_fbw._extend_from_slices_each_c(&[b"user", config.user.as_bytes()])?;
local_fbw._extend_from_slices_each_c(&[b"database", config.db.as_bytes()])?;
if !config.app_name.is_empty() {
Expand Down Expand Up @@ -144,7 +140,7 @@ pub(crate) fn parse(
write(fbw, true, Some(b'P'), |local_fbw| {
local_fbw._extend_from_slices_each_c(&[name.as_bytes(), cmd.as_bytes()])?;
write_iter(local_fbw, iter, None, |ty, local_local_fbw| {
local_local_fbw._extend_from_slice(&ty.to_be_bytes())?;
local_local_fbw.extend_from_slice(&ty.to_be_bytes())?;
Ok(())
})
})
Expand All @@ -167,9 +163,9 @@ pub(crate) fn sasl_first(
write(fbw, true, Some(b'p'), |local_fbw| {
local_fbw._extend_from_slice_c(method_bytes)?;
write(local_fbw, false, None, |local_local_fbw| {
local_local_fbw._extend_from_slice(method_header)?;
local_local_fbw._extend_from_slice(b"n=,r=")?;
local_local_fbw._extend_from_slice(nonce)?;
local_local_fbw.extend_from_slice(method_header)?;
local_local_fbw.extend_from_slice(b"n=,r=")?;
local_local_fbw.extend_from_slice(nonce)?;
Ok::<_, crate::Error>(())
})
})?;
Expand All @@ -186,7 +182,7 @@ pub(crate) fn sasl_second(
tls_server_end_point: &[u8],
) -> crate::Result<()> {
write(fbw, true, Some(b'p'), |local_fbw| {
local_fbw._extend_from_slice(b"c=")?;
local_fbw.extend_from_slice(b"c=")?;
{
let n = STANDARD.encode_slice(method_header, local_fbw._remaining_bytes_mut())?;
local_fbw._shift_idx(n)?;
Expand Down Expand Up @@ -223,7 +219,7 @@ pub(crate) fn sasl_second(
}

{
local_fbw._extend_from_slice(b",p=")?;
local_fbw.extend_from_slice(b",p=")?;
let n = STANDARD.encode_slice(client_proof, local_fbw._remaining_bytes_mut())?;
local_fbw._shift_idx(n)?;
}
Expand All @@ -249,15 +245,15 @@ where
E: From<crate::Error>,
{
if let Some(elem) = prefix {
fbw._extend_from_byte(elem).map_err(Into::into)?;
fbw._extend_from_byte(elem)?;
}
let (len_before, start) = if include_len {
let len = fbw._len();
fbw._extend_from_slice(&[0; 4]).map_err(Into::into)?;
fbw.extend_from_slice(&[0; 4])?;
(len, len)
} else {
let start = fbw._len();
fbw._extend_from_slice(&[0; 4]).map_err(Into::into)?;
fbw.extend_from_slice(&[0; 4])?;
(fbw._len(), start)
};
cb(fbw)?;
Expand All @@ -283,10 +279,10 @@ where
E: From<crate::Error>,
{
if let Some(elem) = prefix {
fbw._extend_from_byte(elem).map_err(Into::into)?;
fbw._extend_from_byte(elem)?;
}
let len_before = fbw._len();
fbw._extend_from_slice(&[0; 2]).map_err(Into::into)?;
fbw.extend_from_slice(&[0; 2])?;
let mut counter: usize = 0;
for elem in iter.into_iter().take(u16::MAX.into()) {
cb(elem, fbw)?;
Expand Down
7 changes: 1 addition & 6 deletions wtx/src/database/client/postgres/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ mod array {
fn from_record(
record: &crate::database::client::postgres::record::Record<'_, E>,
) -> Result<Self, E> {
Ok(
from_utf8_basic(into_rslt(record.value(0))?.bytes())
.map_err(From::from)?
.try_into()
.map_err(From::from)?,
)
Ok(from_utf8_basic(into_rslt(record.value(0))?.bytes()).map_err(From::from)?.try_into()?)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions wtx/src/database/client/postgres/struct_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
#[inline]
pub fn new(ev: &'ev mut EncodeValue<'buffer, 'tmp>) -> Result<Self, E> {
let start = ev.fbw()._len();
ev.fbw()._extend_from_slice(&[0; 4]).map_err(Into::into)?;
ev.fbw().extend_from_slice(&[0; 4])?;
Ok(Self { ev, len: 0, phantom: PhantomData, start })
}

Expand All @@ -40,12 +40,12 @@ where
where
T: Encode<Postgres<E>>,
{
self.ev.fbw()._extend_from_slice(&u32::from(ty).to_be_bytes()).map_err(Into::into)?;
self.ev.fbw().extend_from_slice(&u32::from(ty).to_be_bytes())?;
if value.is_null() {
self.ev.fbw()._extend_from_slice(&(-1i32).to_be_bytes()).map_err(Into::into)?;
self.ev.fbw().extend_from_slice(&(-1i32).to_be_bytes())?;
} else {
let len_start = self.ev.fbw()._len();
self.ev.fbw()._extend_from_slice(&[0; 4]).map_err(Into::into)?;
self.ev.fbw().extend_from_slice(&[0; 4])?;
let elem_start = self.ev.fbw()._len();
value.encode(self.ev)?;
let len = self.ev.fbw()._len().wrapping_sub(elem_start).try_into().unwrap_or_default();
Expand Down
Loading

0 comments on commit a3c91b9

Please sign in to comment.