forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanifest.rs
More file actions
199 lines (176 loc) · 6.3 KB
/
Copy pathmanifest.rs
File metadata and controls
199 lines (176 loc) · 6.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Parsing of `Nulang.toml` package manifests.
//!
//! A manifest looks like:
//!
//! ```toml
//! [package]
//! name = "my-app"
//! version = "0.1.0"
//! entry = "src/main.nula" # optional; this is the default
//!
//! [dependencies]
//! util = { path = "../util" }
//! json = { git = "https://github.com/example/json.nu.git", tag = "v0.2.0" }
//! ```
use std::collections::BTreeMap;
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::types::{NuError, NuResult, Span};
/// Manifest file name, expected at the root of every package.
pub const MANIFEST_FILE: &str = "Nulang.toml";
/// Default entry point, relative to the package root.
pub const DEFAULT_ENTRY: &str = "src/main.nula";
/// A parsed `Nulang.toml`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Manifest {
pub package: PackageSection,
#[serde(default)]
pub dependencies: BTreeMap<String, Dependency>,
}
/// The `[package]` section.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct PackageSection {
pub name: String,
pub version: String,
/// Entry point relative to the package root; `src/main.nula` when omitted.
#[serde(default = "default_entry")]
pub entry: String,
/// Registry URL for publishing and fetching dependencies.
/// When set, `nula publish` uploads here and bare version deps resolve from here.
#[serde(default)]
pub registry: Option<String>,
#[serde(default)]
pub language: Option<String>,
}
fn default_entry() -> String {
DEFAULT_ENTRY.to_string()
}
/// One entry in `[dependencies]`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Dependency {
/// `foo = "0.1.0"` — a bare version requirement. Resolved from the
/// package's configured registry (`[package] registry`) at build time.
Version(String),
/// `foo = { path = "../foo" }` or `foo = { git = "...", ... }`.
Detailed(DependencyDetail),
}
/// Table form of a dependency: a local path, a git URL, or both refined by a
/// version requirement.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct DependencyDetail {
pub path: Option<String>,
pub git: Option<String>,
pub rev: Option<String>,
pub branch: Option<String>,
pub tag: Option<String>,
pub version: Option<String>,
}
impl Manifest {
/// Parse a manifest from its TOML text.
pub fn parse(source: &str) -> NuResult<Manifest> {
toml::from_str(source).map_err(|e| NuError::PackageError {
msg: format!("invalid {}: {}", MANIFEST_FILE, e),
span: Span::default(),
})
}
/// Load and parse the manifest in `dir`.
pub fn load(dir: &Path) -> NuResult<Manifest> {
let path = dir.join(MANIFEST_FILE);
let source = std::fs::read_to_string(&path).map_err(|e| NuError::PackageError {
msg: format!("cannot read {}: {}", path.display(), e),
span: Span::default(),
})?;
Self::parse(&source)
}
/// Serialize this manifest to a TOML string.
pub fn to_toml(&self) -> NuResult<String> {
toml::to_string_pretty(self).map_err(|e| NuError::PackageError {
msg: format!("cannot serialize {}: {}", MANIFEST_FILE, e),
span: Span::default(),
})
}
/// Write this manifest into `dir`.
pub fn save(&self, dir: &Path) -> NuResult<()> {
let path = dir.join(MANIFEST_FILE);
std::fs::write(&path, self.to_toml()?).map_err(|e| NuError::PackageError {
msg: format!("cannot write {}: {}", path.display(), e),
span: Span::default(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_manifest_parse_minimal() {
let source = r#"
[package]
name = "my-app"
version = "0.1.0"
"#;
let manifest = Manifest::parse(source).expect("minimal manifest should parse");
assert_eq!(manifest.package.name, "my-app");
assert_eq!(manifest.package.version, "0.1.0");
assert_eq!(manifest.package.entry, DEFAULT_ENTRY);
assert!(manifest.dependencies.is_empty());
}
#[test]
fn test_manifest_parse_with_dependencies() {
let source = r#"
[package]
name = "my-app"
version = "0.2.0"
entry = "src/app.nula"
[dependencies]
util = { path = "../util" }
json = { git = "https://github.com/example/json.nu.git", tag = "v0.2.0" }
fancy = { git = "https://example.com/fancy.git", rev = "abc123", version = "1.0.0" }
registry_dep = "0.3.0"
"#;
let manifest = Manifest::parse(source).expect("manifest with deps should parse");
assert_eq!(manifest.package.entry, "src/app.nula");
assert_eq!(manifest.dependencies.len(), 4);
let util = &manifest.dependencies["util"];
assert_eq!(
*util,
Dependency::Detailed(DependencyDetail {
path: Some("../util".to_string()),
..Default::default()
})
);
let json = &manifest.dependencies["json"];
match json {
Dependency::Detailed(d) => {
assert_eq!(
d.git.as_deref(),
Some("https://github.com/example/json.nu.git")
);
assert_eq!(d.tag.as_deref(), Some("v0.2.0"));
assert_eq!(d.path, None);
}
Dependency::Version(_) => panic!("json should be a detailed dependency"),
}
assert_eq!(
manifest.dependencies["registry_dep"],
Dependency::Version("0.3.0".to_string())
);
}
#[test]
fn test_manifest_parse_missing_name_fails() {
let source = r#"
[package]
version = "0.1.0"
"#;
let err = Manifest::parse(source).expect_err("name is required");
match err {
NuError::PackageError { msg, .. } => assert!(msg.contains(MANIFEST_FILE)),
other => panic!("expected PackageError, got {:?}", other),
}
}
#[test]
fn test_manifest_parse_invalid_toml_fails() {
let err = Manifest::parse("not [valid toml").expect_err("garbage should not parse");
assert!(matches!(err, NuError::PackageError { msg: _, span: _ }));
}
}