microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
alex/lsp

Branches

Tags

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

Clone

HTTPS

Download ZIP

katas/src/lib.rs

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