MCPcopy Create free account
hub / github.com/cel-rust/cel-rust / new_struct

Method new_struct

cel/src/env.rs:266–299  ·  view source on GitHub ↗
(
        &self,
        fields: BTreeMap<String, std::borrow::Cow<dyn Val>>,
    )

Source from the content-addressed store, hash-verified

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)]

Callers 1

resolve_valMethod · 0.45

Calls 7

cloneMethod · 0.80
add_field_valueMethod · 0.80
clone_as_boxedMethod · 0.45
getMethod · 0.45
get_typeMethod · 0.45
to_ownedMethod · 0.45
nameMethod · 0.45

Tested by

no test coverage detected