basalt-bedrock 1.0.0

Definitions for Basalt competition packets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use std::{
    hash::{DefaultHasher, Hasher},
    io::Read,
    path::PathBuf,
    time::Duration,
};

use integrations::Integrations;
use language::LanguageSet;
use miette::{Diagnostic, LabeledSpan, NamedSource, SourceCode};
use packet::Packet;
use roi::RawOrImport;
use serde::{Deserialize, Serialize};
use typst::foundations::{Array, Dict, IntoValue, Str, Value};

mod custom_serde;
pub mod integrations;
pub mod language;
pub mod packet;
pub mod render;
pub mod roi;
pub mod scoring;

mod util;

#[cfg(test)]
mod tests;

pub(crate) fn default_false() -> bool {
    false
}

pub(crate) fn default_true() -> bool {
    false
}

pub(crate) fn default_port() -> u16 {
    8517
}

pub(crate) fn default_time_limit() -> Duration {
    Duration::from_secs(60 * 75)
}

pub(crate) fn default_points() -> i32 {
    10
}

/// Authentication details for a specific user (competitor or host)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
#[serde(deny_unknown_fields)]
pub struct User {
    pub name: String,
    pub display_name: Option<String>,
    pub password: String,
}

impl User {
    pub fn display_name(&self) -> &str {
        self.display_name.as_ref().unwrap_or(&self.name)
    }
}

/// Set of users that are either hosts or competitors
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
#[serde(deny_unknown_fields)]
pub struct Accounts {
    /// Hosts in charge of managing the competition
    pub hosts: Vec<User>,
    /// Competitors participating in the competition
    pub competitors: Vec<User>,
}

impl Accounts {
    pub fn to_value(&self) -> (Value, Value) {
        let hosts = self
            .hosts
            .iter()
            .map(|h| {
                [
                    (Str::from("username"), Str::from(&*h.name).into_value()),
                    (Str::from("password"), Str::from(&*h.password).into_value()),
                ]
                .into_iter()
                .collect::<Dict>()
                .into_value()
            })
            .collect::<Array>();
        let competitors = self
            .competitors
            .iter()
            .map(|c| {
                [
                    (Str::from("username"), Str::from(&*c.name).into_value()),
                    (Str::from("password"), Str::from(&*c.password).into_value()),
                ]
                .into_iter()
                .collect::<Dict>()
                .into_value()
            })
            .collect::<Array>();

        (hosts.into_value(), competitors.into_value())
    }
}

/// Configuration for setting up the docker container and starting the server
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
#[serde(deny_unknown_fields)]
pub struct Setup {
    /// Specifies what commands are to be run when building the container to ensure dependencies
    /// are installed.
    pub install: Option<RawOrImport<String, roi::Raw>>,
    /// Specifies commands to run before running basalt-server so that dependencies are enabled
    /// properly.
    pub init: Option<RawOrImport<String, roi::Raw>>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[serde(deny_unknown_fields)]
pub struct PointsSettings {
    /// An expression used to evaluate how many points a competitor should get upon
    /// completion of a problem. This expression has access to variables that can allow for
    /// very flexible scoring mechanisms. By default however, all points will simply be
    /// awarded upon successful submission and evaluation.
    ///
    /// Variables:
    ///
    /// `p` or `points`: number of points available for the problem
    ///
    /// `t` or `teams`: number of teams in the competition
    ///
    /// `c` or `completed`: number of teams that have already completed the problem
    ///
    /// `a` or `attempts`: number of attempts that a team has made to solve the problem
    ///
    /// Example:
    /// ```toml
    /// # Decrease the points by 2 each time someone completes it.
    /// score = "p - 2*c"
    /// ```
    pub score: String,
    #[serde(default = "default_points")]
    pub question_point_value: i32,
    #[serde(default = "default_time_limit", with = "custom_serde::duration")]
    pub time_limit: Duration,
}

impl Default for PointsSettings {
    fn default() -> Self {
        Self {
            score: "p".into(),
            question_point_value: default_points(),
            time_limit: default_time_limit(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
#[serde(untagged)]
pub enum RaceMode {
    #[default]
    Sprint,
    Endurance,
    Relay,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
pub struct RaceSettings {
    pub race: RaceMode,
    pub arcade: bool,
    #[serde(with = "custom_serde::option_duration")]
    pub time_limit: Option<Duration>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[serde(untagged)]
pub enum Game {
    Points(PointsSettings),
    Race(RaceSettings),
}

impl Default for Game {
    fn default() -> Self {
        Self::Points(PointsSettings::default())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[serde(deny_unknown_fields)]
pub struct FileCopy {
    /// Source file to copy
    ///
    /// Relative to the directory in which the server is running
    pub from: PathBuf,
    /// Destination of the file
    ///
    /// Relative to the directory in which the test is run
    pub to: PathBuf,
}

/// Mirrors the `CommandConfig` type in [leucite](https://basalt-rs.github.io/erudite/erudite/struct.CommandConfig.html)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Default)]
#[serde(deny_unknown_fields, untagged)]
pub enum CommandConfig<T> {
    #[default]
    Neither,
    Both(T),
    Compile {
        compile: T,
    },
    Run {
        run: T,
    },
    Each {
        compile: T,
        run: T,
    },
}

impl<T> CommandConfig<T> {
    pub fn compile(&self) -> Option<&T> {
        match self {
            CommandConfig::Neither => None,
            CommandConfig::Both(t) => Some(t),
            CommandConfig::Compile { compile } => Some(compile),
            CommandConfig::Run { .. } => None,
            CommandConfig::Each { compile, .. } => Some(compile),
        }
    }

    pub fn run(&self) -> Option<&T> {
        match self {
            CommandConfig::Neither => None,
            CommandConfig::Both(t) => Some(t),
            CommandConfig::Compile { .. } => None,
            CommandConfig::Run { run } => Some(run),
            CommandConfig::Each { run, .. } => Some(run),
        }
    }
}

/// Configuration for the test runner
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[serde(deny_unknown_fields)]
pub struct TestRunner {
    /// The amount of time that a test may run before it is cancelled by the test runner and marked
    /// as failure
    ///
    /// Measured in milliseconds
    ///
    /// [Default: 10 seconds]
    #[serde(
        with = "custom_serde::duration",
        default = "TestRunner::default_timeout"
    )]
    pub timeout: Duration,
    /// Whether the test runner should trim the output of a test before comparing with the
    /// expected output
    ///
    /// If this is true, the output of `hello world    ` matches the expected output of ` hello
    /// world`
    ///
    /// [Default: true]
    #[serde(default = "TestRunner::default_trim_output")]
    pub trim_output: bool,
    /// Files to copy into the test directory
    #[serde(default)]
    pub copy_files: Vec<FileCopy>,
    /// Amount of memory that may be used by the process, measured in MiB
    #[serde(default)]
    pub max_memory: CommandConfig<u64>,
    /// Maximum size of files that may be created by the tests, measured in MiB
    #[serde(default)]
    pub max_file_size: CommandConfig<u64>,
}

impl TestRunner {
    fn default_timeout() -> Duration {
        Duration::from_secs(10)
    }

    fn default_trim_output() -> bool {
        true
    }
}

impl Default for TestRunner {
    fn default() -> Self {
        Self {
            timeout: Self::default_timeout(),
            trim_output: Self::default_trim_output(),
            copy_files: Default::default(),
            max_memory: CommandConfig::Neither,
            max_file_size: CommandConfig::Neither,
        }
    }
}

#[derive(Debug, thiserror::Error, Diagnostic)]
pub enum ConfigReadError {
    /// The Config file was unable to be read due to an IO error
    #[error("Failed to read file: {0}")]
    ReadError(#[from] std::io::Error),
    /// The data being deserialised was formatted incorrectly
    #[error("{}", .0.to_string())] // needed to use the miette error instead of thiserror
    #[diagnostic(transparent)]
    MalformedData(miette::Error),
}

impl ConfigReadError {
    fn malformed<S>(source: S, value: toml_edit::de::Error) -> Self
    where
        S: SourceCode + 'static,
    {
        let labels = if let Some(span) = value.span() {
            vec![LabeledSpan::new_with_span(Some("here".into()), span)]
        } else {
            Vec::new()
        };
        Self::MalformedData(
            miette::miette! {
                labels = labels,
                "{}", value.message()
            }
            .with_source_code(source),
        )
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Config {
    /// Configuration for setting up the docker container and starting the server
    pub setup: Option<RawOrImport<Setup>>,
    /// Port on which the server will be hosted
    #[serde(default = "default_port")]
    pub port: u16,
    /// Whether this competition should host the web client
    #[serde(default = "default_true")]
    pub web_client: bool,
    /// Indicates the mode by which the competition is held.
    ///
    /// In points, each team must attempt to score the most points possible
    #[serde(default)]
    pub game: Game,
    #[serde(default)]
    /// Contains information for all things related to programmability and
    /// external integrations in Basalt.
    pub integrations: Integrations,
    /// Maximum number of attempts that a user is allowed to make for a given problem
    pub max_submissions: Option<std::num::NonZero<u32>>,
    /// List of languages available for the server
    pub languages: RawOrImport<LanguageSet>,
    /// Accounts that will be granted access to the server
    pub accounts: RawOrImport<Accounts>,
    /// The packet for this competition
    pub packet: RawOrImport<Packet>,
    /// Configuration for the test runner
    #[serde(default)]
    pub test_runner: RawOrImport<TestRunner>,
}

impl std::hash::Hash for Config {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.setup.hash(state);
        // skip port
        // self.setup.hash(port);
        // self.events.hash(state);
        self.web_client.hash(state);
        self.languages.hash(state);
        self.accounts.hash(state);
        self.packet.hash(state);
        self.test_runner.hash(state);
    }
}

impl Config {
    /// Read config from a string
    ///
    /// - `file_name` provided for better miette errors
    pub fn from_str(
        content: impl AsRef<str>,
        file_name: Option<impl AsRef<str>>,
    ) -> Result<Self, ConfigReadError> {
        let content = content.as_ref();
        let config: Self = toml_edit::de::from_str(content).map_err(|e| {
            if let Some(file_name) = file_name {
                ConfigReadError::malformed(
                    NamedSource::new(file_name, content.to_string()).with_language("TOML"),
                    e,
                )
            } else {
                ConfigReadError::malformed(content.to_string(), e)
            }
        })?;
        Ok(config)
    }

    /// Read config from a file
    ///
    /// - `file_name` provided for better miette errors
    pub fn read<R>(
        reader: &mut R,
        file_name: Option<impl AsRef<str>>,
    ) -> Result<Self, ConfigReadError>
    where
        R: Read,
    {
        let mut buf = String::new();
        reader.read_to_string(&mut buf)?;
        Self::from_str(&buf, file_name)
    }

    /// Read config from a file asynchronously
    ///
    /// - `file_name` provided for better miette errors
    #[cfg(feature = "tokio")]
    pub async fn read_async<R>(
        reader: &mut R,
        file_name: Option<impl AsRef<str>>,
    ) -> Result<Self, ConfigReadError>
    where
        R: tokio::io::AsyncRead + Unpin,
    {
        use tokio::io::AsyncReadExt;
        let mut buf = String::new();
        reader.read_to_string(&mut buf).await?;
        Self::from_str(&buf, file_name)
    }

    /// Generate a hash string for this config
    ///
    /// ```
    /// # use basalt_bedrock::Config;
    /// # let config = Config::default();
    /// let hash = format!("Your hash is: {}", config.hash());
    /// ```
    pub fn hash(&self) -> String {
        let mut hasher = DefaultHasher::new();
        std::hash::Hash::hash(self, &mut hasher);
        let mut hash = hasher.finish();
        const N: u64 = 36;
        const ALPHABET: [u8; N as usize] = *b"abcdefghijklmnopqrstuvwxyz0123456789";
        let mut out = String::with_capacity(14);
        loop {
            let n = (hash % N) as usize;
            hash /= N;
            out.push(ALPHABET[n] as char);
            if hash == 0 {
                break;
            }
        }
        out
    }

    /// Render the competition information to a PDF, either using a provided template (written in
    /// [typst](https://typst.app/)) or the default template
    ///
    /// # Template
    ///
    /// The template currently provides several variables that contain information about the
    /// competition.
    ///
    /// - `#title`: `str` - the title of the competition
    /// - `#preamble`: `content` - rendered markdown of the competition
    /// - `#problems`: `array<Dict>` - array of problems in the packet
    pub fn render_pdf(&self, template: Option<String>) -> std::io::Result<Vec<u8>> {
        let template = if let Some(template) = template {
            template
        } else {
            #[cfg(feature = "dev")]
            {
                std::fs::read_to_string("./data/template.typ").unwrap()
            }
            #[cfg(not(feature = "dev"))]
            {
                include_str!("../data/template.typ").into()
            }
        };

        let mut world = render::typst::TypstWrapperWorld::new(template);

        let mut errs = Vec::new();
        let mut problems = Array::with_capacity(self.packet.problems.len());
        for p in &self.packet.problems {
            match p.as_value(&world) {
                Ok(v) => problems.push(v),
                Err(err) => errs.push(err),
            }
        }

        world
            .library
            .global
            .scope_mut()
            .define("problems", problems);

        world
            .library
            .global
            .scope_mut()
            .define("title", self.packet.title.as_str());

        let preamble = self
            .packet
            .preamble
            .as_deref()
            .map(|s| s.content(&world))
            .transpose()?;
        world
            .library
            .global
            .scope_mut()
            .define("preamble", preamble);

        let document = typst::compile(&world)
            .output
            .expect("Error compiling typst");
        typst_pdf::pdf(&document, &typst_pdf::PdfOptions::default())
            .map_err(|e| std::io::Error::other(format!("{:?}", e)))
    }

    /// Note: In the current implementation of `typst-pdf`, this just renders to a vector and then
    /// writes that to the `writer`.
    pub fn write_pdf<W>(&self, writer: &mut W, template: Option<String>) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        // XXX: I would really love it if typst offered an API that did not have to create a vec
        // just to render the PDF
        let vec = self.render_pdf(template)?;
        writer.write_all(&vec)
    }

    /// Render competition logins to PDF, either using a provided template (written in
    /// [typst](https://typst.app/)) or the default template
    ///
    /// # Template
    ///
    /// The template currently provides several variables that contain information about the
    /// competition.
    ///
    /// - `#title`: `str` - the title of the competition
    /// - `#preamble`: `content` - rendered markdown of the competition
    /// - `#problems`: `array<Dict>` - array of problems in the packet
    pub fn render_login_pdf(&self, template: Option<String>) -> std::io::Result<Vec<u8>> {
        let template = if let Some(template) = template {
            template
        } else {
            #[cfg(feature = "dev")]
            {
                std::fs::read_to_string("./data/login-template.typ").unwrap()
            }
            #[cfg(not(feature = "dev"))]
            {
                include_str!("../data/login-template.typ").into()
            }
        };

        let mut world = render::typst::TypstWrapperWorld::new(template);

        let mut errs = Vec::new();
        let mut problems = Array::with_capacity(self.packet.problems.len());
        for p in &self.packet.problems {
            match p.as_value(&world) {
                Ok(v) => problems.push(v),
                Err(err) => errs.push(err),
            }
        }

        world
            .library
            .global
            .scope_mut()
            .define("problems", problems);

        world
            .library
            .global
            .scope_mut()
            .define("title", self.packet.title.as_str());

        let preamble = self
            .packet
            .preamble
            .as_deref()
            .map(|s| s.content(&world))
            .transpose()?;
        world
            .library
            .global
            .scope_mut()
            .define("preamble", preamble);

        let (hosts, competitors) = self.accounts.to_value();
        world.library.global.scope_mut().define("hosts", hosts);
        world
            .library
            .global
            .scope_mut()
            .define("competitors", competitors);

        let document = typst::compile(&world)
            .output
            .expect("Error compiling typst");
        typst_pdf::pdf(&document, &typst_pdf::PdfOptions::default())
            .map_err(|e| std::io::Error::other(format!("{:?}", e)))
    }

    /// Note: In the current implementation of `typst-pdf`, this just renders to a vector and then
    /// writes that to the `writer`.
    pub fn write_login_pdf<W>(
        &self,
        writer: &mut W,
        template: Option<String>,
    ) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        // XXX: I would really love it if typst offered an API that did not have to create a vec
        // just to render the PDF
        let vec = self.render_login_pdf(template)?;
        writer.write_all(&vec)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            setup: None,
            port: default_port(),
            web_client: true,
            integrations: Default::default(),
            game: Default::default(),
            max_submissions: None,
            languages: Default::default(),
            accounts: Default::default(),
            packet: Default::default(),
            test_runner: Default::default(),
        }
    }
}