use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::RelativePath;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum FileEntry {
Directory,
File {
text: String,
},
Symlink {
target: RelativePath,
},
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FileTree {
pub entries: BTreeMap<RelativePath, FileEntry>,
}
impl FileTree {
#[must_use]
pub fn contains_file(&self, path: &RelativePath) -> bool {
matches!(self.entries.get(path), Some(FileEntry::File { .. }))
}
#[must_use]
pub fn file_text(&self, path: &RelativePath) -> Option<&str> {
match self.entries.get(path) {
Some(FileEntry::File { text }) => Some(text),
_ => None,
}
}
pub fn directories(&self) -> impl Iterator<Item = &RelativePath> {
self.entries
.iter()
.filter_map(|(path, entry)| matches!(entry, FileEntry::Directory).then_some(path))
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct DiscoveryRequest {
pub protocol_version: semver::Version,
pub development_root: FileTree,
pub morphir_home: Option<FileTree>,
pub system_config: Option<FileTree>,
#[serde(default)]
pub environment: BTreeMap<String, String>,
#[serde(default)]
pub cli_overlay: serde_json::Value,
#[serde(default)]
pub purpose: DiscoveryPurpose,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceSelection {
pub root: RelativePath,
pub paths: Vec<RelativePath>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ProjectSource {
Synthesized,
#[serde(rename_all = "camelCase")]
Manifest { path: RelativePath },
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum DiscoveryPurpose {
#[default]
ManifestProjects,
#[serde(rename_all = "camelCase")]
AdHocSources {
project: ProjectSource,
sources: SourceSelection,
language_id: String,
},
}