sestring/src/payload/emphasis.rs

50 lines
1.1 KiB
Rust

use crate::payload::{Decode, Encode, SeStringChunkKind};
use std::io::{Read, Seek};
use crate::Payload;
#[derive(Debug, Clone, PartialEq)]
pub struct EmphasisPayload(pub bool);
impl EmphasisPayload {
pub fn enable() -> Self {
Self(true)
}
pub fn disable() -> Self {
Self(false)
}
pub fn enabled(&self) -> bool {
self.0
}
}
impl From<bool> for EmphasisPayload {
fn from(enabled: bool) -> Self {
Self(enabled)
}
}
impl Decode for EmphasisPayload {
fn decode<R: Read + Seek>(reader: R, _chunk_len: usize) -> Result<Self, crate::Error> {
let enabled = Self::read_integer(reader)?;
Ok(Self(enabled == 1))
}
}
impl Encode for EmphasisPayload {
fn encode(&self) -> Vec<u8> {
use std::iter::once;
let enabled = Self::make_integer(if self.0 { 1 } else { 0 });
let chunk_len = enabled.len() + 1;
once(Payload::START_BYTE)
.chain(once(SeStringChunkKind::Emphasis.as_u8()))
.chain(once(chunk_len as u8))
.chain(enabled.into_iter())
.chain(once(Payload::END_BYTE))
.collect()
}
}