Skip to main content

byob_protocol/
env.rs

1//! Workspace environments as clients see them: whether byob loads the workspace's own
2//! environment (flake.nix, pixi.toml, …), and whether its commands run in a container.
3
4use serde::{Deserialize, Serialize};
5
6/// Ask for the [`WorkspaceEnv`] of the chat in view, optionally changing it first.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(tag = "kind", rename_all = "snake_case")]
9pub enum EnvAction {
10    /// Remember whether to load the workspace's project environment (`None`: forget the
11    /// answer, so byob asks again next time).
12    ProjectEnv { load: Option<bool> },
13    /// Run this workspace's commands in a container (`None`: forget, ask again if it has a
14    /// devcontainer.json).
15    Container { on: Option<bool> },
16    /// Stop the workspace's container (it starts again on the next command).
17    StopContainer,
18    /// Remove the workspace's container (packages installed in it are lost).
19    RemoveContainer,
20    /// An action from a newer client this engine doesn't know (answered with a message,
21    /// changing nothing).
22    #[serde(other)]
23    Unsupported,
24}
25
26/// One workspace's environment settings and state.
27#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
28pub struct WorkspaceEnv {
29    pub workspace: String,
30    /// Project files byob can load an environment from, e.g. `flake.nix`, `pixi.toml`.
31    #[serde(default)]
32    pub project_files: Vec<String>,
33    /// The remembered answer: load it (`true`), don't (`false`), or ask (`None`).
34    #[serde(default)]
35    pub project_env: Option<bool>,
36    /// `BYOB_WORKSPACE_ENV` overrides the answer (`off`/`on`), for display.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub project_env_forced: Option<bool>,
39    /// What `install_packages` uses here ("Nix", "pixi"), if anything.
40    #[serde(default)]
41    pub installer: Option<String>,
42    #[serde(default)]
43    pub container: ContainerEnv,
44    /// What the last [`EnvAction`] did, or why it failed.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub message: Option<String>,
47    #[serde(default)]
48    pub failed: bool,
49}
50
51/// What the engine's machine offers for workspace environments.
52#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
53pub struct EnvSupport {
54    /// Nix is installed (the Nix environment loads a folder's dev shell).
55    #[serde(default)]
56    pub nix: bool,
57    /// What `install_packages` uses ("Nix", "pixi"), if anything.
58    #[serde(default)]
59    pub installer: Option<String>,
60    /// "docker" or "podman", if one is installed.
61    #[serde(default)]
62    pub runtime: Option<String>,
63}
64
65/// The container side of a [`WorkspaceEnv`].
66#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
67pub struct ContainerEnv {
68    /// "docker" or "podman", if one is installed.
69    #[serde(default)]
70    pub runtime: Option<String>,
71    /// The workspace's `.devcontainer/devcontainer.json` (or `.devcontainer.json`), if any.
72    #[serde(default)]
73    pub devcontainer: Option<String>,
74    /// The remembered choice: commands run in the container (`true`), on this machine
75    /// (`false`), or not decided (`None`; asked if there's a devcontainer.json).
76    #[serde(default)]
77    pub use_container: Option<bool>,
78    /// The image it uses (from devcontainer.json, else the default).
79    #[serde(default)]
80    pub image: String,
81    /// The container's state ("running", "exited", …); `None` if there is none.
82    #[serde(default)]
83    pub state: Option<String>,
84}