Skip to content

Commit

Permalink
Merge pull request 'Migration to edcode2 part 2' (#47) from edcode-…
Browse files Browse the repository at this point in the history
…derive-mig into main

Reviewed-on: https://codeberg.org/DM-Earth/rimecraft/pulls/47
  • Loading branch information
JieningYu committed Aug 12, 2024
2 parents 7091347 + 31a628d commit 555c61a
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 230 deletions.
2 changes: 1 addition & 1 deletion crates/core/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct Registry<K, T> {
///
/// # Serialization and Deserialization
///
/// This type can be serialized and deserialized using `serde` and `edcode`.
/// This type can be serialized and deserialized using `serde` and `edcode2`.
/// (with `serde` feature and `edcode` feature respectively)
///
/// ## Serde
Expand Down
4 changes: 2 additions & 2 deletions crates/core/text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ rimecraft-global-cx = { path = "../global-cx" }
rgb = "0.8"
rimecraft-fmt = { path = "../../util/fmt" }
serde = { version = "1.0", features = ["derive"], optional = true }
rimecraft-edcode = { path = "../../util/edcode", optional = true }
rimecraft-edcode2 = { path = "../../util/edcode2", optional = true }

[features]
default = ["macros"]
serde = ["dep:serde"]
macros = []
edcode = ["dep:rimecraft-edcode"]
edcode = ["dep:rimecraft-edcode2"]

[lints]
workspace = true
38 changes: 0 additions & 38 deletions crates/util/edcode/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions crates/util/edcode/readme.md

This file was deleted.

135 changes: 0 additions & 135 deletions crates/util/edcode/src/lib.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "rimecraft-edcode-derive-tests"
name = "rimecraft-edcode2-derive-tests"
version = "0.1.0"
edition = "2021"
authors = ["C191239 <[email protected]>"]
description = "Tests for rimecraft-edcode-derive"
description = "Tests for rimecraft-edcode2-derive"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = ["encoding"]
Expand All @@ -12,7 +12,7 @@ categories = ["encoding"]
maintenance = { status = "passively-maintained" }

[dev-dependencies]
rimecraft-edcode = { path = "../edcode", features = ["derive"] }
rimecraft-edcode2 = { path = "../edcode2", features = ["derive"] }

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Tests for `rimecraft-edcode-derive` crate.
//! Tests for `rimecraft-edcode2-derive` crate.

#![allow(deprecated)]

#[cfg(test)]
mod tests {
use rimecraft_edcode::{bytes::BytesMut, Decode, Encode};
use rimecraft_edcode2::{Decode, Encode};

#[test]
#[allow(dead_code)]
Expand All @@ -18,8 +18,8 @@ mod tests {
Someone = 36,
}

let mut buf = BytesMut::new();
let mut buf: Vec<u8> = Vec::new();
assert!(Topics::Someone.encode(&mut buf).is_ok());
assert!(Topics::decode(buf).is_ok_and(|x| x == Topics::Someone));
assert!(Topics::decode(buf.as_ref()).is_ok_and(|x| x == Topics::Someone));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "rimecraft-edcode-derive"
name = "rimecraft-edcode2-derive"
version = "0.1.0"
edition = "2021"
authors = ["C191239 <[email protected]>"]
description = "Derive macros for rimecraft-edcode"
description = "Derive macros for rimecraft-edcode2"
repository = "https://github.com/rimecraft-rs/rimecraft/"
license = "AGPL-3.0-or-later"
categories = ["encoding"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Proc-macros for deriving `rimecraft_edcode` traits.
//! Proc-macros for deriving `rimecraft_edcode2` traits.
//!
//! __You shouldn't use this crate directly__, use `rimecraft_edcode` crate
//! __You shouldn't use this crate directly__, use `rimecraft_edcode2` crate
//! with `derive` feature flag instead.

use proc_macro::TokenStream;
Expand Down Expand Up @@ -111,7 +111,7 @@ fn parse_derive_enum(
Ok((ident, repr_type, enum_idents, enum_vals))
}

/// Derive `rimecraft_edcode::Encode` to objects.
/// Derive `rimecraft_edcode2::Encode` to objects.
///
/// # Enum
///
Expand All @@ -131,15 +131,12 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
Err(err) => return err,
};
let expanded = quote! {
impl ::rimecraft_edcode::Encode for #ident {
fn encode<B>(&self, mut buf: B) -> Result<(), std::io::Error>
where
B: ::rimecraft_edcode::bytes::BufMut,
{
impl<B: ::rimecraft_edcode2::BufMut> ::rimecraft_edcode2::Encode<B> for #ident {
fn encode(&self, mut buf: B) -> Result<(), ::rimecraft_edcode2::BoxedError<'static>> {
let x:#repr_type = match self {
#( Self::#enum_idents => #enum_vals, )*
};
::rimecraft_edcode::Encode::encode(&x, &mut buf)?;
::rimecraft_edcode2::Encode::encode(&x, &mut buf)?;
Ok(())
}
}
Expand All @@ -161,7 +158,7 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
}
}

/// Derive `rimecraft_edcode::Decode` to objects.
/// Derive `rimecraft_edcode2::Decode` to objects.
#[proc_macro_derive(Decode)]
pub fn derive_decode(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand All @@ -175,15 +172,12 @@ pub fn derive_decode(input: TokenStream) -> TokenStream {
}
};
let expanded = quote! {
impl ::rimecraft_edcode::Decode for #ident {
fn decode<B>(mut buf: B) -> Result<Self, std::io::Error>
where
B: ::rimecraft_edcode::bytes::Buf,
{
let x:#repr_type = ::rimecraft_edcode::Decode::decode(&mut buf)?;
impl<'de, B: ::rimecraft_edcode2::Buf> ::rimecraft_edcode2::Decode<'de, B> for #ident {
fn decode(mut buf: B) -> Result<Self, ::rimecraft_edcode2::BoxedError<'de>> {
let x:#repr_type = ::rimecraft_edcode2::Decode::decode(&mut buf)?;
let var = match x {
#( #enum_vals => Self::#enum_idents, )*
unknown => return Err(std::io::Error::other(format!("unknown variant {}", unknown))),
unknown => return Err(Box::new(std::io::Error::other(format!("unknown variant {}", unknown)))),
};
Ok(var)
}
Expand Down
3 changes: 3 additions & 0 deletions crates/util/edcode2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ maintenance = { status = "passively-maintained" }

[dependencies]
bytes = "1.6"
rimecraft-edcode2-derive = { path = "../edcode2-derive", optional = true }

[features]
default = []
derive = ["dep:rimecraft-edcode2-derive"]

[lints]
workspace = true
3 changes: 3 additions & 0 deletions crates/util/edcode2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub use codecs::Variable;

pub mod codecs;

#[cfg(feature = "derive")]
pub use rimecraft_edcode2_derive::{Decode, Encode};

/// A boxed error type.
pub type BoxedError<'a> = Box<dyn std::error::Error + Send + Sync + 'a>;

Expand Down
4 changes: 2 additions & 2 deletions crates/util/identifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ maintenance = { status = "passively-maintained" }

[dependencies]
serde = { version = "1.0", optional = true }
rimecraft-edcode = { path = "../edcode", optional = true }
rimecraft-edcode2 = { path = "../edcode2", optional = true }

[features]
default = ["vanilla"]
serde = ["dep:serde"]
edcode = ["dep:rimecraft-edcode"]
edcode = ["dep:rimecraft-edcode2"]
vanilla = []

[lints]
Expand Down
Loading

0 comments on commit 555c61a

Please sign in to comment.