use std::sync::Arc;
use sim_kernel::{Cx, Expr, Result, Symbol};
use crate::{Shape, ShapeMatch, TableExtraPolicy, TableFieldSpec, TableShape};
pub struct OptionFieldSpec {
pub key: Symbol,
pub shape: Arc<dyn Shape>,
pub required: bool,
}
impl OptionFieldSpec {
pub fn required(key: Symbol, shape: Arc<dyn Shape>) -> Self {
Self {
key,
shape,
required: true,
}
}
pub fn optional(key: Symbol, shape: Arc<dyn Shape>) -> Self {
Self {
key,
shape,
required: false,
}
}
}
pub fn check_option_map(
cx: &mut Cx,
expr: &Expr,
fields: Vec<OptionFieldSpec>,
extra: TableExtraPolicy,
) -> Result<ShapeMatch> {
let fields = fields
.into_iter()
.map(|field| TableFieldSpec {
key: field.key,
shape: field.shape,
required: field.required,
})
.collect();
TableShape::new(fields, extra).check_expr(cx, expr)
}