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> { pub fn list(&self) -> Result<Vec<Term<'a, '_>>, TypeError> {
match self.0.mkref() { match self.0.mkref() {
AnyTerm::ListRef(_r, l) => Ok(l.iter().map(|x| Term(x.mkref().into())).collect::<Vec<_>>()), AnyTerm::ListRef(_r, l) => {
_ => Err(TypeError::WrongType("LIST")), 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"{}"); buf.extend_from_slice(b"{}");
} else if d.len() == 1 { } else if d.len() == 1 {
let (k, v) = d.into_iter().next().unwrap(); let (k, v) = d.into_iter().next().unwrap();
buf.extend_from_slice(b"{ "); buf.extend_from_slice(b"{ ");
buf.extend_from_slice(k.borrow()); buf.extend_from_slice(k.borrow());
buf.extend_from_slice(b" = "); buf.extend_from_slice(b" = ");
v.encode_aux(buf, indent + 2, false); v.encode_aux(buf, indent + 2, false);
buf.extend_from_slice(b" }"); buf.extend_from_slice(b" }");
} else { } else {
@ -307,29 +307,32 @@ impl<'a> T<'a> {
buf.push(b'}'); buf.push(b'}');
} }
} }
T::List(l) => { T::List(l) => {
if l.len() == 0 { if l.len() == 0 {
buf.extend_from_slice(b"[]"); buf.extend_from_slice(b"[]");
} else if l.len() == 1 { } else if l.len() == 1 {
buf.extend_from_slice(b"[ "); buf.extend_from_slice(b"[ ");
l.into_iter().next().unwrap().encode_aux(buf, indent + 2, false); l.into_iter()
buf.extend_from_slice(b" ]"); .next()
} else { .unwrap()
let indent2 = indent + 2; .encode_aux(buf, indent + 2, false);
buf.extend_from_slice(b"[\n"); buf.extend_from_slice(b" ]");
for item in l { } else {
for _ in 0..indent2 { let indent2 = indent + 2;
buf.push(b' '); buf.extend_from_slice(b"[\n");
} for item in l {
item.encode_aux(buf, indent2, false); for _ in 0..indent2 {
buf.extend_from_slice(b",\n"); buf.push(b' ');
} }
for _ in 0..indent { item.encode_aux(buf, indent2, false);
buf.push(b' '); buf.extend_from_slice(b",\n");
} }
buf.push(b']'); for _ in 0..indent {
} buf.push(b' ');
} }
buf.push(b']');
}
}
T::Seq(l) => { T::Seq(l) => {
let indent2 = indent + 2; let indent2 = indent + 2;
for (i, v) in l.into_iter().enumerate() { 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::Str(s) => buf.extend_from_slice(s),
T::OwnedStr(s) => buf.extend_from_slice(&s), T::OwnedStr(s) => buf.extend_from_slice(&s),
T::Dict(mut d) => { T::Dict(mut d) => {
buf.push(b'{'); buf.push(b'{');
let mut keys = d.keys().cloned().collect::<Vec<_>>(); let mut keys = d.keys().cloned().collect::<Vec<_>>();
keys.sort(); keys.sort();
for (i, k) in keys.into_iter().enumerate() { 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() {
if i > 0 { if i > 0 {
buf.push(b','); buf.push(b',');
} }
item.encode_concise_aux(buf); let v = d.remove(&k).unwrap();
} buf.extend_from_slice(k.borrow());
buf.push(b']'); 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) => { T::Seq(l) => {
for (i, v) in l.into_iter().enumerate() { for (i, v) in l.into_iter().enumerate() {
if i > 0 { if i > 0 {
@ -390,18 +393,15 @@ impl<'a> T<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::debug;
use super::*; use super::*;
use crate::debug;
#[test] #[test]
fn complex1() { fn complex1() {
let input = seq([ let input = seq([
string("HELLO").unwrap(), string("HELLO").unwrap(),
string("alexhelloworld").unwrap(), string("alexhelloworld").unwrap(),
list([ list([string("dude").unwrap(), string("why").unwrap()]),
string("dude").unwrap(),
string("why").unwrap(),
]),
dict([ dict([
("from", string("jxx").unwrap()), ("from", string("jxx").unwrap()),
("subject", string("hello").unwrap()), ("subject", string("hello").unwrap()),
@ -420,17 +420,14 @@ mod tests {
subject = hello, subject = hello,
}"; }";
assert_eq!(debug(&input.encode()), expected); assert_eq!(debug(&input.encode()), expected);
} }
#[test] #[test]
fn complex1_concise() { fn complex1_concise() {
let input = seq([ let input = seq([
string("HELLO").unwrap(), string("HELLO").unwrap(),
string("alexhelloworld").unwrap(), string("alexhelloworld").unwrap(),
list([ list([string("dude").unwrap(), string("why").unwrap()]),
string("dude").unwrap(),
string("why").unwrap(),
]),
dict([ dict([
("from", string("jxx").unwrap()), ("from", string("jxx").unwrap()),
("subject", string("hello").unwrap()), ("subject", string("hello").unwrap()),

View file

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