microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cesarzc/ssa-panic

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#[cfg(test)]
5mod tests;
6
7use qsc::{
8 interpret::{output::Receiver, Error, Interpreter, Value},
9 target::Profile,
10 PackageType, SourceContents, SourceMap, SourceName,
11};
12
13use qsc::LanguageFeatures;
14
15pub const EXAMPLE_ENTRY: &str = "Kata.RunExample()";
16
17pub 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.
26pub 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 mut interpreter: Interpreter = Interpreter::new(
32 true,
33 source_map,
34 PackageType::Exe,
35 Profile::Unrestricted.into(),
36 LanguageFeatures::default(),
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