Skip to content

Commit

Permalink
Merge pull request 'edcode support for Text' (#55) from text-edcode…
Browse files Browse the repository at this point in the history
… into main

Reviewed-on: https://codeberg.org/DM-Earth/rimecraft/pulls/55
Reviewed-by: C191239 <[email protected]>
  • Loading branch information
C191239 committed Aug 25, 2024
2 parents eb8f5c9 + 83ea636 commit cc8eabd
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 72 deletions.
4 changes: 2 additions & 2 deletions crates/client/option/src/enums/attack_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, fmt::Display};

use enum_iterator::Sequence;
use rimecraft_text::{format_localization_key, Localizable};
use rimecraft_text::{format_localization_key, Localize};

use super::ByUSizeId;

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Display for AttackIndicator {
}
}

impl Localizable for AttackIndicator {
impl Localize for AttackIndicator {
fn localization_key(&self) -> Cow<'_, str> {
Cow::Owned(format_localization_key!(
"options",
Expand Down
4 changes: 2 additions & 2 deletions crates/client/option/src/enums/graphics_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, fmt::Display};

use enum_iterator::Sequence;
use rimecraft_text::{format_localization_key, Localizable};
use rimecraft_text::{format_localization_key, Localize};

use super::ByUSizeId;

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Display for GraphicsMode {
}
}

impl Localizable for GraphicsMode {
impl Localize for GraphicsMode {
fn localization_key(&self) -> Cow<'_, str> {
Cow::Owned(format_localization_key!(
"options",
Expand Down
4 changes: 2 additions & 2 deletions crates/client/option/src/enums/narrator_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, fmt::Display};

use enum_iterator::Sequence;
use rimecraft_text::{format_localization_key, Localizable};
use rimecraft_text::{format_localization_key, Localize};

use super::ByUSizeId;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl NarratorMode {
}
}

impl Localizable for NarratorMode {
impl Localize for NarratorMode {
fn localization_key(&self) -> Cow<'_, str> {
Cow::Owned(format_localization_key!(
"options",
Expand Down
4 changes: 2 additions & 2 deletions crates/client/option/src/enums/particles_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{borrow::Cow, fmt::Display};

use enum_iterator::Sequence;
use rimecraft_text::{format_localization_key, Localizable};
use rimecraft_text::{format_localization_key, Localize};

use super::ByUSizeId;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl Display for ParticlesMode {
}
}

impl Localizable for ParticlesMode {
impl Localize for ParticlesMode {
fn localization_key(&self) -> Cow<'_, str> {
Cow::Owned(format_localization_key!(
"options",
Expand Down
2 changes: 1 addition & 1 deletion crates/core/component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{any::TypeId, cell::UnsafeCell, fmt::Debug, hash::Hash, marker::Phantom
use bytes::{Buf, BufMut};
use edcode2::{Decode, Encode};
use rimecraft_global_cx::{
nbt_edcode::{ReadNbt, UpdateNbt, WriteNbt},
nbt::{ReadNbt, UpdateNbt, WriteNbt},
ProvideIdTy,
};
use rimecraft_registry::{ProvideRegistry, Reg};
Expand Down
2 changes: 2 additions & 0 deletions crates/core/global-cx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ maintenance = { status = "passively-maintained" }

[dependencies]
serde = { version = "1.0", default-features = false, optional = true }
edcode2 = { path = "../../util/edcode2", package = "rimecraft-edcode2", optional = true }

[features]
default = ["std"]
std = []
serde = ["dep:serde"]
nbt = ["serde"]
edcode = ["dep:edcode2", "std"]

[lints]
workspace = true
56 changes: 56 additions & 0 deletions crates/core/global-cx/src/edcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! `edcode2` crate integration.

use core::marker::PhantomData;

use edcode2::{Buf, BufMut, Decode, Encode};

use crate::nbt::{ReadNbt, WriteNbt};

/// A type that wraps a value with a context.
#[derive(Debug)]
pub struct Nbt<T, Cx>(pub T, PhantomData<Cx>);

impl<T, Cx> Nbt<T, Cx> {
/// Creates a new `Nbt` with the given value.
#[inline]
pub const fn new(value: T) -> Self {
Self(value, PhantomData)
}

/// Consumes the `Nbt` and returns the inner value.
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}

impl<T, Cx> From<T> for Nbt<T, Cx> {
#[inline]
fn from(value: T) -> Self {
Self::new(value)
}
}

impl<B, T, Cx> Encode<B> for Nbt<T, Cx>
where
B: BufMut,
Cx: for<'s> WriteNbt<&'s T>,
{
#[inline]
fn encode(&self, buf: B) -> Result<(), edcode2::BoxedError<'static>> {
Cx::write_nbt(&self.0, buf.writer()).map_err(Into::into)
}
}

impl<'de, B, T, Cx> Decode<'de, B> for Nbt<T, Cx>
where
B: Buf,
Cx: ReadNbt<T>,
{
#[inline]
fn decode(buf: B) -> Result<Self, edcode2::BoxedError<'de>> {
Cx::read_nbt(buf.reader())
.map(Self::new)
.map_err(Into::into)
}
}
67 changes: 9 additions & 58 deletions crates/core/global-cx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ extern crate std;

use core::{fmt::Display, hash::Hash};

#[cfg(feature = "std")]
pub mod nbt;

#[deprecated = "use `nbt` feature instead"]
pub use nbt as nbt_edcode;

#[cfg(feature = "edcode")]
pub mod edcode;

/// Marker trait for global contexts.
///
/// # Safety
Expand Down Expand Up @@ -51,61 +60,3 @@ pub trait ProvideNbtTy: GlobalContext {
/// Function that converts a `Compound` to a `Deserializer`.
fn compound_to_deserializer(compound: &Self::Compound) -> impl serde::Deserializer<'_>;
}

/// NBT `edcode`-ing related marker traits.
#[cfg(feature = "std")]
pub mod nbt_edcode {
use std::io;

use crate::GlobalContext;

/// Marker trait for global contexts that can write nbt tags to a [`io::Write`] object.
pub trait WriteNbt<T>: GlobalContext {
/// Function that performs writing operation.
///
/// # Errors
///
/// I/O errors.
fn write_nbt<W>(value: T, writer: W) -> Result<(), io::Error>
where
W: io::Write;
}

/// Marker trait for global contexts that can read nbt tags from a [`io::Read`] object.
pub trait ReadNbt<T>: GlobalContext {
/// Function that performs reading operation.
///
/// # Errors
///
/// I/O errors.
fn read_nbt<R>(reader: R) -> Result<T, io::Error>
where
R: io::Read;
}

/// Marker trait for global contexts that can update existing nbt tags from a [`io::Read`] object.
pub trait UpdateNbt<T: ?Sized>: GlobalContext {
/// Function that performs updating operation.
///
/// # Errors
///
/// I/O errors.
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error>
where
R: io::Read;
}

impl<T, Cx> UpdateNbt<T> for Cx
where
Cx: ReadNbt<T>,
{
#[inline]
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error>
where
R: io::Read,
{
*value = Self::read_nbt(reader)?;
Ok(())
}
}
}
55 changes: 55 additions & 0 deletions crates/core/global-cx/src/nbt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! NBT `edcode`-ing related marker traits.

use std::io;

use crate::GlobalContext;

/// Marker trait for global contexts that can write nbt tags to a [`io::Write`] object.
pub trait WriteNbt<T>: GlobalContext {
/// Function that performs writing operation.
///
/// # Errors
///
/// I/O errors.
fn write_nbt<W>(value: T, writer: W) -> Result<(), io::Error>
where
W: io::Write;
}

/// Marker trait for global contexts that can read nbt tags from a [`io::Read`] object.
pub trait ReadNbt<T>: GlobalContext {
/// Function that performs reading operation.
///
/// # Errors
///
/// I/O errors.
fn read_nbt<R>(reader: R) -> Result<T, io::Error>
where
R: io::Read;
}

/// Marker trait for global contexts that can update existing nbt tags from a [`io::Read`] object.
pub trait UpdateNbt<T: ?Sized>: GlobalContext {
/// Function that performs updating operation.
///
/// # Errors
///
/// I/O errors.
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error>
where
R: io::Read;
}

impl<T, Cx> UpdateNbt<T> for Cx
where
Cx: ReadNbt<T>,
{
#[inline]
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error>
where
R: io::Read,
{
*value = Self::read_nbt(reader)?;
Ok(())
}
}
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-edcode2 = { path = "../../util/edcode2", optional = true }
edcode2 = { path = "../../util/edcode2", optional = true, package = "rimecraft-edcode2" }

[features]
default = ["macros"]
serde = ["dep:serde"]
macros = []
edcode = ["dep:rimecraft-edcode2"]
edcode = ["dep:edcode2", "serde", "rimecraft-global-cx/edcode"]

[lints]
workspace = true
11 changes: 8 additions & 3 deletions crates/core/text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ where

/// Global context for text.
///
/// The associated types [`Texts::T`] and [`Texts::StyleExt`] should be applied to [`Text`] when used.
/// The associated type `Content` and `StyleExt` should be applied to [`Text`] when used.
pub trait ProvideTextTy: GlobalContext {
/// Generic `T` that should be applied to [`Text`].
type Content;
type Content: Plain;

/// Generic `StyleExt` that should be applied to [`Text`].
type StyleExt;
}
Expand All @@ -194,11 +195,15 @@ pub trait ProvideTextTy: GlobalContext {
pub type Text<Cx> = RawText<<Cx as ProvideTextTy>::Content, <Cx as ProvideTextTy>::StyleExt>;

/// A localizable value.
pub trait Localizable {
pub trait Localize {
/// Returns the localization key of this value.
fn localization_key(&self) -> Cow<'_, str>;
}

/// A seed for encoding and decoding [`Text`] through `edcode2` crate.
#[cfg(feature = "edcode")]
pub type EdcodeSeed<Cx> = rimecraft_global_cx::edcode::Nbt<Text<Cx>, Cx>;

#[cfg(test)]
mod tests;

Expand Down

0 comments on commit cc8eabd

Please sign in to comment.