Skip to content

Commit

Permalink
fix(format): fix formatting
Browse files Browse the repository at this point in the history
Remove incorrect `"` in bool formatting.
Detect unkeyed Links correctly.
Further compact value formatting.
  • Loading branch information
SebastianSpeitel committed Feb 15, 2024
1 parent b16969d commit 7be6f33
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 9 deletions.
81 changes: 81 additions & 0 deletions src/data/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,87 @@ pub trait DataExt: Data {
value
}

#[inline]
#[must_use]
fn as_number(&self) -> Option<usize> {
enum NumberBuilder {
NotFound,
Found(usize),
Invalid,
}

impl NumberBuilder {
fn try_set(&mut self, val: usize) {
match self {
Self::NotFound => *self = Self::Found(val),
Self::Found(before) if *before == val => {}
Self::Found(_) => *self = Self::Invalid,
Self::Invalid => {}
}
}
}

impl crate::value::ValueBuiler<'_> for NumberBuilder {
fn bool(&mut self, value: bool) {
self.try_set(value as usize);
}
fn bytes(&mut self, _value: Cow<'_, [u8]>) {
*self = Self::Invalid;
}
fn f32(&mut self, value: f32) {
self.try_set(value as usize);
}
fn f64(&mut self, value: f64) {
self.try_set(value as usize);
}
fn i128(&mut self, value: i128) {
self.try_set(value as usize);
}
fn i16(&mut self, value: i16) {
self.try_set(value as usize);
}
fn i32(&mut self, value: i32) {
self.try_set(value as usize);
}
fn i64(&mut self, value: i64) {
self.try_set(value as usize);
}
fn i8(&mut self, value: i8) {
self.try_set(value as usize);
}
fn u16(&mut self, value: u16) {
self.try_set(value as usize);
}
fn u32(&mut self, value: u32) {
self.try_set(value as usize);
}
fn u64(&mut self, value: u64) {
self.try_set(value as usize);
}
fn u8(&mut self, value: u8) {
self.try_set(value as usize);
}
fn u128(&mut self, value: u128) {
self.try_set(value as usize);
}
fn str(&mut self, value: Cow<'_, str>) {
if let Ok(val) = value.parse() {
self.try_set(val);
} else {
*self = Self::Invalid;
}
}
}

let mut num = NumberBuilder::NotFound;
self.provide_value(&mut num);

match num {
NumberBuilder::Found(val) => Some(val),
_ => None,
}
}

#[inline]
fn query<L: Links + Default>(&self, query: &Query) -> Result<L, LinkError> {
let mut links = L::default();
Expand Down
58 changes: 49 additions & 9 deletions src/data/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
#[derive(Default, Debug)]
pub struct FORMAT<const SERIAL: bool = false, const MAX_DEPTH: u16 = 6, const VERBOSITY: i8 = 0>;

pub type COMPACT<const MAX_DEPTH: u16 = 6> = FORMAT<true, MAX_DEPTH, -1>;
pub type COMPACT<const MAX_DEPTH: u16 = 6, const VERBOSITY: i8 = -1> =
FORMAT<true, MAX_DEPTH, VERBOSITY>;
pub type DEBUG = FORMAT<true, 6, 1>;

pub trait Format {
Expand All @@ -40,15 +41,30 @@ pub trait Format {
// Format prefix
Self::fmt_prefix(f, data)?;

if Self::verbosity() < 0 {
if Self::verbosity() <= -1 {
let values = crate::value::Value::from_data(data);
if let Some(val) = values.as_enum() {
let mut has_links = None::<BoxedData>;
let no_links = std::cell::OnceCell::<bool>::new();

let link_check = || {
let mut link_check = None::<BoxedData>;
// Ignore errors
let _ = data.provide_links(&mut has_links);
if has_links.is_none() {
let _ = data.provide_links(&mut link_check);
link_check.is_none()
};

if Self::verbosity() <= -2 {
if let Some(num) = super::DataExt::as_number(&values) {
if *no_links.get_or_init(link_check) {
f.write_fmt(format_args!("{{{:?}}}", num))?;
return Ok(());
}
}
}

if let Some(val) = values.as_enum() {
if *no_links.get_or_init(link_check) {
if let Some(v) = val {
f.write_fmt(format_args!("({v})"))?;
f.write_fmt(format_args!("{{{v}}}"))?;
} else {
f.write_str("")?;
}
Expand Down Expand Up @@ -229,7 +245,20 @@ struct StreamingLinks<'a, 'b, 'c, F: Format> {
}

impl<F: Format> Links for StreamingLinks<'_, '_, '_, F> {
#[inline]
fn push(&mut self, target: BoxedData, key: Option<BoxedData>) -> Result {
use crate::links::MaybeKeyed;
let link = LinkEntry::<_, F> {
link: MaybeKeyed::new(key, target),
state: self.state,
};
self.fmt_set.entry(&link);

CONTINUE
}

#[inline]
fn push_keyed(&mut self, target: BoxedData, key: BoxedData) -> Result {
let link = LinkEntry::<_, F> {
link: (key, target),
state: self.state,
Expand All @@ -238,14 +267,25 @@ impl<F: Format> Links for StreamingLinks<'_, '_, '_, F> {

CONTINUE
}

#[inline]
fn push_unkeyed(&mut self, target: BoxedData) -> Result {
let link = LinkEntry::<_, F> {
link: target,
state: self.state,
};
self.fmt_set.entry(&link);

CONTINUE
}
}

impl ValueBuiler<'_> for fmt::DebugSet<'_, '_> {
#[inline]
fn bool(&mut self, value: bool) {
match value {
true => self.entry(&"bool: true"),
false => self.entry(&"bool: false"),
true => self.entry(&format_args!("bool: true")),
false => self.entry(&format_args!("bool: false")),
};
}
#[inline]
Expand Down

0 comments on commit 7be6f33

Please sign in to comment.