1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Path, PathBuf},
};

use serde::{de::Error, Deserialize, Serialize};

use crate::{
    configuration_file::ConfigurationFileFormat, settings::GlobalSettings, util::normalize_path,
};

/// Main workspace configuration object.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Workspace {
    root: PathBuf,
    configuration_file_path: PathBuf,
    configuration_file_format: ConfigurationFileFormat,
    #[serde(flatten)]
    configuration: WorkspaceConfiguration,
}

#[derive(Debug, Serialize)]
pub struct ProjectRef {
    path: PathBuf,
    tags: BTreeSet<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
}

impl ProjectRef {
    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn tags(&self) -> &BTreeSet<String> {
        &self.tags
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }
}

impl From<PathBuf> for ProjectRef {
    fn from(path: PathBuf) -> Self {
        Self {
            description: None,
            tags: BTreeSet::new(),
            path,
        }
    }
}

impl<'de> Deserialize<'de> for ProjectRef {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(remote = "ProjectRef")]
        struct ProjectRefAsObject {
            path: PathBuf,
            #[serde(default)]
            tags: BTreeSet<String>,
            description: Option<String>,
        }

        #[derive(Deserialize)]
        #[serde(untagged)]
        enum ProjectRefDeserializationModes {
            SinglePath(PathBuf),
            #[serde(with = "ProjectRefAsObject")]
            Full(ProjectRef),
        }

        Ok(
            match ProjectRefDeserializationModes::deserialize(deserializer)? {
                ProjectRefDeserializationModes::SinglePath(path) => {
                    normalize_path(path).map_err(D::Error::custom)?.into()
                }
                ProjectRefDeserializationModes::Full(mut project_ref) => {
                    project_ref.path =
                        normalize_path(&project_ref.path).map_err(D::Error::custom)?;
                    project_ref
                }
            },
        )
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct WorkspaceConfiguration {
    name: String,
    #[serde(default)]
    projects: BTreeMap<String, ProjectRef>,
    #[serde(default)]
    settings: GlobalSettings,
}

impl Workspace {
    /// Create a [`Workspace`] from configuration file metadata and deserialized content.
    pub fn from_configuration_and_metadata<P: AsRef<Path>>(
        source: (P, ConfigurationFileFormat),
        configuration: WorkspaceConfiguration,
    ) -> Self {
        let mut root = source.0.as_ref().to_path_buf();
        let _ = root.pop();

        Self {
            root,
            configuration_file_path: source.0.as_ref().to_path_buf(),
            configuration_file_format: source.1,
            configuration,
        }
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn configuration_file_path(&self) -> &Path {
        &self.configuration_file_path
    }

    pub fn configuration_file_format(&self) -> ConfigurationFileFormat {
        self.configuration_file_format
    }

    pub fn name(&self) -> &str {
        &self.configuration.name
    }

    pub fn projects(&self) -> &BTreeMap<String, ProjectRef> {
        &self.configuration.projects
    }

    pub fn settings(&self) -> &GlobalSettings {
        &self.configuration.settings
    }
}