Skip to main content

morphir_workspace/
snapshot.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::{DiscoveryFailure, RelativePath, WorkspaceDiagnostic};
7
8/// The state of a discovered workspace.
9#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
10#[serde(rename_all = "kebab-case")]
11pub enum WorkspaceState {
12    /// The workspace was opened successfully.
13    Open,
14    /// The workspace was opened with workspace-level errors.
15    Error,
16}
17
18/// The state of a discovered project.
19#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
20#[serde(rename_all = "kebab-case")]
21pub enum ProjectState {
22    /// The project has been discovered but not loaded.
23    Unloaded,
24    /// The project could not be discovered completely.
25    Error,
26}
27
28/// A deterministic, provider-neutral snapshot of a workspace.
29#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
30#[serde(rename_all = "camelCase")]
31pub struct WorkspaceSnapshot {
32    /// The protocol version used to produce this snapshot.
33    pub protocol_version: semver::Version,
34    /// The workspace configuration path relative to the development root,
35    /// when one exists.
36    pub config_anchor: Option<RelativePath>,
37    /// The configured workspace name, when present.
38    pub name: Option<String>,
39    /// The overall workspace state.
40    pub state: WorkspaceState,
41    /// Discovered projects, sorted by canonical relative path and then name.
42    pub projects: Vec<ProjectSnapshot>,
43    /// Workspace diagnostics, sorted by project path, path, code, severity,
44    /// and message.
45    pub diagnostics: Vec<WorkspaceDiagnostic>,
46}
47
48/// In-memory discovery details for native hosts that need the exact effective
49/// configuration values produced by the portable discovery pass.
50///
51/// This sidecar is deliberately not part of the serialized discovery protocol.
52#[derive(Clone, Debug, PartialEq, Eq)]
53pub struct WorkspaceDiscoveryDetails {
54    /// The provider-neutral workspace snapshot.
55    pub snapshot: WorkspaceSnapshot,
56    /// The fully merged effective configuration at the workspace root.
57    pub root_effective: Value,
58    /// Fully merged effective configurations for valid projects, keyed by path.
59    pub project_effective: BTreeMap<RelativePath, Value>,
60}
61
62/// A deterministic, provider-neutral snapshot of a project.
63#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
64#[serde(rename_all = "camelCase")]
65pub struct ProjectSnapshot {
66    /// The project name. A synthesized project (see
67    /// [`ProjectOrigin::Synthesized`]) carries the explicit name from
68    /// `DiscoveryRequest::cli_overlay`'s `project.name`, when the request
69    /// supplied one; otherwise it emits `""` here; a later provider-synthesis
70    /// step fills it in.
71    pub name: String,
72    /// The project version, when configured.
73    pub version: Option<String>,
74    /// The project path relative to the development root.
75    pub relative_path: RelativePath,
76    /// The project configuration path, when one exists.
77    pub config_anchor: Option<RelativePath>,
78    /// The source directory relative to this project's [`Self::relative_path`].
79    pub source_directory: RelativePath,
80    /// The project state.
81    pub state: ProjectState,
82    /// Project diagnostics, sorted by project path, path, code, severity, and
83    /// message.
84    pub diagnostics: Vec<WorkspaceDiagnostic>,
85    /// Where this project came from.
86    pub origin: ProjectOrigin,
87    /// The modules this project exposes. `None` when not yet determined;
88    /// `Some(vec![])` means the project exposes no modules.
89    #[serde(default, skip_serializing_if = "Option::is_none")]
90    pub exposed_modules: Option<Vec<String>>,
91}
92
93/// Where a discovered project came from.
94///
95/// `config_anchor` records *which* manifest a project was read from but not
96/// *whether* there was one. A caller applies different configuration rules to
97/// a synthesized project, and inferring that from an absent anchor turns a
98/// policy decision into a missing field.
99#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(tag = "kind", rename_all = "kebab-case")]
101pub enum ProjectOrigin {
102    /// Read from the manifest at this path.
103    #[serde(rename_all = "camelCase")]
104    Manifest { path: RelativePath },
105    /// Synthesized from these sources, in the order the request gave them.
106    /// Development-root-relative, copied verbatim from
107    /// [`SourceSelection::paths`](crate::SourceSelection::paths).
108    #[serde(rename_all = "camelCase")]
109    Synthesized { inputs: Vec<RelativePath> },
110}
111
112/// The provider-neutral result of workspace discovery.
113#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
114#[serde(tag = "status", rename_all = "kebab-case")]
115pub enum DiscoveryResponse {
116    /// Discovery completed and produced a snapshot.
117    Success {
118        /// The discovered workspace snapshot.
119        snapshot: WorkspaceSnapshot,
120    },
121    /// Discovery failed before a snapshot could be produced.
122    Failure {
123        /// The fatal discovery failure.
124        error: DiscoveryFailure,
125    },
126}
127
128impl DiscoveryResponse {
129    /// Converts the wire response into a conventional Rust result.
130    pub fn into_result(self) -> Result<WorkspaceSnapshot, DiscoveryFailure> {
131        match self {
132            Self::Success { snapshot } => Ok(snapshot),
133            Self::Failure { error } => Err(error),
134        }
135    }
136}