Determine whether a given TODO is allowed
(&self, todo: &Todo)
| 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 |
no test coverage detected