#![warn(missing_docs)]
pub mod gzip_packed;
#[cfg(feature = "json")]
pub mod json;
pub mod msg_container;
mod primitives;
use std::cell::Cell;
const MAX_DEPTH: u32 = 64;
pub(crate) const MAX_BYTES: usize = 1 << 24;
thread_local! {
static DEPTH: Cell<u32> = const { Cell::new(0) };
static OVERSIZED: Cell<Option<bool>> = const { Cell::new(None) };
}
pub(crate) fn checked_bytes<T: Serializable + ?Sized>(value: &T) -> Option<Vec<u8>> {
struct Restore(Option<bool>);
impl Drop for Restore {
fn drop(&mut self) {
OVERSIZED.set(self.0);
}
}
let restore = Restore(OVERSIZED.replace(Some(false)));
let bytes = value.to_bytes();
let oversized = OVERSIZED.get() == Some(true);
drop(restore);
(!oversized).then_some(bytes)
}
fn mark_oversized() -> bool {
OVERSIZED.with(|checking| checking.get().map(|_| checking.set(Some(true))).is_some())
}
pub(crate) fn nested<T>(f: impl FnOnce() -> Result<T, Error>) -> Result<T, Error> {
let depth = DEPTH.get();
if depth >= MAX_DEPTH {
return Err(Error::TooDeep);
}
DEPTH.set(depth + 1);
let r = f();
DEPTH.set(depth);
r
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
UnexpectedEof,
UnexpectedConstructor {
id: u32,
},
InvalidGzip,
TooDeep,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::UnexpectedEof => f.write_str("unexpected end of TL data"),
Error::UnexpectedConstructor { id } => write!(f, "unexpected TL constructor {id:#010x}"),
Error::InvalidGzip => f.write_str("invalid gzip_packed data"),
Error::TooDeep => f.write_str("TL data nests too deeply"),
}
}
}
impl std::error::Error for Error {}
pub trait Identifiable {
const CONSTRUCTOR_ID: u32;
}
pub trait Serializable {
fn serialize(&self, buf: &mut Vec<u8>);
fn to_bytes(&self) -> Vec<u8> {
let mut buf = Vec::with_capacity(128);
self.serialize(&mut buf);
buf
}
}
pub trait Deserializable: Sized {
fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>;
fn from_bytes(mut bytes: &[u8]) -> Result<Self, Error> {
Self::deserialize(&mut bytes)
}
}
pub trait RemoteCall: Serializable {
const NAME: &'static str;
type Return: Deserializable;
}