microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b74867ceb788f490b95e6bb8a552faabc05c19ee

Branches

Tags

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

Clone

HTTPS

Download ZIP

katas/src/lib.rs

44lines · 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 PackageType, SourceContents, SourceMap, SourceName, TargetProfile,
17};
18
19pub const EXAMPLE_ENTRY: &str = "Kata.RunExample()";
20
21pub const EXERCISE_ENTRY: &str = "Kata.Verification.CheckSolution()";
22
23/// # Errors
24///
25/// Returns a vector of errors if compilation or evaluation failed.
26///
27/// # Panics
28///
29/// Will panic if evaluation does not return a boolean as result.
30pub fn check_solution(
31 exercise_sources: Vec<(SourceName, SourceContents)>,
32 receiver: &mut impl Receiver,
33) -> Result<bool, Vec<stateful::Error>> {
34 let source_map = SourceMap::new(exercise_sources, Some(EXERCISE_ENTRY.into()));
35 let mut interpreter: Interpreter =
36 Interpreter::new(true, source_map, PackageType::Exe, TargetProfile::Full)?;
37 interpreter.eval_entry(receiver).map(|value| {
38 if let Value::Bool(success) = value {
39 success
40 } else {
41 panic!("exercise verification did not return a boolean")
42 }
43 })
44}
45