Skip to main content

provenant/parsers/
julia.rs

1// SPDX-FileCopyrightText: Provenant contributors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Parser for Julia Project.toml and Manifest.toml files.
5//!
6//! Extracts package metadata, dependencies, and license information from
7//! Julia package manager (Pkg.jl) manifest files.
8//!
9//! # Supported Formats
10//! - Project.toml (package metadata)
11//! - Manifest.toml (resolved dependency tree)
12//!
13//! # Key Features
14//! - Dependency extraction with UUID tracking
15//! - `is_pinned` analysis based on Manifest.toml resolved versions
16//! - Package URL (purl) generation
17//! - Compat section version constraint extraction
18//!
19//! # Implementation Notes
20//! - Uses toml crate for parsing
21//! - Julia packages are identified by UUID
22//! - Project.toml `[deps]` lists direct dependencies by name → UUID
23//! - Manifest.toml `[[deps]]` entries contain resolved version + tree SHA
24
25use crate::models::{DatasourceId, Dependency, PackageData, PackageType, Party};
26use crate::parser_warn as warn;
27use crate::parsers::utils::{
28    MAX_ITERATION_COUNT, RecursionGuard, read_file_to_string, truncate_field,
29};
30use packageurl::PackageUrl;
31use std::path::Path;
32use toml::Value;
33
34use super::PackageParser;
35use super::license_normalization::{
36    DeclaredLicenseMatchMetadata, build_declared_license_data, empty_declared_license_data,
37    normalize_spdx_expression,
38};
39use super::metadata::ParserMetadata;
40
41const FIELD_NAME: &str = "name";
42const FIELD_UUID: &str = "uuid";
43const FIELD_VERSION: &str = "version";
44const FIELD_LICENSE: &str = "license";
45const FIELD_AUTHOR: &str = "author";
46const FIELD_AUTHORS: &str = "authors";
47const FIELD_REPOSITORY: &str = "repository";
48const FIELD_DEPS: &str = "deps";
49const FIELD_COMPAT: &str = "compat";
50const FIELD_TARGETS: &str = "targets";
51const FIELD_HOMEPAGE: &str = "homepage";
52
53pub struct JuliaProjectTomlParser;
54
55impl PackageParser for JuliaProjectTomlParser {
56    const PACKAGE_TYPE: PackageType = PackageType::Julia;
57
58    fn metadata() -> Vec<ParserMetadata> {
59        vec![ParserMetadata {
60            description: "Julia Project.toml manifest",
61            file_patterns: &["**/Project.toml"],
62            package_type: "julia",
63            primary_language: "Julia",
64            documentation_url: Some("https://pkgdocs.julialang.org/v1/toml-files/"),
65        }]
66    }
67
68    fn extract_packages(path: &Path) -> Vec<PackageData> {
69        let toml_content = match read_julia_toml(path) {
70            Ok(content) => content,
71            Err(e) => {
72                warn!("Failed to read or parse Project.toml at {:?}: {}", path, e);
73                return vec![default_project_package_data()];
74            }
75        };
76
77        let name = toml_content
78            .get(FIELD_NAME)
79            .and_then(|v| v.as_str())
80            .map(|s| truncate_field(s.to_string()));
81
82        let version = toml_content
83            .get(FIELD_VERSION)
84            .and_then(|v| v.as_str())
85            .map(|s| truncate_field(s.to_string()));
86
87        let raw_license = toml_content
88            .get(FIELD_LICENSE)
89            .and_then(|v| v.as_str())
90            .map(|s| truncate_field(s.to_string()));
91
92        let (declared_license_expression, declared_license_expression_spdx, license_detections) =
93            raw_license
94                .as_deref()
95                .and_then(normalize_spdx_expression)
96                .map(|normalized| {
97                    build_declared_license_data(
98                        normalized,
99                        DeclaredLicenseMatchMetadata::single_line(
100                            raw_license.as_deref().unwrap_or_default(),
101                        ),
102                    )
103                })
104                .unwrap_or_else(empty_declared_license_data);
105
106        let extracted_license_statement = raw_license.clone().map(truncate_field);
107
108        let dependencies = extract_project_dependencies(&toml_content);
109
110        let uuid = toml_content.get(FIELD_UUID).and_then(|v| v.as_str());
111        let purl = create_package_url(&name, &version, uuid);
112
113        let repository_url = toml_content
114            .get(FIELD_REPOSITORY)
115            .and_then(|v| v.as_str())
116            .map(|s| truncate_field(s.to_string()));
117
118        let homepage_url = toml_content
119            .get(FIELD_HOMEPAGE)
120            .and_then(|v| v.as_str())
121            .map(|s| truncate_field(s.to_string()));
122
123        let description = None;
124
125        let extra_data = extract_project_extra_data(&toml_content);
126
127        let is_private = false;
128
129        vec![PackageData {
130            package_type: Some(Self::PACKAGE_TYPE),
131            namespace: None,
132            name,
133            version,
134            qualifiers: None,
135            subpath: None,
136            primary_language: Some("Julia".to_string()),
137            description,
138            release_date: None,
139            parties: extract_parties(&toml_content),
140            keywords: Vec::new(),
141            homepage_url,
142            download_url: None,
143            size: None,
144            sha1: None,
145            md5: None,
146            sha256: None,
147            sha512: None,
148            bug_tracking_url: None,
149            code_view_url: None,
150            vcs_url: repository_url,
151            copyright: None,
152            holder: None,
153            declared_license_expression,
154            declared_license_expression_spdx,
155            license_detections,
156            other_license_expression: None,
157            other_license_expression_spdx: None,
158            other_license_detections: Vec::new(),
159            extracted_license_statement,
160            notice_text: None,
161            source_packages: Vec::new(),
162            file_references: Vec::new(),
163            is_private,
164            is_virtual: false,
165            extra_data,
166            dependencies,
167            repository_homepage_url: None,
168            repository_download_url: None,
169            api_data_url: None,
170            datasource_id: Some(DatasourceId::JuliaProjectToml),
171            purl,
172        }]
173    }
174
175    fn is_match(path: &Path) -> bool {
176        path.file_name()
177            .and_then(|name| name.to_str())
178            .is_some_and(|name| name.eq_ignore_ascii_case("Project.toml"))
179    }
180}
181
182pub struct JuliaManifestTomlParser;
183
184impl PackageParser for JuliaManifestTomlParser {
185    const PACKAGE_TYPE: PackageType = PackageType::Julia;
186
187    fn metadata() -> Vec<ParserMetadata> {
188        vec![ParserMetadata {
189            description: "Julia Manifest.toml resolved dependencies",
190            file_patterns: &["**/Manifest.toml"],
191            package_type: "julia",
192            primary_language: "Julia",
193            documentation_url: Some("https://pkgdocs.julialang.org/v1/toml-files/"),
194        }]
195    }
196
197    fn extract_packages(path: &Path) -> Vec<PackageData> {
198        let toml_content = match read_julia_toml(path) {
199            Ok(content) => content,
200            Err(e) => {
201                warn!("Failed to read or parse Manifest.toml at {:?}: {}", path, e);
202                return vec![];
203            }
204        };
205
206        extract_manifest_packages(&toml_content)
207    }
208
209    fn is_match(path: &Path) -> bool {
210        path.file_name()
211            .and_then(|name| name.to_str())
212            .is_some_and(|name| name.eq_ignore_ascii_case("Manifest.toml"))
213    }
214}
215
216fn read_julia_toml(path: &Path) -> Result<Value, String> {
217    let content =
218        read_file_to_string(path, None).map_err(|e| format!("Failed to read file: {}", e))?;
219    toml::from_str(&content).map_err(|e| format!("Failed to parse TOML: {}", e))
220}
221
222fn create_package_url(
223    name: &Option<String>,
224    version: &Option<String>,
225    uuid: Option<&str>,
226) -> Option<String> {
227    let name = name.as_ref()?;
228    // The julia purl-spec marks `uuid` as a required qualifier (the registry
229    // identity). Without it the PURL is spec-invalid, so omit it entirely
230    // rather than emit one missing the uuid.
231    let uuid = uuid?;
232
233    let mut package_url = match PackageUrl::new(PackageType::Julia.as_str(), name) {
234        Ok(p) => p,
235        Err(e) => {
236            warn!(
237                "Failed to create PackageUrl for julia package '{}': {}",
238                name, e
239            );
240            return None;
241        }
242    };
243
244    if let Some(v) = version
245        && let Err(e) = package_url.with_version(v)
246    {
247        warn!(
248            "Failed to set version '{}' for julia package '{}': {}",
249            v, name, e
250        );
251        return None;
252    }
253
254    if let Err(e) = package_url.add_qualifier("uuid", uuid) {
255        warn!(
256            "Failed to set uuid '{}' for julia package '{}': {}",
257            uuid, name, e
258        );
259        return None;
260    }
261
262    Some(truncate_field(package_url.to_string()))
263}
264
265fn extract_parties(toml_content: &Value) -> Vec<Party> {
266    use std::collections::HashSet;
267
268    let mut parties = Vec::new();
269    let mut seen = HashSet::new();
270
271    if let Some(authors) = toml_content.get(FIELD_AUTHORS).and_then(|v| v.as_array()) {
272        for author in authors.iter().take(MAX_ITERATION_COUNT) {
273            push_author_party(author, &mut parties, &mut seen);
274        }
275    }
276
277    if let Some(author_value) = toml_content.get(FIELD_AUTHOR) {
278        match author_value {
279            Value::Array(authors) => {
280                for author in authors.iter().take(MAX_ITERATION_COUNT) {
281                    push_author_party(author, &mut parties, &mut seen);
282                }
283            }
284            other => push_author_party(other, &mut parties, &mut seen),
285        }
286    }
287
288    parties
289}
290
291fn push_author_party(
292    value: &Value,
293    parties: &mut Vec<Party>,
294    seen: &mut std::collections::HashSet<String>,
295) {
296    let Some(author_str) = value.as_str() else {
297        return;
298    };
299
300    let author_name = truncate_field(author_str.trim().to_string());
301    if author_name.is_empty() || !seen.insert(author_name.clone()) {
302        return;
303    }
304
305    parties.push(Party {
306        r#type: None,
307        role: Some("author".to_string()),
308        name: Some(author_name),
309        email: None,
310        url: None,
311        organization: None,
312        organization_url: None,
313        timezone: None,
314    });
315}
316
317fn extract_project_dependencies(toml_content: &Value) -> Vec<Dependency> {
318    let mut dependencies = Vec::new();
319
320    let deps_table = match toml_content.get(FIELD_DEPS).and_then(|v| v.as_table()) {
321        Some(table) => table,
322        None => return dependencies,
323    };
324
325    let compat_table = toml_content.get(FIELD_COMPAT).and_then(|v| v.as_table());
326
327    for (dep_name, dep_value) in deps_table.iter().take(MAX_ITERATION_COUNT) {
328        let uuid = dep_value.as_str().map(String::from);
329
330        let extracted_requirement = compat_table
331            .and_then(|ct| ct.get(dep_name))
332            .and_then(|v| v.as_str())
333            .map(|s| truncate_field(s.to_string()));
334
335        let is_pinned = extracted_requirement
336            .as_deref()
337            .is_some_and(is_julia_version_pinned);
338
339        let Some(purl) = create_package_url(&Some(dep_name.clone()), &None, uuid.as_deref()) else {
340            continue;
341        };
342
343        let mut extra_data_map = std::collections::HashMap::new();
344        if let Some(ref uuid_val) = uuid {
345            extra_data_map.insert("uuid".to_string(), serde_json::json!(uuid_val));
346        }
347
348        dependencies.push(Dependency {
349            purl: Some(purl),
350            extracted_requirement,
351            scope: Some("dependencies".to_string()),
352            is_runtime: Some(true),
353            is_optional: None,
354            is_pinned: Some(is_pinned),
355            is_direct: Some(true),
356            resolved_package: None,
357            extra_data: if extra_data_map.is_empty() {
358                None
359            } else {
360                Some(extra_data_map)
361            },
362        });
363    }
364
365    dependencies
366}
367
368fn extract_manifest_packages(toml_content: &Value) -> Vec<PackageData> {
369    let mut packages = Vec::new();
370
371    // Manifest.toml format 2.0 (Julia 1.7+) nests packages under a [deps] table; format
372    // 1.0 (Julia 1.0-1.6) lists them as root-level [[PackageName]] array-of-tables. Prefer
373    // the [deps] table when present, otherwise fall back to the root table. Non-package
374    // metadata keys (manifest_format, julia_version, project_hash, ...) are scalars, so the
375    // as_array() check below skips them.
376    let deps_table = match toml_content.get(FIELD_DEPS).and_then(|v| v.as_table()) {
377        Some(table) => table,
378        None => match toml_content.as_table() {
379            Some(table) => table,
380            None => return packages,
381        },
382    };
383
384    for (dep_name, dep_value) in deps_table.iter().take(MAX_ITERATION_COUNT) {
385        let dep_entries = match dep_value.as_array() {
386            Some(entries) => entries,
387            None => continue,
388        };
389
390        for dep_entry in dep_entries.iter().take(MAX_ITERATION_COUNT) {
391            let name = Some(truncate_field(dep_name.clone()));
392
393            let uuid = dep_entry
394                .get(FIELD_UUID)
395                .and_then(|v| v.as_str())
396                .map(String::from);
397
398            let version = dep_entry
399                .get(FIELD_VERSION)
400                .and_then(|v| v.as_str())
401                .map(|s| truncate_field(s.to_string()));
402
403            let purl = create_package_url(&name, &version, uuid.as_deref());
404
405            let tree_hash = dep_entry
406                .get("git-tree-sha1")
407                .and_then(|v| v.as_str())
408                .map(String::from);
409
410            let source_url = dep_entry
411                .get("url")
412                .and_then(|v| v.as_str())
413                .map(|s| truncate_field(s.to_string()));
414
415            let mut extra_data_map = std::collections::HashMap::new();
416            if let Some(ref uuid_val) = uuid {
417                extra_data_map.insert("uuid".to_string(), serde_json::json!(uuid_val));
418            }
419            if let Some(ref tree_hash_val) = tree_hash {
420                extra_data_map.insert("tree_hash".to_string(), serde_json::json!(tree_hash_val));
421            }
422            if let Some(ref source_url_val) = source_url {
423                extra_data_map.insert("url".to_string(), serde_json::json!(source_url_val));
424            }
425
426            packages.push(PackageData {
427                package_type: Some(PackageType::Julia),
428                namespace: None,
429                name,
430                version,
431                qualifiers: None,
432                subpath: None,
433                primary_language: Some("Julia".to_string()),
434                description: None,
435                release_date: None,
436                parties: Vec::new(),
437                keywords: Vec::new(),
438                homepage_url: None,
439                download_url: None,
440                size: None,
441                sha1: None,
442                md5: None,
443                sha256: None,
444                sha512: None,
445                bug_tracking_url: None,
446                code_view_url: None,
447                vcs_url: source_url,
448                copyright: None,
449                holder: None,
450                declared_license_expression: None,
451                declared_license_expression_spdx: None,
452                license_detections: Vec::new(),
453                other_license_expression: None,
454                other_license_expression_spdx: None,
455                other_license_detections: Vec::new(),
456                extracted_license_statement: None,
457                notice_text: None,
458                source_packages: Vec::new(),
459                file_references: Vec::new(),
460                is_private: false,
461                is_virtual: false,
462                extra_data: if extra_data_map.is_empty() {
463                    None
464                } else {
465                    Some(extra_data_map)
466                },
467                dependencies: Vec::new(),
468                repository_homepage_url: None,
469                repository_download_url: None,
470                api_data_url: None,
471                datasource_id: Some(DatasourceId::JuliaManifestToml),
472                purl,
473            });
474        }
475    }
476
477    packages
478}
479
480fn extract_project_extra_data(
481    toml_content: &Value,
482) -> Option<std::collections::HashMap<String, serde_json::Value>> {
483    use serde_json::json;
484    let mut extra_data = std::collections::HashMap::new();
485
486    if let Some(uuid) = toml_content.get(FIELD_UUID).and_then(|v| v.as_str()) {
487        extra_data.insert("uuid".to_string(), json!(uuid));
488    }
489
490    if let Some(targets) = toml_content.get(FIELD_TARGETS) {
491        extra_data.insert("targets".to_string(), toml_to_json(targets));
492    }
493
494    if let Some(compat) = toml_content.get(FIELD_COMPAT) {
495        extra_data.insert("compat".to_string(), toml_to_json(compat));
496    }
497
498    if let Some(deps) = toml_content.get(FIELD_DEPS) {
499        extra_data.insert("deps".to_string(), toml_to_json(deps));
500    }
501
502    if let Some(extras) = toml_content.get("extras") {
503        extra_data.insert("extras".to_string(), toml_to_json(extras));
504    }
505
506    if let Some(sources) = toml_content.get("sources") {
507        extra_data.insert("sources".to_string(), toml_to_json(sources));
508    }
509
510    if extra_data.is_empty() {
511        None
512    } else {
513        Some(extra_data)
514    }
515}
516
517fn toml_to_json(value: &toml::Value) -> serde_json::Value {
518    toml_to_json_inner(value, &mut RecursionGuard::depth_only())
519}
520
521fn toml_to_json_inner(value: &toml::Value, guard: &mut RecursionGuard<()>) -> serde_json::Value {
522    if guard.descend() {
523        warn!("Recursion depth exceeded in toml_to_json, returning Null");
524        return serde_json::Value::Null;
525    }
526
527    let result = match value {
528        toml::Value::String(s) => serde_json::json!(s),
529        toml::Value::Integer(i) => serde_json::json!(i),
530        toml::Value::Float(f) => serde_json::json!(f),
531        toml::Value::Boolean(b) => serde_json::json!(b),
532        toml::Value::Array(a) => {
533            serde_json::Value::Array(a.iter().map(|v| toml_to_json_inner(v, guard)).collect())
534        }
535        toml::Value::Table(t) => {
536            let map: serde_json::Map<String, serde_json::Value> = t
537                .iter()
538                .map(|(k, v)| (k.clone(), toml_to_json_inner(v, guard)))
539                .collect();
540            serde_json::Value::Object(map)
541        }
542        toml::Value::Datetime(d) => serde_json::json!(d.to_string()),
543    };
544    guard.ascend();
545    result
546}
547
548fn default_project_package_data() -> PackageData {
549    PackageData {
550        package_type: Some(PackageType::Julia),
551        datasource_id: Some(DatasourceId::JuliaProjectToml),
552        ..Default::default()
553    }
554}
555
556fn is_julia_version_pinned(version_str: &str) -> bool {
557    let trimmed = version_str.trim();
558    if trimmed.is_empty() {
559        return false;
560    }
561    if trimmed.contains('^')
562        || trimmed.contains('~')
563        || trimmed.contains('>')
564        || trimmed.contains('<')
565        || trimmed.contains('*')
566    {
567        return false;
568    }
569    trimmed.matches('.').count() >= 2
570}