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

Method todo_is_allowed

example/src/axum.rs:93–114  ·  view source on GitHub ↗

Determine whether a given TODO is allowed

(&self, todo: &Todo)

Source from the content-addressed store, hash-verified

91
92 // Determine whether a given TODO is allowed
93 fn todo_is_allowed(&self, todo: &Todo) -> Result<bool, TodosError> {
94 // Create a new mutable context out of the root context
95 let mut ctx = self.0.new_inner_scope();
96 // Add the TODO's text as a variable so that it can be part of the expression
97 ctx.add_variable_from_value("text", todo.text.clone());
98
99 // Which policy to enforce depends on the kind of TODO
100 let policy = match todo.kind {
101 TodoKind::Home => HOME_TODO_POLICY,
102 TodoKind::Work => WORK_TODO_POLICY,
103 };
104
105 // Compile the program
106 let program = Program::compile(policy)?;
107
108 // Execute the program and either return a Boolean or the TODO is
109 // considered invalid
110 match program.execute(&ctx)? {
111 Value::Bool(b) => Ok(b),
112 _ => Err(TodosError::Invalid),
113 }
114 }
115}
116
117// Custom error type

Callers 1

add_todoFunction · 0.80

Calls 4

new_inner_scopeMethod · 0.80
cloneMethod · 0.80
executeMethod · 0.80

Tested by

no test coverage detected