(
&self,
fields: BTreeMap<String, std::borrow::Cow<dyn Val>>,
)
| 264 | /// - An unknown field name is provided. |
| 265 | #[cfg(feature = "structs")] |
| 266 | pub(crate) fn new_struct( |
| 267 | &self, |
| 268 | fields: BTreeMap<String, std::borrow::Cow<dyn Val>>, |
| 269 | ) -> Result<CelStruct, ExecutionError> { |
| 270 | let mut s = CelStruct::new(self.name.clone()); |
| 271 | let mut fields = fields; |
| 272 | for (field, default) in &self.defaults { |
| 273 | if let Some(value) = fields.remove(field) { |
| 274 | s.add_field_value(field.clone(), value); |
| 275 | } else { |
| 276 | s.add_field_value(field.clone(), Cow::Owned(default.clone_as_boxed())); |
| 277 | } |
| 278 | } |
| 279 | for (field, value) in fields { |
| 280 | match self.fields.get(&field) { |
| 281 | Some(t) => { |
| 282 | if t != value.get_type() { |
| 283 | return Err(ExecutionError::UnexpectedType { |
| 284 | got: value.get_type().name().to_owned(), |
| 285 | want: format!("{} for field {field} in {}", t.name(), self.name), |
| 286 | }); |
| 287 | } |
| 288 | s.add_field_value(field, value); |
| 289 | } |
| 290 | None => { |
| 291 | return Err(ExecutionError::NoSuchKey(std::sync::Arc::new(format!( |
| 292 | "field `{field}` on struct `{}`", |
| 293 | self.name |
| 294 | )))) |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | Ok(s) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | #[cfg(test)] |
no test coverage detected