1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-06 16:18:08 +03:00

Add term::make::static_access (#1438)

* Add `term::make::static_access`

Add a helper function to the `term::make` module to construct chains of
static record accesses.

* Provide a variant accepting an arbitrary `RichTerm` as the record

* Remove `term::make::static_access_`
This commit is contained in:
Viktor Kleen 2023-07-10 20:11:10 +00:00 committed by GitHub
parent 7a01d60270
commit 63bf9bb72e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1962,6 +1962,20 @@ pub mod make {
pub fn integer(n: impl Into<i64>) -> RichTerm { pub fn integer(n: impl Into<i64>) -> RichTerm {
Term::Num(Number::from(n.into())).into() Term::Num(Number::from(n.into())).into()
} }
pub fn static_access<I, S, T>(record: T, fields: I) -> RichTerm
where
I: IntoIterator<Item = S>,
I::IntoIter: DoubleEndedIterator,
S: Into<Ident>,
T: Into<RichTerm>,
{
let mut term = record.into();
for f in fields.into_iter() {
term = make::op1(UnaryOp::StaticAccess(f.into()), term);
}
term
}
} }
#[cfg(test)] #[cfg(test)]
@ -1984,4 +1998,19 @@ mod tests {
let res = TypeAnnotation::combine(outer, inner); let res = TypeAnnotation::combine(outer, inner);
assert_ne!(res.types, None); assert_ne!(res.types, None);
} }
#[test]
fn make_static_access() {
let t = make::op1(
UnaryOp::StaticAccess("record".into()),
make::op1(
UnaryOp::StaticAccess("records".into()),
make::var("predicates"),
),
);
assert_eq!(
make::static_access(make::var("predicates"), ["records", "record"]),
t
);
}
} }