Feature - support empty custom fn's ()

This commit is contained in:
Finn Bear 2023-05-31 15:39:44 -07:00 committed by GitHub
parent 3900bfa737
commit 721d3c5444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,7 @@ use crate::sql::value::{value, Value};
use nom::branch::alt;
use nom::combinator::map;
use nom::multi::many0;
use nom::multi::separated_list1;
use nom::multi::separated_list0;
use nom::sequence::delimited;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
@ -117,6 +117,7 @@ impl Display for Block {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let mut f = Pretty::from(f);
match (self.len(), self.first()) {
(0, _) => f.write_str("{}"),
(1, Some(Entry::Value(v))) => {
write!(f, "{{ {v} }}")
}
@ -159,7 +160,7 @@ impl Display for Block {
pub fn block(i: &str) -> IResult<&str, Block> {
let (i, _) = openbraces(i)?;
let (i, v) = separated_list1(colons, entry)(i)?;
let (i, v) = separated_list0(colons, entry)(i)?;
let (i, _) = many0(alt((colons, comment)))(i)?;
let (i, _) = closebraces(i)?;
Ok((i, Block(v)))
@ -245,6 +246,15 @@ mod tests {
use super::*;
#[test]
fn block_empty() {
let sql = "{}";
let res = block(sql);
assert!(res.is_ok());
let out = res.unwrap().1;
assert_eq!(sql, format!("{}", out))
}
#[test]
fn block_value() {
let sql = "{ 80 }";