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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Parser for `*.opam` files (OCaml/OPAM).
//!
//! Heuristic line-based parsing of OPAM package files:
//! - `name:` / `version:` → package metadata
//! - `depends:` list → `DepKind::Normal` by default,
//! `{with-test ...}` constraint → `DepKind::Dev`
//! - `depopts:` list → `DepKind::Optional`
//!
//! OPAM list format:
//! ```text
//! depends: [
//! "pkg-name" {>= "1.0"}
//! "other-pkg"
//! ]
//! ```
use crate::{DeclaredDep, DepKind, ManifestError, ManifestParser, ParsedManifest};
/// Parser for `*.opam` files (OCaml OPAM packages).
///
/// Since OPAM files use non-standard filenames (e.g. `mypackage.opam`),
/// register via extension using `parse_manifest_by_extension("opam", content)`.
pub struct OpamParser;
impl ManifestParser for OpamParser {
fn filename(&self) -> &'static str {
"*.opam"
}
fn parse(&self, content: &str) -> Result<ParsedManifest, ManifestError> {
let mut name: Option<String> = None;
let mut version: Option<String> = None;
let mut deps: Vec<DeclaredDep> = Vec::new();
#[derive(Clone, Copy, PartialEq)]
enum Section {
None,
Depends,
Depopts,
}
let mut section = Section::None;
let mut bracket_depth: i32 = 0;
for line in content.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
// Check for section headers at depth 0
if bracket_depth == 0 {
if let Some(val) = parse_opam_field(trimmed, "name") {
name = Some(val);
continue;
}
if let Some(val) = parse_opam_field(trimmed, "version") {
version = Some(val);
continue;
}
// `depends: [` or `depends: [ ... ]`
if trimmed.starts_with("depends:") {
section = Section::Depends;
} else if trimmed.starts_with("depopts:") {
section = Section::Depopts;
} else if !trimmed.starts_with('"') {
// Non-dep line at top level — could be another field starting a new block
if !trimmed.contains('[') {
section = Section::None;
}
}
}
// Count brackets
for ch in trimmed.chars() {
match ch {
'[' => bracket_depth += 1,
']' => {
bracket_depth -= 1;
if bracket_depth == 0 {
section = Section::None;
}
}
_ => {}
}
}
// Parse dep entries inside a section
if section != Section::None && bracket_depth > 0 {
// A dep entry looks like: `"pkg-name" {constraints}` or just `"pkg-name"`
if trimmed.starts_with('"') {
let kind_for_section = match section {
Section::Depends => DepKind::Normal,
Section::Depopts => DepKind::Optional,
Section::None => continue,
};
if let Some(dep) = parse_opam_dep_entry(trimmed, kind_for_section) {
deps.push(dep);
}
}
}
}
Ok(ParsedManifest {
ecosystem: "opam",
name,
version,
dependencies: deps,
})
}
}
/// Parse `key: "value"` → `Some(value)` (strips quotes).
fn parse_opam_field(line: &str, key: &str) -> Option<String> {
let prefix = format!("{}:", key);
let rest = line.strip_prefix(&prefix)?.trim();
let val = rest.trim_matches('"');
if val.is_empty() {
None
} else {
Some(val.to_string())
}
}
/// Parse a dep entry like `"pkg-name" {>= "1.0"}` or `"pkg-name" {with-test & >= "1.6"}`.
fn parse_opam_dep_entry(line: &str, default_kind: DepKind) -> Option<DeclaredDep> {
// Extract the package name (first quoted string)
let after_quote = line.strip_prefix('"')?;
let name_end = after_quote.find('"')?;
let pkg_name = after_quote[..name_end].to_string();
if pkg_name.is_empty() {
return None;
}
let rest = after_quote[name_end + 1..].trim();
// No constraints
if rest.is_empty() || !rest.contains('{') {
return Some(DeclaredDep {
name: pkg_name,
version_req: None,
kind: default_kind,
});
}
// Parse constraint block `{...}`
let brace_start = rest.find('{')?;
let brace_content_start = brace_start + 1;
let brace_end = rest.rfind('}')?;
let constraint = rest[brace_content_start..brace_end].trim();
// Detect `with-test` → Dev
let kind = if constraint.contains("with-test") || constraint.contains("with-doc") {
DepKind::Dev
} else {
default_kind
};
// Extract version: look for `>= "x"` or `"x"` patterns within constraint
let version_req = extract_opam_version(constraint);
Some(DeclaredDep {
name: pkg_name,
version_req,
kind,
})
}
/// Extract a version string from an OPAM constraint like `>= "4.14"` or `>= "1.0" & < "2.0"`.
fn extract_opam_version(constraint: &str) -> Option<String> {
// Remove with-test and similar flags, collect version constraints
let mut parts = Vec::new();
let clean = constraint
.replace("with-test", "")
.replace("with-doc", "")
.replace(['&', '|'], " ");
let mut chars = clean.chars().peekable();
let mut current_op = String::new();
while let Some(ch) = chars.next() {
match ch {
'>' | '<' | '=' | '!' => {
current_op.push(ch);
// Collect the rest of the operator
while chars.peek().is_some_and(|&c| matches!(c, '>' | '<' | '=')) {
// normalize-syntax-allow: rust/unwrap-in-impl - peek() confirmed Some; next() cannot fail
current_op.push(chars.next().unwrap());
}
// Skip whitespace
while chars.peek().is_some_and(|c| c.is_whitespace()) {
chars.next();
}
// Read quoted version
if chars.peek() == Some(&'"') {
chars.next(); // consume '"'
let mut ver = String::new();
for c in chars.by_ref() {
if c == '"' {
break;
}
ver.push(c);
}
parts.push(format!("{} \"{}\"", current_op.trim(), ver));
current_op.clear();
}
}
'"' => {
// Bare quoted version (no operator)
let mut ver = String::new();
for c in chars.by_ref() {
if c == '"' {
break;
}
ver.push(c);
}
if !ver.is_empty() {
parts.push(format!("\"{}\"", ver));
}
}
_ => {}
}
}
if parts.is_empty() {
None
} else {
Some(parts.join(" & "))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ManifestParser;
#[test]
fn test_parse_opam() {
let content = r#"opam-version: "2.0"
name: "my-package"
version: "0.1.0"
synopsis: "My OCaml package"
depends: [
"ocaml" {>= "4.14"}
"dune" {>= "3.0"}
"cmdliner" {>= "1.1"}
"alcotest" {with-test & >= "1.6"}
]
depopts: [
"ppx_sexp_conv"
]
"#;
let m = OpamParser.parse(content).unwrap();
assert_eq!(m.ecosystem, "opam");
assert_eq!(m.name.as_deref(), Some("my-package"));
assert_eq!(m.version.as_deref(), Some("0.1.0"));
let ocaml = m.dependencies.iter().find(|d| d.name == "ocaml").unwrap();
assert_eq!(ocaml.kind, DepKind::Normal);
assert!(ocaml.version_req.is_some());
let dune = m.dependencies.iter().find(|d| d.name == "dune").unwrap();
assert_eq!(dune.kind, DepKind::Normal);
let alcotest = m
.dependencies
.iter()
.find(|d| d.name == "alcotest")
.unwrap();
assert_eq!(alcotest.kind, DepKind::Dev);
let ppx = m
.dependencies
.iter()
.find(|d| d.name == "ppx_sexp_conv")
.unwrap();
assert_eq!(ppx.kind, DepKind::Optional);
}
#[test]
fn test_bare_dep() {
let content = "opam-version: \"2.0\"\nname: \"mypkg\"\nversion: \"1.0\"\ndepends: [\n \"ocaml\"\n \"dune\"\n]\n";
let m = OpamParser.parse(content).unwrap();
assert_eq!(m.dependencies.len(), 2);
assert!(m.dependencies.iter().all(|d| d.kind == DepKind::Normal));
}
}