microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7421e7dd1015dcbd940bf843d33583470de580ea

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#[cfg(test)]
5mod tests;
6
7use qsc::{
8 PackageType, SourceContents, SourceMap, SourceName,
9 interpret::{Error, Interpreter, Value, output::Receiver},
10 target::Profile,
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 (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}