2021-02-14 16:53:16 +01:00
|
|
|
|
extern crate netencode;
|
|
|
|
|
|
2022-02-07 16:49:59 +01:00
|
|
|
|
use netencode::{Tag, T, U};
|
2021-02-14 16:53:16 +01:00
|
|
|
|
|
|
|
|
|
pub enum Pretty {
|
|
|
|
|
Single {
|
|
|
|
|
r#type: char,
|
|
|
|
|
length: String,
|
|
|
|
|
val: String,
|
|
|
|
|
trailer: char,
|
|
|
|
|
},
|
|
|
|
|
Tag {
|
|
|
|
|
r#type: char,
|
|
|
|
|
length: String,
|
|
|
|
|
key: String,
|
|
|
|
|
inner: char,
|
|
|
|
|
val: Box<Pretty>,
|
|
|
|
|
},
|
|
|
|
|
Multi {
|
|
|
|
|
r#type: char,
|
|
|
|
|
length: String,
|
|
|
|
|
vals: Vec<Pretty>,
|
2022-02-07 16:49:59 +01:00
|
|
|
|
trailer: char,
|
2021-02-14 16:53:16 +01:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Pretty {
|
|
|
|
|
pub fn from_u<'a>(u: U<'a>) -> Pretty {
|
|
|
|
|
match u {
|
|
|
|
|
U::Unit => Self::scalar('u', "", ""),
|
|
|
|
|
U::N1(b) => Self::scalar('n', "1:", if b { "1" } else { "0" }),
|
|
|
|
|
U::N3(n) => Self::scalar('n', "3:", n),
|
|
|
|
|
U::N6(n) => Self::scalar('n', "6:", n),
|
|
|
|
|
U::N7(n) => Self::scalar('n', "7:", n),
|
|
|
|
|
U::I3(i) => Self::scalar('i', "3:", i),
|
|
|
|
|
U::I6(i) => Self::scalar('i', "6:", i),
|
|
|
|
|
U::I7(i) => Self::scalar('i', "7:", i),
|
|
|
|
|
U::Text(s) => Pretty::Single {
|
|
|
|
|
r#type: 't',
|
|
|
|
|
length: format!("{}:", s.len()),
|
|
|
|
|
val: s.to_string(),
|
2022-02-07 16:49:59 +01:00
|
|
|
|
trailer: ',',
|
2021-02-14 16:53:16 +01:00
|
|
|
|
},
|
|
|
|
|
U::Binary(s) => Pretty::Single {
|
|
|
|
|
r#type: 'b',
|
|
|
|
|
length: format!("{}:", s.len()),
|
|
|
|
|
// For pretty printing we want the string to be visible obviously.
|
|
|
|
|
// Instead of not supporting binary, let’s use lossy conversion.
|
|
|
|
|
val: String::from_utf8_lossy(s).into_owned(),
|
2022-02-07 16:49:59 +01:00
|
|
|
|
trailer: ',',
|
2021-02-14 16:53:16 +01:00
|
|
|
|
},
|
2022-02-07 16:49:59 +01:00
|
|
|
|
U::Sum(Tag { tag, val }) => Self::pretty_tag(tag, Self::from_u(*val)),
|
2021-02-14 16:53:16 +01:00
|
|
|
|
U::Record(m) => Pretty::Multi {
|
|
|
|
|
r#type: '{',
|
|
|
|
|
// TODO: we are losing the size here, should we recompute it? Keep it?
|
|
|
|
|
length: String::from(""),
|
2022-02-07 16:49:59 +01:00
|
|
|
|
vals: m
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(k, v)| Self::pretty_tag(k, Self::from_u(v)))
|
|
|
|
|
.collect(),
|
|
|
|
|
trailer: '}',
|
2021-02-14 16:53:16 +01:00
|
|
|
|
},
|
|
|
|
|
U::List(l) => Pretty::Multi {
|
|
|
|
|
r#type: '[',
|
|
|
|
|
// TODO: we are losing the size here, should we recompute it? Keep it?
|
|
|
|
|
length: String::from(""),
|
|
|
|
|
vals: l.into_iter().map(|v| Self::from_u(v)).collect(),
|
|
|
|
|
trailer: ']',
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn scalar<D>(r#type: char, length: &str, d: D) -> Pretty
|
2022-02-07 16:49:59 +01:00
|
|
|
|
where
|
|
|
|
|
D: std::fmt::Display,
|
2021-02-14 16:53:16 +01:00
|
|
|
|
{
|
|
|
|
|
Pretty::Single {
|
|
|
|
|
r#type,
|
|
|
|
|
length: length.to_string(),
|
|
|
|
|
val: format!("{}", d),
|
2022-02-07 16:49:59 +01:00
|
|
|
|
trailer: ',',
|
2021-02-14 16:53:16 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn pretty_tag(tag: &str, val: Pretty) -> Pretty {
|
|
|
|
|
Pretty::Tag {
|
|
|
|
|
r#type: '<',
|
|
|
|
|
length: format!("{}:", tag.len()),
|
|
|
|
|
key: tag.to_string(),
|
|
|
|
|
inner: '|',
|
|
|
|
|
val: Box::new(val),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn print_multiline<W>(&self, mut w: &mut W) -> std::io::Result<()>
|
2022-02-07 16:49:59 +01:00
|
|
|
|
where
|
|
|
|
|
W: std::io::Write,
|
2021-02-14 16:53:16 +01:00
|
|
|
|
{
|
|
|
|
|
Self::go(&mut w, self, 0, true);
|
|
|
|
|
write!(w, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn go<W>(mut w: &mut W, p: &Pretty, depth: usize, is_newline: bool) -> std::io::Result<()>
|
2022-02-07 16:49:59 +01:00
|
|
|
|
where
|
|
|
|
|
W: std::io::Write,
|
2021-02-14 16:53:16 +01:00
|
|
|
|
{
|
2022-02-07 16:49:59 +01:00
|
|
|
|
const full: usize = 4;
|
|
|
|
|
const half: usize = 2;
|
|
|
|
|
let i = &vec![b' '; depth * full];
|
|
|
|
|
let iandhalf = &vec![b' '; depth * full + half];
|
|
|
|
|
let (i, iandhalf) = unsafe {
|
|
|
|
|
(
|
|
|
|
|
std::str::from_utf8_unchecked(i),
|
|
|
|
|
std::str::from_utf8_unchecked(iandhalf),
|
|
|
|
|
)
|
|
|
|
|
};
|
2021-02-14 16:53:16 +01:00
|
|
|
|
if is_newline {
|
|
|
|
|
write!(&mut w, "{}", i);
|
|
|
|
|
}
|
|
|
|
|
match p {
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Pretty::Single {
|
|
|
|
|
r#type,
|
|
|
|
|
length,
|
|
|
|
|
val,
|
|
|
|
|
trailer,
|
|
|
|
|
} => write!(&mut w, "{} {}{}", r#type, val, trailer),
|
|
|
|
|
Pretty::Tag {
|
|
|
|
|
r#type,
|
|
|
|
|
length,
|
|
|
|
|
key,
|
|
|
|
|
inner,
|
|
|
|
|
val,
|
|
|
|
|
} => {
|
2021-02-14 16:53:16 +01:00
|
|
|
|
write!(&mut w, "{} {} {}", r#type, key, inner)?;
|
|
|
|
|
Self::go::<W>(&mut w, val, depth, false)
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
2021-02-14 16:53:16 +01:00
|
|
|
|
// if the length is 0 or 1, we print on one line,
|
|
|
|
|
// only if there’s more than one element we split the resulting value.
|
|
|
|
|
// we never break lines on arbitrary column sizes, since that is just silly.
|
2022-02-07 16:49:59 +01:00
|
|
|
|
Pretty::Multi {
|
|
|
|
|
r#type,
|
|
|
|
|
length,
|
|
|
|
|
vals,
|
|
|
|
|
trailer,
|
|
|
|
|
} => match vals.len() {
|
2021-02-14 16:53:16 +01:00
|
|
|
|
0 => write!(&mut w, "{} {}", r#type, trailer),
|
|
|
|
|
1 => {
|
|
|
|
|
write!(&mut w, "{} ", r#type);
|
|
|
|
|
Self::go::<W>(&mut w, &vals[0], depth, false)?;
|
|
|
|
|
write!(&mut w, "{}", trailer)
|
2022-02-07 16:49:59 +01:00
|
|
|
|
}
|
2021-02-14 16:53:16 +01:00
|
|
|
|
more => {
|
|
|
|
|
write!(&mut w, "\n{}{} \n", iandhalf, r#type)?;
|
|
|
|
|
for v in vals {
|
|
|
|
|
Self::go::<W>(&mut w, v, depth + 1, true)?;
|
|
|
|
|
write!(&mut w, "\n")?;
|
|
|
|
|
}
|
|
|
|
|
write!(&mut w, "{}{}", iandhalf, trailer)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|