use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{DiscoveryFailure, RelativePath, WorkspaceDiagnostic};
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum WorkspaceState {
Open,
Error,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum ProjectState {
Unloaded,
Error,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceSnapshot {
pub protocol_version: semver::Version,
pub config_anchor: Option<RelativePath>,
pub name: Option<String>,
pub state: WorkspaceState,
pub projects: Vec<ProjectSnapshot>,
pub diagnostics: Vec<WorkspaceDiagnostic>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceDiscoveryDetails {
pub snapshot: WorkspaceSnapshot,
pub root_effective: Value,
pub project_effective: BTreeMap<RelativePath, Value>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ProjectSnapshot {
pub name: String,
pub version: Option<String>,
pub relative_path: RelativePath,
pub config_anchor: Option<RelativePath>,
pub source_directory: RelativePath,
pub state: ProjectState,
pub diagnostics: Vec<WorkspaceDiagnostic>,
pub origin: ProjectOrigin,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exposed_modules: Option<Vec<String>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum ProjectOrigin {
#[serde(rename_all = "camelCase")]
Manifest { path: RelativePath },
#[serde(rename_all = "camelCase")]
Synthesized { inputs: Vec<RelativePath> },
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "status", rename_all = "kebab-case")]
pub enum DiscoveryResponse {
Success {
snapshot: WorkspaceSnapshot,
},
Failure {
error: DiscoveryFailure,
},
}
impl DiscoveryResponse {
pub fn into_result(self) -> Result<WorkspaceSnapshot, DiscoveryFailure> {
match self {
Self::Success { snapshot } => Ok(snapshot),
Self::Failure { error } => Err(error),
}
}
}