Skip to main content

basalt_bedrock/
packet.rs

1use std::collections::BTreeSet;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    render::markdown::{MarkdownRenderable, RenderError},
7    roi, RawOrImport,
8};
9
10/// Structure represnting data for a problem
11#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default, Hash)]
12#[serde(deny_unknown_fields)]
13pub struct Problem {
14    /// The languages that may be used to solve this question
15    ///
16    /// Must be a subset of the languages listed in the Config
17    pub languages: Option<BTreeSet<String>>,
18    /// The title for this specific problem
19    pub title: String,
20    /// The description of this problem (supports markdown)
21    pub description: Option<RawOrImport<MarkdownRenderable, roi::Raw>>,
22    /// The tests that will be used on this problem
23    pub tests: Vec<Test>,
24    pub points: Option<i32>,
25}
26
27impl Problem {
28    pub(crate) fn as_value(
29        &self,
30        world: &impl typst::World,
31    ) -> Result<typst::foundations::Value, RenderError> {
32        use crate::util;
33        use typst::foundations::Value;
34
35        let mut dict = typst::foundations::Dict::new();
36
37        if let Some(langs) = &self.languages {
38            dict.insert("languages".into(), util::convert(&langs));
39        }
40
41        dict.insert("title".into(), util::convert(&self.title));
42
43        if let Some(desc) = &self.description {
44            dict.insert("description".into(), Value::Content(desc.content(world)?));
45        }
46
47        dict.insert("tests".into(), util::convert(&self.tests));
48
49        Ok(Value::Dict(dict))
50    }
51}
52
53/// A specific test that will be used to validate that user's code.
54///
55/// The input and expected output for visible tests will be shown to the user
56#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
57#[serde(deny_unknown_fields)]
58pub struct Test {
59    /// The input that will be provided via STDIN to the test
60    pub input: String,
61    /// The expected output from STDOUT
62    pub output: String,
63    /// Whether this test should be shown to the competitor or just used for validation
64    ///
65    /// The first visible test will be shown as an example for the user
66    #[serde(default = "crate::default_false")]
67    pub visible: bool,
68}
69
70/// A packet which contains configuration for problems and their tests
71#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default, Hash)]
72#[serde(deny_unknown_fields)]
73pub struct Packet {
74    /// Title of the packet
75    pub title: String,
76    /// Information about the packet that will be included at the top of the file
77    pub preamble: Option<RawOrImport<MarkdownRenderable, roi::Raw>>,
78    /// The list of problems for this
79    pub problems: Vec<RawOrImport<Problem>>,
80}