Skip to main content

morphir_workspace/
file_tree.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::RelativePath;
6
7/// An entry in a portable, root-confined file tree.
8#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
9#[serde(tag = "kind", rename_all = "kebab-case")]
10pub enum FileEntry {
11    /// A directory.
12    Directory,
13    /// A UTF-8 text file.
14    File {
15        /// The complete text file contents.
16        text: String,
17    },
18    /// A symbolic link whose target is confined to the same named mount.
19    ///
20    /// Protocol-v1 discovery rejects unresolved link entries explicitly. Hosts
21    /// must materialize confined targets into directory and file entries before
22    /// invoking discovery.
23    Symlink {
24        /// The canonical target path under the named mount.
25        target: RelativePath,
26    },
27}
28
29/// A deterministic collection of canonical paths and their entries.
30#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
31#[serde(rename_all = "camelCase")]
32pub struct FileTree {
33    /// Entries ordered by canonical relative path.
34    pub entries: BTreeMap<RelativePath, FileEntry>,
35}
36
37impl FileTree {
38    /// Returns whether `path` names a UTF-8 text file in this tree.
39    #[must_use]
40    pub fn contains_file(&self, path: &RelativePath) -> bool {
41        matches!(self.entries.get(path), Some(FileEntry::File { .. }))
42    }
43
44    /// Returns the text stored at `path`, or `None` when it is not a text file.
45    #[must_use]
46    pub fn file_text(&self, path: &RelativePath) -> Option<&str> {
47        match self.entries.get(path) {
48            Some(FileEntry::File { text }) => Some(text),
49            _ => None,
50        }
51    }
52
53    /// Iterates over canonical directory paths in sorted order.
54    pub fn directories(&self) -> impl Iterator<Item = &RelativePath> {
55        self.entries
56            .iter()
57            .filter_map(|(path, entry)| matches!(entry, FileEntry::Directory).then_some(path))
58    }
59}
60
61/// A complete, provider-independent request for workspace discovery.
62#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
63#[serde(rename_all = "camelCase")]
64pub struct DiscoveryRequest {
65    /// The requested workspace discovery protocol version.
66    pub protocol_version: semver::Version,
67    /// The file tree rooted at the development mount.
68    pub development_root: FileTree,
69    /// The optional file tree rooted at the Morphir home mount.
70    pub morphir_home: Option<FileTree>,
71    /// The optional file tree rooted at the system configuration mount.
72    pub system_config: Option<FileTree>,
73    /// Environment values available to portable configuration resolution.
74    #[serde(default)]
75    pub environment: BTreeMap<String, String>,
76    /// Command-line configuration values overlaid onto discovered configuration.
77    ///
78    /// For [`DiscoveryPurpose::AdHocSources`], `project.name` here is the
79    /// *only* place an explicit project name can come from: a host that wants
80    /// to name a synthesized project puts it at `cli_overlay.project.name`.
81    /// Portable discovery reads that exact path directly, never the merged
82    /// effective configuration, so a default from built-in defaults, a shared
83    /// system/global layer or the environment can never be mistaken for an
84    /// explicit override. When absent, the synthesized project has no name
85    /// and discovery requires its selection to contain exactly one source.
86    #[serde(default)]
87    pub cli_overlay: serde_json::Value,
88    /// What this request is asking for.
89    #[serde(default)]
90    pub purpose: DiscoveryPurpose,
91}
92
93/// Which sources a request selects, and the root they are measured from.
94///
95/// The root is carried, never recomputed. Module names are derived relative to
96/// it, and recovering it from the files gets it wrong: a selection of
97/// `models/domain/customer.gleam` has no `src` segment, so a provider guessing
98/// a root falls through to the basename and names the module `customer`
99/// instead of `domain/customer`.
100#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
101#[serde(rename_all = "camelCase")]
102pub struct SourceSelection {
103    /// The root the selected paths are measured from, relative to the
104    /// development root.
105    pub root: RelativePath,
106    /// The selected sources, relative to the development root, in the order
107    /// the caller gave them.
108    pub paths: Vec<RelativePath>,
109}
110
111/// Where a request's project identity comes from.
112#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(tag = "kind", rename_all = "kebab-case")]
114pub enum ProjectSource {
115    /// No manifest. Name, version and configuration are synthesized.
116    Synthesized,
117    /// Identity comes from the manifest at this path, relative to the
118    /// development root, while the selection decides the sources.
119    #[serde(rename_all = "camelCase")]
120    Manifest { path: RelativePath },
121}
122
123/// What a discovery request is asking for.
124#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(tag = "kind", rename_all = "kebab-case")]
126pub enum DiscoveryPurpose {
127    /// Find the projects this tree's manifests describe. The default, and what
128    /// discovery has always meant.
129    #[default]
130    ManifestProjects,
131    /// Compile an explicit selection of sources, whose identity comes from
132    /// `project`.
133    #[serde(rename_all = "camelCase")]
134    AdHocSources {
135        project: ProjectSource,
136        sources: SourceSelection,
137        /// The language every selected source is in. One language per set.
138        /// Discovery rejects an empty string; it does not otherwise validate
139        /// the value.
140        language_id: String,
141    },
142}