Small syntax hack for nested things

This commit is contained in:
Alex 2022-11-18 18:06:43 +01:00
parent 790aac7f75
commit 24c7a9bbb5
Signed by: lx
GPG key ID: 0E496D15096376BE
3 changed files with 34 additions and 11 deletions

View file

@ -134,7 +134,7 @@ pub fn list_flatten<'a, I: IntoIterator<Item = Term<'a>>>(terms: I) -> Result<'a
Ok(Term(T::List(tmp)))
}
/// Term corresponding to a list of terms. Sub-lists are represented as NESTED: `{ . = sub list items }`.
/// Term corresponding to a list of terms. Sub-lists are represented as NESTED: `{.= sub list items }`.
pub fn list_nested<'a, I: IntoIterator<Item = Term<'a>>>(terms: I) -> Result<'a> {
let mut tmp = Vec::with_capacity(8);
for t in terms {
@ -238,7 +238,7 @@ impl<'a> Term<'a> {
/// ```
/// use nettext::enc::*;
///
/// assert_eq!(list([string("hello").unwrap(), string("world").unwrap()]).unwrap().nested().encode(), b"{ . = hello world }");
/// assert_eq!(list([string("hello").unwrap(), string("world").unwrap()]).unwrap().nested().encode(), b"{.= hello world }");
/// ```
#[must_use]
pub fn nested(self) -> Term<'a> {
@ -266,10 +266,14 @@ impl<'a> T<'a> {
if d.is_empty() {
buf.extend_from_slice(b"{}");
} else if d.len() == 1 {
buf.extend_from_slice(b"{ ");
let (k, v) = d.into_iter().next().unwrap();
if k.as_ref() == b"." {
buf.extend_from_slice(b"{.= ");
} else {
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 {
@ -357,7 +361,7 @@ mod tests {
])
.unwrap()
.encode(),
b"a b { . = c d }"
b"a b {.= c d }"
);
}
}

View file

@ -39,6 +39,25 @@ mod tests {
seq = YQ Yg,
}"#;
test_bidir(input, expected);
let input = vec![
Test {
int: 1,
seq: vec!["a".to_string(), "b".to_string()],
},
Test {
int: 2,
seq: vec!["c".to_string(), "d".to_string()],
},
];
let expected = br#"{.= Test {
int = 1,
seq = YQ Yg,
} } {.= Test {
int = 2,
seq = Yw ZA,
} }"#;
test_bidir(input, expected);
}
#[test]
@ -75,7 +94,7 @@ mod tests {
E::Struct { a: 1 },
E::Tuple(3, 2),
];
let expected = br#"E.Unit E.Unit { . = E.Newtype 1 } { . = E.Tuple 1 2 } { . = E.Struct { a = 1 } } { . =
let expected = br#"E.Unit E.Unit {.= E.Newtype 1 } {.= E.Tuple 1 2 } {.= E.Struct { a = 1 } } {.=
E.Tuple 3 2 }"#;
test_bidir(input, expected);
}
@ -90,7 +109,7 @@ mod tests {
#[test]
fn test_seq2() {
let input = (1, 2, (2, 3, 4), 5, 6);
let expected = br#"1 2 { . = 2 3 4 } 5 6"#;
let expected = br#"1 2 {.= 2 3 4 } 5 6"#;
test_bidir(input, expected);
}
@ -104,7 +123,7 @@ mod tests {
#[test]
fn test_seq4() {
let input = [[1, 2], [2, 3], [3, 4]];
let expected = br#"{ . = 1 2 } { . = 2 3 } { . = 3 4 }"#;
let expected = br#"{.= 1 2 } {.= 2 3 } {.= 3 4 }"#;
test_bidir(input, expected);
}

View file

@ -422,7 +422,7 @@ mod tests {
assert_eq!(&to_bytes(&u).unwrap(), expected);
let n = (1, 2, (2, 3, 4), 5, 6);
let expected = br#"1 2 { . = 2 3 4 } 5 6"#;
let expected = br#"1 2 {.= 2 3 4 } 5 6"#;
assert_eq!(&to_bytes(&n).unwrap(), expected);
let t = [1, 2, 3, 4];
@ -430,7 +430,7 @@ mod tests {
assert_eq!(&to_bytes(&t).unwrap(), expected);
let s = [[1, 2], [2, 3], [3, 4]];
let expected = br#"{ . = 1 2 } { . = 2 3 } { . = 3 4 }"#;
let expected = br#"{.= 1 2 } {.= 2 3 } {.= 3 4 }"#;
assert_eq!(&to_bytes(&s).unwrap(), expected);
}