microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dmitryv/select-updated

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

katas/src/lib.rs

49lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![warn(clippy::mod_module_files, clippy::pedantic, clippy::unwrap_used)]
5#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
6
7#[cfg(test)]
8mod tests;
9
10use qsc::{
11 interpret::{
12 output::Receiver,
13 stateful::{self, Interpreter},
14 Value,
15 },
16 target::Profile,
17 PackageType, SourceContents, SourceMap, SourceName,
18};
19
20pub const EXAMPLE_ENTRY: &str = "Kata.RunExample()";
21
22pub const EXERCISE_ENTRY: &str = "Kata.Verification.CheckSolution()";
23
24/// # Errors
25///
26/// Returns a vector of errors if compilation or evaluation failed.
27///
28/// # Panics
29///
30/// Will panic if evaluation does not return a boolean as result.
31pub fn check_solution(
32 exercise_sources: Vec<(SourceName, SourceContents)>,
33 receiver: &mut impl Receiver,
34) -> Result<bool, Vec<stateful::Error>> {
35 let source_map = SourceMap::new(exercise_sources, Some(EXERCISE_ENTRY.into()));
36 let mut interpreter: Interpreter = Interpreter::new(
37 true,
38 source_map,
39 PackageType::Exe,
40 Profile::Unrestricted.into(),
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