microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/src/lib.rs
49lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #[cfg(test)] |
| 5 | mod tests; |
| 6 | |
| 7 | use qsc::{ |
| 8 | PackageType, SourceContents, SourceMap, SourceName, |
| 9 | interpret::{Error, Interpreter, Value, output::Receiver}, |
| 10 | target::Profile, |
| 11 | }; |
| 12 | |
| 13 | use qsc::LanguageFeatures; |
| 14 | |
| 15 | pub const EXAMPLE_ENTRY: &str = "Kata.RunExample()"; |
| 16 | |
| 17 | pub const EXERCISE_ENTRY: &str = "Kata.Verification.CheckSolution()"; |
| 18 | |
| 19 | /// # Errors |
| 20 | /// |
| 21 | /// Returns a vector of errors if compilation or evaluation failed. |
| 22 | /// |
| 23 | /// # Panics |
| 24 | /// |
| 25 | /// Will panic if evaluation does not return a boolean as result. |
| 26 | pub fn check_solution( |
| 27 | exercise_sources: Vec<(SourceName, SourceContents)>, |
| 28 | receiver: &mut impl Receiver, |
| 29 | ) -> Result<bool, Vec<Error>> { |
| 30 | let source_map = SourceMap::new(exercise_sources, Some(EXERCISE_ENTRY.into())); |
| 31 | let (std_id, store) = qsc::compile::package_store_with_stdlib(Profile::Unrestricted.into()); |
| 32 | |
| 33 | let mut interpreter: Interpreter = Interpreter::new( |
| 34 | source_map, |
| 35 | PackageType::Exe, |
| 36 | Profile::Unrestricted.into(), |
| 37 | LanguageFeatures::default(), |
| 38 | store, |
| 39 | &[(std_id, None)], |
| 40 | )?; |
| 41 | |
| 42 | interpreter.eval_entry(receiver).map(|value| { |
| 43 | if let Value::Bool(success) = value { |
| 44 | success |
| 45 | } else { |
| 46 | panic!("exercise verification did not return a boolean") |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | |