Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error handling for edcode encoding/decoding #24

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rimecraft_event::Event;
use rimecraft_primitives::{id, Id, SerDeUpdate};
use tracing::{trace_span, warn};

use crate::BoxedError;

/// Represents a type of component that can be attached
/// on [`Components`].
pub trait Attach {
Expand Down Expand Up @@ -119,20 +121,25 @@ pub enum ComponentsError {
}

impl Encode for Components {
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
type Error = BoxedError;

fn encode<B>(&self, buf: &mut B) -> Result<(), Self::Error>
where
B: bytes::BufMut,
{
let Component(event) = self.get::<Component<BytesPEvent>>(&NET_SEND_ID).unwrap();

let mut hashmap = HashMap::new();
event.invoker()(&mut hashmap)?;
hashmap.encode(buf)
hashmap.encode(buf)?;
Ok(())
}
}

impl rimecraft_edcode::Update for Components {
fn update<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
type Error = BoxedError;

fn update<B>(&mut self, buf: &mut B) -> Result<(), <Self as rimecraft_edcode::Update>::Error>
where
B: bytes::Buf,
{
Expand All @@ -143,7 +150,9 @@ impl rimecraft_edcode::Update for Components {
.unwrap();

let mut hashmap = HashMap::<Id, Bytes>::decode(buf)?;
event.invoker()(&mut hashmap)
event.invoker()(&mut hashmap)?;

Ok(())
}
}

Expand Down Expand Up @@ -269,8 +278,10 @@ impl<T> Encode for Component<T>
where
T: Encode,
{
type Error = <T as Encode>::Error;

#[inline]
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
fn encode<B>(&self, buf: &mut B) -> Result<(), Self::Error>
where
B: bytes::BufMut,
{
Expand All @@ -282,8 +293,10 @@ impl<T> rimecraft_edcode::Update for Component<T>
where
T: rimecraft_edcode::Update,
{
type Error = <T as rimecraft_edcode::Update>::Error;

#[inline]
fn update<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
fn update<B>(&mut self, buf: &mut B) -> Result<(), <Self as rimecraft_edcode::Update>::Error>
where
B: bytes::Buf,
{
Expand Down Expand Up @@ -329,13 +342,13 @@ static NET_RECV_ID: once_cell::sync::Lazy<Id> = once_cell::sync::Lazy::new(net_r
/// The `1` field is the component id which is used
/// to be registered into components.
#[derive(Debug)]
pub struct Synced<T>(pub T, pub Id)
where
T: Attach + rimecraft_edcode::Update + 'static;
pub struct Synced<T>(pub T, pub Id);

impl<T> Attach for Synced<T>
where
T: Attach + rimecraft_edcode::Update + 'static,
<T as rimecraft_edcode::Update>::Error: std::error::Error + Send + Sync + 'static,
<T as Encode>::Error: std::error::Error + Send + Sync + 'static,
{
fn on_attach(&mut self, components: &mut Components) {
self.0.on_attach(components);
Expand Down Expand Up @@ -375,7 +388,7 @@ where
let this = unsafe { &mut *ptr };
let mut bytes = map.remove(&this.1).unwrap();

this.0.update(&mut bytes)
this.0.update(&mut bytes).map_err(From::from)
})),
Err(err) => {
warn!("network receiving event not found: {err}");
Expand Down Expand Up @@ -410,8 +423,10 @@ impl<T> Encode for Synced<T>
where
T: Attach + rimecraft_edcode::Update + 'static,
{
type Error = <T as Encode>::Error;

#[inline]
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
fn encode<B>(&self, buf: &mut B) -> Result<(), Self::Error>
where
B: bytes::BufMut,
{
Expand All @@ -423,8 +438,10 @@ impl<T> rimecraft_edcode::Update for Synced<T>
where
T: Attach + rimecraft_edcode::Update + 'static,
{
type Error = <T as rimecraft_edcode::Update>::Error;

#[inline]
fn update<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
fn update<B>(&mut self, buf: &mut B) -> Result<(), <Self as rimecraft_edcode::Update>::Error>
where
B: bytes::Buf,
{
Expand Down Expand Up @@ -495,9 +512,7 @@ static NBT_READ_ID: once_cell::sync::Lazy<Id> = once_cell::sync::Lazy::new(nbt_r
/// The `1` field is the component id which is used
/// to be registered into components.
#[derive(Debug)]
pub struct Stored<T>(pub T, pub Id)
where
T: Attach + SerDeUpdate + 'static;
pub struct Stored<T>(pub T, pub Id);

impl<T> Attach for Stored<T>
where
Expand Down Expand Up @@ -571,8 +586,10 @@ impl<T> Encode for Stored<T>
where
T: Attach + SerDeUpdate + Encode + 'static,
{
type Error = <T as Encode>::Error;

#[inline]
fn encode<B>(&self, buf: &mut B) -> anyhow::Result<()>
fn encode<B>(&self, buf: &mut B) -> Result<(), Self::Error>
where
B: bytes::BufMut,
{
Expand All @@ -584,8 +601,10 @@ impl<T> rimecraft_edcode::Update for Stored<T>
where
T: Attach + SerDeUpdate + rimecraft_edcode::Update + 'static,
{
type Error = <T as rimecraft_edcode::Update>::Error;

#[inline]
fn update<B>(&mut self, buf: &mut B) -> anyhow::Result<()>
fn update<B>(&mut self, buf: &mut B) -> Result<(), <Self as rimecraft_edcode::Update>::Error>
where
B: bytes::Buf,
{
Expand Down
9 changes: 7 additions & 2 deletions core/src/net/packet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod c2s;
pub mod s2c;

use anyhow::Ok;
pub mod error;

use std::convert::Infallible;

use rimecraft_edcode::Encode;

use super::listener::*;
Expand Down Expand Up @@ -93,8 +96,10 @@ impl<T> Encode for Bundled<T>
where
T: Listener + ?Sized,
{
type Error = Infallible;

#[inline]
fn encode<B>(&self, _buf: &mut B) -> anyhow::Result<()>
fn encode<B>(&self, _buf: &mut B) -> Result<(), Self::Error>
where
B: bytes::BufMut,
{
Expand Down
Loading