Skip to main content

dvm_api/
lib.rs

1//! dvm.run's REST API: what it takes and returns. dvm.run's controller
2//! serves it, and the SDKs and its panel call it; the controller, the panel
3//! and the Rust SDK check a sandbox's spec with the same code.
4//!
5//! A type that holds an invariant (a checked spec, an image reference) keeps
6//! its fields private; a plain record of what the controller answers has
7//! them public.
8
9mod api_key;
10mod capacity;
11mod cli_login;
12mod execution;
13mod image_reference;
14mod run;
15mod sandbox;
16mod sandbox_input;
17mod sandbox_protocol;
18mod timestamp;
19mod user;
20
21pub use api_key::{ApiKey, CreatedApiKey, NewApiKey};
22pub use capacity::Capacity;
23pub use cli_login::{CliLogin, CollectCliLogin, NewCliLogin, StartedCliLogin};
24pub use execution::Execution;
25pub use image_reference::{ImageReference, InvalidImageReference};
26pub use run::{
27    ByteChunk, InvalidRunAction, OutputStream, ReplayVerdict, Run, RunAction, RunId, RunOutput,
28    RunState,
29};
30pub use sandbox::{
31    EnvironmentVariable, Sandbox, SandboxDraft, SandboxId, SandboxSpec, SandboxesPage, SpecError,
32    SpecField,
33};
34pub use sandbox_input::{OutputAfter, SandboxInput, SandboxInputState};
35pub use sandbox_protocol::{
36    SandboxAnswer, SandboxAnswerKind, SandboxExited, SandboxProgram, SandboxRead, SandboxRequest,
37    SandboxRequestAction, SandboxRun, SandboxStarted, SandboxStdin, SandboxWrite,
38};
39pub use timestamp::Timestamp;
40pub use user::{Me, UserLimits};
41
42use serde::{Deserialize, Serialize};
43
44/// What the controller answers instead, when it refuses a request: the body
45/// of every response that isn't a success.
46#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
47#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
48#[cfg_attr(feature = "typescript", ts(export_to = "controller-api.ts"))]
49pub struct ApiError {
50    pub message: String,
51}
52
53/// Writes every type's TypeScript, with the types it names, into
54/// `controller-api.ts` in `config`'s directory: the panel's API.
55#[cfg(feature = "typescript")]
56pub fn export_typescript(config: &ts_rs::Config) -> Result<(), ts_rs::ExportError> {
57    use ts_rs::TS;
58    ApiError::export_all(config)?;
59    ApiKey::export_all(config)?;
60    ByteChunk::export_all(config)?;
61    Capacity::export_all(config)?;
62    CliLogin::export_all(config)?;
63    CreatedApiKey::export_all(config)?;
64    EnvironmentVariable::export_all(config)?;
65    Execution::export_all(config)?;
66    ImageReference::export_all(config)?;
67    Me::export_all(config)?;
68    NewApiKey::export_all(config)?;
69    OutputStream::export_all(config)?;
70    ReplayVerdict::export_all(config)?;
71    Run::export_all(config)?;
72    RunAction::export_all(config)?;
73    RunId::export_all(config)?;
74    RunOutput::export_all(config)?;
75    SandboxInput::export_all(config)?;
76    SandboxInputState::export_all(config)?;
77    OutputAfter::export_all(config)?;
78    RunState::export_all(config)?;
79    Sandbox::export_all(config)?;
80    SandboxRequest::export_all(config)?;
81    SandboxAnswer::export_all(config)?;
82    SandboxDraft::export_all(config)?;
83    SandboxId::export_all(config)?;
84    SandboxSpec::export_all(config)?;
85    SandboxesPage::export_all(config)?;
86    Timestamp::export_all(config)?;
87    UserLimits::export_all(config)?;
88    Ok(())
89}
90
91#[cfg(all(test, feature = "typescript"))]
92mod typescript_tests {
93    #[test]
94    fn every_type_is_written_to_one_file() {
95        let directory = std::env::temp_dir().join(format!("dvm-api-{}", std::process::id()));
96        let config = ts_rs::Config::from_env().with_out_dir(&directory);
97        super::export_typescript(&config).unwrap();
98        let written = std::fs::read_to_string(directory.join("controller-api.ts")).unwrap();
99        std::fs::remove_dir_all(&directory).unwrap();
100        for declaration in [
101            "export type Sandbox = ",
102            "export type ApiError = ",
103            "export type Me = ",
104            "export type SandboxRequest = ",
105            "export type SandboxAnswer = ",
106        ] {
107            assert!(written.contains(declaration), "{declaration}");
108        }
109        for (name, fields) in [
110            ("SandboxRun", &["env", "cwd", "stdin"][..]),
111            ("SandboxStdin", &["data", "close"][..]),
112            ("SandboxWrite", &["mode", "last"][..]),
113            ("SandboxAnswer", &["id"][..]),
114        ] {
115            let declaration = written
116                .split_once(&format!("export type {name} = "))
117                .unwrap()
118                .1
119                .split(';')
120                .next()
121                .unwrap();
122            for field in fields {
123                assert!(
124                    declaration.contains(&format!("{field}?:")),
125                    "{name}.{field}: {declaration}"
126                );
127            }
128        }
129    }
130}