Cargo fmt

This commit is contained in:
Alex 2022-12-15 15:38:10 +01:00
parent 52f6bd177c
commit 1579030b44
Signed by: lx
GPG key ID: 0E496D15096376BE
3 changed files with 60 additions and 62 deletions

View file

@ -378,8 +378,10 @@ impl<'a, 'b> Term<'a, 'b> {
/// ```
pub fn list(&self) -> Result<Vec<Term<'a, '_>>, TypeError> {
match self.0.mkref() {
AnyTerm::ListRef(_r, l) => Ok(l.iter().map(|x| Term(x.mkref().into())).collect::<Vec<_>>()),
_ => Err(TypeError::WrongType("LIST")),
AnyTerm::ListRef(_r, l) => {
Ok(l.iter().map(|x| Term(x.mkref().into())).collect::<Vec<_>>())
}
_ => Err(TypeError::WrongType("LIST")),
}
}

View file

@ -281,9 +281,9 @@ impl<'a> T<'a> {
buf.extend_from_slice(b"{}");
} else if d.len() == 1 {
let (k, v) = d.into_iter().next().unwrap();
buf.extend_from_slice(b"{ ");
buf.extend_from_slice(k.borrow());
buf.extend_from_slice(b" = ");
buf.extend_from_slice(b"{ ");
buf.extend_from_slice(k.borrow());
buf.extend_from_slice(b" = ");
v.encode_aux(buf, indent + 2, false);
buf.extend_from_slice(b" }");
} else {
@ -307,29 +307,32 @@ impl<'a> T<'a> {
buf.push(b'}');
}
}
T::List(l) => {
if l.len() == 0 {
buf.extend_from_slice(b"[]");
} else if l.len() == 1 {
buf.extend_from_slice(b"[ ");
l.into_iter().next().unwrap().encode_aux(buf, indent + 2, false);
buf.extend_from_slice(b" ]");
} else {
let indent2 = indent + 2;
buf.extend_from_slice(b"[\n");
for item in l {
for _ in 0..indent2 {
buf.push(b' ');
}
item.encode_aux(buf, indent2, false);
buf.extend_from_slice(b",\n");
}
for _ in 0..indent {
buf.push(b' ');
}
buf.push(b']');
}
}
T::List(l) => {
if l.len() == 0 {
buf.extend_from_slice(b"[]");
} else if l.len() == 1 {
buf.extend_from_slice(b"[ ");
l.into_iter()
.next()
.unwrap()
.encode_aux(buf, indent + 2, false);
buf.extend_from_slice(b" ]");
} else {
let indent2 = indent + 2;
buf.extend_from_slice(b"[\n");
for item in l {
for _ in 0..indent2 {
buf.push(b' ');
}
item.encode_aux(buf, indent2, false);
buf.extend_from_slice(b",\n");
}
for _ in 0..indent {
buf.push(b' ');
}
buf.push(b']');
}
}
T::Seq(l) => {
let indent2 = indent + 2;
for (i, v) in l.into_iter().enumerate() {
@ -352,30 +355,30 @@ impl<'a> T<'a> {
T::Str(s) => buf.extend_from_slice(s),
T::OwnedStr(s) => buf.extend_from_slice(&s),
T::Dict(mut d) => {
buf.push(b'{');
let mut keys = d.keys().cloned().collect::<Vec<_>>();
keys.sort();
for (i, k) in keys.into_iter().enumerate() {
if i > 0 {
buf.push(b',');
}
let v = d.remove(&k).unwrap();
buf.extend_from_slice(k.borrow());
buf.push(b'=');
v.encode_concise_aux(buf);
}
buf.push(b'}');
}
T::List(l) => {
buf.push(b'[');
for (i,item) in l.into_iter().enumerate() {
buf.push(b'{');
let mut keys = d.keys().cloned().collect::<Vec<_>>();
keys.sort();
for (i, k) in keys.into_iter().enumerate() {
if i > 0 {
buf.push(b',');
}
item.encode_concise_aux(buf);
}
buf.push(b']');
}
let v = d.remove(&k).unwrap();
buf.extend_from_slice(k.borrow());
buf.push(b'=');
v.encode_concise_aux(buf);
}
buf.push(b'}');
}
T::List(l) => {
buf.push(b'[');
for (i, item) in l.into_iter().enumerate() {
if i > 0 {
buf.push(b',');
}
item.encode_concise_aux(buf);
}
buf.push(b']');
}
T::Seq(l) => {
for (i, v) in l.into_iter().enumerate() {
if i > 0 {
@ -390,18 +393,15 @@ impl<'a> T<'a> {
#[cfg(test)]
mod tests {
use crate::debug;
use super::*;
use crate::debug;
#[test]
fn complex1() {
let input = seq([
string("HELLO").unwrap(),
string("alexhelloworld").unwrap(),
list([
string("dude").unwrap(),
string("why").unwrap(),
]),
list([string("dude").unwrap(), string("why").unwrap()]),
dict([
("from", string("jxx").unwrap()),
("subject", string("hello").unwrap()),
@ -420,17 +420,14 @@ mod tests {
subject = hello,
}";
assert_eq!(debug(&input.encode()), expected);
}
}
#[test]
fn complex1_concise() {
let input = seq([
string("HELLO").unwrap(),
string("alexhelloworld").unwrap(),
list([
string("dude").unwrap(),
string("why").unwrap(),
]),
list([string("dude").unwrap(), string("why").unwrap()]),
dict([
("from", string("jxx").unwrap()),
("subject", string("hello").unwrap()),

View file

@ -11,9 +11,9 @@ pub use ser::{to_bytes, to_term, Serializer};
#[cfg(test)]
mod tests {
use super::*;
use crate::debug;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::debug;
fn test_bidir<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(
input: T,
@ -96,8 +96,7 @@ mod tests {
E::Struct { a: 1 },
E::Tuple(3, 2),
];
let expected =
r#"[E.Unit,E.Unit,E.Newtype 1,E.Tuple 1 2,E.Struct {a=1},E.Tuple 3 2]"#;
let expected = r#"[E.Unit,E.Unit,E.Newtype 1,E.Tuple 1 2,E.Struct {a=1},E.Tuple 3 2]"#;
test_bidir(input, expected);
}