Add support for array default values

This commit is contained in:
Lukas Kalbertodt
2022-10-17 11:28:12 +02:00
parent 0677f751aa
commit f0895a8b2f
6 changed files with 142 additions and 45 deletions

View File

@@ -62,3 +62,19 @@ pub fn from_env_with<T>(
}.into()),
}
}
/// `serde` does not implement `IntoDeserializer` for fixed size arrays. This
/// helper type is just used for this purpose.
pub struct ArrayIntoDeserializer<T, const N: usize>(pub [T; N]);
impl<'de, T, E, const N: usize> serde::de::IntoDeserializer<'de, E> for ArrayIntoDeserializer<T, N>
where
T: serde::de::IntoDeserializer<'de, E>,
E: serde::de::Error,
{
type Deserializer = serde::de::value::SeqDeserializer<std::array::IntoIter<T, N>, E>;
fn into_deserializer(self) -> Self::Deserializer {
serde::de::value::SeqDeserializer::new(self.0.into_iter())
}
}

View File

@@ -53,6 +53,7 @@ pub enum Expr {
Float(Float),
Integer(Integer),
Bool(bool),
Array(&'static [Expr]),
}
#[derive(Debug, Clone, Copy, PartialEq)]

View File

@@ -193,6 +193,18 @@ impl fmt::Display for PrintExpr {
Expr::Float(v) => v.fmt(f),
Expr::Integer(v) => v.fmt(f),
Expr::Bool(v) => v.fmt(f),
Expr::Array(items) => {
// TODO: pretty printing of long arrays onto multiple lines?
f.write_char('[')?;
for (i, item) in items.iter().enumerate() {
if i != 0 {
f.write_str(", ")?;
}
PrintExpr(item).fmt(f)?;
}
f.write_char(']')?;
Ok(())
},
}
}
}

View File

@@ -184,6 +184,18 @@ impl fmt::Display for PrintExpr {
Expr::Float(v) => v.fmt(f),
Expr::Integer(v) => v.fmt(f),
Expr::Bool(v) => v.fmt(f),
Expr::Array(items) => {
// TODO: pretty printing of long arrays onto multiple lines?
f.write_char('[')?;
for (i, item) in items.iter().enumerate() {
if i != 0 {
f.write_str(", ")?;
}
PrintExpr(item).fmt(f)?;
}
f.write_char(']')?;
Ok(())
}
}
}
}