azalea_chat/
numbers.rs

1//! Contains a few ways to style numbers. At the time of writing, Minecraft only
2//! uses this for rendering scoreboard objectives.
3
4use std::io::{Cursor, Write};
5
6#[cfg(feature = "azalea-buf")]
7use azalea_buf::{AzaleaRead, AzaleaWrite};
8use azalea_registry::NumberFormatKind;
9use simdnbt::owned::Nbt;
10
11use crate::FormattedText;
12
13#[derive(Clone, Debug)]
14pub enum NumberFormat {
15    Blank,
16    Styled { style: Nbt },
17    Fixed { value: FormattedText },
18}
19
20#[cfg(feature = "azalea-buf")]
21impl AzaleaRead for NumberFormat {
22    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
23        let kind = NumberFormatKind::azalea_read(buf)?;
24        match kind {
25            NumberFormatKind::Blank => Ok(NumberFormat::Blank),
26            NumberFormatKind::Styled => Ok(NumberFormat::Styled {
27                style: simdnbt::owned::read(buf)?,
28            }),
29            NumberFormatKind::Fixed => Ok(NumberFormat::Fixed {
30                value: FormattedText::azalea_read(buf)?,
31            }),
32        }
33    }
34}
35
36#[cfg(feature = "azalea-buf")]
37impl AzaleaWrite for NumberFormat {
38    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
39        match self {
40            NumberFormat::Blank => NumberFormatKind::Blank.azalea_write(buf)?,
41            NumberFormat::Styled { style } => {
42                NumberFormatKind::Styled.azalea_write(buf)?;
43                style.azalea_write(buf)?;
44            }
45            NumberFormat::Fixed { value } => {
46                NumberFormatKind::Fixed.azalea_write(buf)?;
47                value.azalea_write(buf)?;
48            }
49        }
50        Ok(())
51    }
52}