style(rust): Format all Rust code with rustfmt

Change-Id: Iab7e00cc26a4f9727d3ab98691ef379921a33052
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5240
Tested-by: BuildkiteCI
Reviewed-by: kanepyork <rikingcoding@gmail.com>
Reviewed-by: Profpatsch <mail@profpatsch.de>
Reviewed-by: grfn <grfn@gws.fyi>
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2022-02-07 18:49:59 +03:00 committed by tazjin
parent 3318982f81
commit 3d8ee62087
42 changed files with 1253 additions and 876 deletions

View file

@ -1,6 +1,6 @@
extern crate netencode;
use netencode::{U, T, Tag};
use netencode::{Tag, T, U};
pub enum Pretty {
Single {
@ -20,7 +20,7 @@ pub enum Pretty {
r#type: char,
length: String,
vals: Vec<Pretty>,
trailer: char
trailer: char,
},
}
@ -39,7 +39,7 @@ impl Pretty {
r#type: 't',
length: format!("{}:", s.len()),
val: s.to_string(),
trailer: ','
trailer: ',',
},
U::Binary(s) => Pretty::Single {
r#type: 'b',
@ -47,15 +47,18 @@ impl Pretty {
// For pretty printing we want the string to be visible obviously.
// Instead of not supporting binary, lets use lossy conversion.
val: String::from_utf8_lossy(s).into_owned(),
trailer: ','
trailer: ',',
},
U::Sum(Tag{tag, val}) => Self::pretty_tag(tag, Self::from_u(*val)),
U::Sum(Tag { tag, val }) => Self::pretty_tag(tag, Self::from_u(*val)),
U::Record(m) => Pretty::Multi {
r#type: '{',
// TODO: we are losing the size here, should we recompute it? Keep it?
length: String::from(""),
vals: m.into_iter().map(|(k, v)| Self::pretty_tag(k, Self::from_u(v))).collect(),
trailer: '}'
vals: m
.into_iter()
.map(|(k, v)| Self::pretty_tag(k, Self::from_u(v)))
.collect(),
trailer: '}',
},
U::List(l) => Pretty::Multi {
r#type: '[',
@ -68,13 +71,14 @@ impl Pretty {
}
fn scalar<D>(r#type: char, length: &str, d: D) -> Pretty
where D: std::fmt::Display
where
D: std::fmt::Display,
{
Pretty::Single {
r#type,
length: length.to_string(),
val: format!("{}", d),
trailer: ','
trailer: ',',
}
}
@ -89,43 +93,62 @@ impl Pretty {
}
pub fn print_multiline<W>(&self, mut w: &mut W) -> std::io::Result<()>
where W: std::io::Write
where
W: std::io::Write,
{
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<()>
where W: std::io::Write
where
W: std::io::Write,
{
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),
)};
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),
)
};
if is_newline {
write!(&mut w, "{}", i);
}
match p {
Pretty::Single {r#type, length, val, trailer} =>
write!(&mut w, "{} {}{}", r#type, val, trailer),
Pretty::Tag { r#type, length, key, inner, val } => {
Pretty::Single {
r#type,
length,
val,
trailer,
} => write!(&mut w, "{} {}{}", r#type, val, trailer),
Pretty::Tag {
r#type,
length,
key,
inner,
val,
} => {
write!(&mut w, "{} {} {}", r#type, key, inner)?;
Self::go::<W>(&mut w, val, depth, false)
},
}
// if the length is 0 or 1, we print on one line,
// only if theres more than one element we split the resulting value.
// we never break lines on arbitrary column sizes, since that is just silly.
Pretty::Multi {r#type, length, vals, trailer} => match vals.len() {
Pretty::Multi {
r#type,
length,
vals,
trailer,
} => match vals.len() {
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)
},
}
more => {
write!(&mut w, "\n{}{} \n", iandhalf, r#type)?;
for v in vals {