use std::cell::Cell;
use crate::cfg::{Cfg, Version};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum Direction {
Serialize,
Deserialize,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) struct Info {
pub(crate) direction: Direction,
pub(crate) allow_skip: bool,
#[allow(dead_code)]
pub(crate) depth_limit: usize,
#[allow(dead_code)]
pub(crate) version: Version,
#[allow(dead_code)]
pub(crate) header: bool,
}
thread_local! {
static ACTIVE: Cell<Option<Info>> = const { Cell::new(None) };
}
pub(crate) fn active() -> Option<Info> {
ACTIVE.get()
}
pub(crate) struct Guard(Option<Info>);
impl Guard {
pub(crate) fn new<const WITH_IDENTS: bool>(direction: Direction, cfg: &Cfg<WITH_IDENTS>) -> Self {
let info = Info {
direction,
allow_skip: cfg.allow_skip(),
depth_limit: cfg.depth_limit(),
version: cfg.version(),
header: cfg.header(),
};
Self(ACTIVE.replace(Some(info)))
}
}
impl Drop for Guard {
fn drop(&mut self) {
ACTIVE.set(self.0);
}
}