commonmeta 0.2.0

Library for conversions to/from the Commonmeta scholarly metadata format
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use serde_yaml::Value;

use crate::data::{Affiliation, Contributor, Data, Date, Description, License, Publisher, Reference, Subject, Title};
use crate::doi_utils::normalize_doi;
use crate::error::{Error, Result};
use crate::utils::{normalize_id, normalize_orcid, sanitize};

// ── YAML value helpers ────────────────────────────────────────────────────────

fn val_str(v: &Value) -> &str {
    match v {
        Value::String(s) => s.as_str(),
        _ => "",
    }
}

fn val_str_owned(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        _ => String::new(),
    }
}

fn val_seq(v: &Value) -> &[Value] {
    match v {
        Value::Sequence(s) => s.as_slice(),
        _ => &[],
    }
}

static NULL_VAL: std::sync::OnceLock<Value> = std::sync::OnceLock::new();

fn null_val() -> &'static Value {
    NULL_VAL.get_or_init(|| Value::Null)
}

fn get<'a>(v: &'a Value, key: &str) -> &'a Value {
    match v {
        Value::Mapping(m) => m
            .get(&Value::String(key.to_string()))
            .unwrap_or(null_val()),
        _ => null_val(),
    }
}

// ── GitHub URL utilities ──────────────────────────────────────────────────────

struct GithubParts {
    owner: String,
    repo: String,
    release: String, // branch or tag, defaults to "main"
    path: String,    // sub-path within the repo
}

fn github_from_url(url: &str) -> Option<GithubParts> {
    let parsed = url::Url::parse(url).ok()?;
    let host = parsed.host_str()?;
    if !host.ends_with("github.com") && !host.ends_with("githubusercontent.com") {
        return None;
    }
    let words: Vec<&str> = parsed
        .path()
        .trim_start_matches('/')
        .split('/')
        .collect();
    let owner = words.first().copied().filter(|s| !s.is_empty())?.to_string();
    let repo = words.get(1).copied().filter(|s| !s.is_empty())?.to_string();
    // GitHub web URLs: owner/repo/tree/<branch>/<path...>
    //                  words: [0]=owner [1]=repo [2]="tree"|"blob" [3]=branch [4+]=path
    let release = words
        .get(3)
        .copied()
        .filter(|s| !s.is_empty())
        .map(|s| s.to_string())
        .unwrap_or_else(|| "main".to_string());
    let path = if words.len() > 4 {
        words[4..].join("/")
    } else {
        String::new()
    };
    Some(GithubParts { owner, repo, release, path })
}

/// Convert any GitHub URL to the raw CITATION.cff download URL.
fn github_as_cff_url(url: &str) -> Option<String> {
    let p = github_from_url(url)?;
    if !p.path.is_empty() && p.path.ends_with("CITATION.cff") {
        Some(format!(
            "https://raw.githubusercontent.com/{}/{}/{}/{}",
            p.owner, p.repo, p.release, p.path
        ))
    } else {
        Some(format!(
            "https://raw.githubusercontent.com/{}/{}/main/CITATION.cff",
            p.owner, p.repo
        ))
    }
}

/// Convert any GitHub-related URL to the canonical repo URL.
fn github_as_repo_url(url: &str) -> Option<String> {
    let p = github_from_url(url)?;
    Some(format!("https://github.com/{}/{}", p.owner, p.repo))
}

// ── Contributor parsing ───────────────────────────────────────────────────────

fn parse_cff_contributors(authors: &[Value]) -> Vec<Contributor> {
    authors
        .iter()
        .map(|author| {
            let family_name = val_str(get(author, "family-names")).to_string();
            let given_name = val_str(get(author, "given-names")).to_string();
            let orcid_raw = val_str(get(author, "orcid")).to_string();
            let orcid = if !orcid_raw.is_empty() {
                let n = normalize_orcid(&orcid_raw);
                if n.is_empty() { None } else { Some(n) }
            } else {
                None
            };

            if !family_name.is_empty() || !given_name.is_empty() || orcid.is_some() {
                // Person
                let affiliations: Vec<Affiliation> = match get(author, "affiliation") {
                    Value::String(s) if !s.is_empty() => vec![Affiliation {
                        name: s.clone(),
                        ..Default::default()
                    }],
                    Value::Sequence(seq) => seq
                        .iter()
                        .filter_map(|a| {
                            let name = val_str(a).to_string();
                            if name.is_empty() { None } else {
                                Some(Affiliation { name, ..Default::default() })
                            }
                        })
                        .collect(),
                    _ => vec![],
                };
                Contributor {
                    id: orcid.unwrap_or_default(),
                    type_: "Person".to_string(),
                    given_name,
                    family_name,
                    affiliations,
                    contributor_roles: vec!["Author".to_string()],
                    ..Default::default()
                }
            } else {
                // Organization
                let name = val_str(get(author, "name")).to_string();
                Contributor {
                    type_: "Organization".to_string(),
                    name,
                    contributor_roles: vec!["Author".to_string()],
                    ..Default::default()
                }
            }
        })
        .collect()
}

// ── Reference parsing ─────────────────────────────────────────────────────────

fn parse_cff_references(references: &[Value]) -> Vec<Reference> {
    references
        .iter()
        .filter_map(|r| {
            // CFF spec field is "identifiers"; look for a DOI entry
            let identifiers = val_seq(get(r, "identifiers"));
            let doi_entry = identifiers.iter().find(|id| {
                val_str(get(id, "type")) == "doi"
            })?;
            let value = val_str(get(doi_entry, "value"));
            if value.is_empty() {
                return None;
            }
            let id = normalize_doi(value);
            if id.is_empty() {
                return None;
            }
            Some(Reference {
                id,
                ..Default::default()
            })
        })
        .collect()
}

// ── Core reader ───────────────────────────────────────────────────────────────

fn from_value(doc: &Value) -> Data {
    // ID from doi field (raw DOI suffix or URL)
    let doi_raw = val_str_owned(get(doc, "doi"));
    let id = if !doi_raw.is_empty() {
        normalize_doi(&doi_raw)
    } else {
        String::new()
    };

    // Repository URL
    let repo_code = val_str(get(doc, "repository-code")).to_string();
    let url = if !repo_code.is_empty() {
        normalize_id(&repo_code)
    } else {
        String::new()
    };

    // Publisher: GitHub if the repository URL is on github.com
    let publisher = if url.contains("github.com") {
        Publisher {
            name: "GitHub".to_string(),
            ..Default::default()
        }
    } else {
        Publisher::default()
    };

    // Title
    let title = val_str(get(doc, "title")).to_string();
    let titles = if !title.is_empty() {
        vec![Title { title, ..Default::default() }]
    } else {
        vec![]
    };

    // Contributors from `authors`
    let contributors = parse_cff_contributors(val_seq(get(doc, "authors")));

    // Date from `date-released`
    let date_released = val_str_owned(get(doc, "date-released"));
    let date = if !date_released.is_empty() {
        Date {
            published: date_released,
            ..Default::default()
        }
    } else {
        Date::default()
    };

    // Abstract
    let abstract_text = val_str(get(doc, "abstract"));
    let descriptions = if !abstract_text.is_empty() {
        vec![Description {
            description: sanitize(abstract_text),
            type_: "Abstract".to_string(),
            ..Default::default()
        }]
    } else {
        vec![]
    };

    // License: can be a single SPDX ID string or a list — take first
    let license_val = get(doc, "license");
    let license_id = match license_val {
        Value::String(s) => s.clone(),
        Value::Sequence(seq) => seq
            .first()
            .and_then(|v| if let Value::String(s) = v { Some(s.clone()) } else { None })
            .unwrap_or_default(),
        _ => String::new(),
    };
    let license = if !license_id.is_empty() {
        License { id: license_id, ..Default::default() }
    } else {
        License::default()
    };

    // Version
    let version = val_str_owned(get(doc, "version"));

    // Keywords → subjects
    let subjects: Vec<Subject> = val_seq(get(doc, "keywords"))
        .iter()
        .map(|k| Subject { subject: val_str(k).to_string() })
        .filter(|s| !s.subject.is_empty())
        .collect();

    // References
    let references = parse_cff_references(val_seq(get(doc, "references")));

    Data {
        id,
        type_: "Software".to_string(),
        url,
        titles,
        contributors,
        date,
        descriptions,
        license,
        version,
        subjects,
        references,
        publisher,
        ..Data::default()
    }
}

// ── Public API ────────────────────────────────────────────────────────────────

pub fn read_yaml(input: &str) -> Result<Data> {
    let doc: Value = serde_yaml::from_str(input).map_err(|e| Error::Parse(e.to_string()))?;
    Ok(from_value(&doc))
}

/// Fetch a CITATION.cff from a GitHub repository URL and parse it.
pub fn fetch(url: &str) -> Result<Data> {
    let cff_url = github_as_cff_url(url)
        .ok_or_else(|| Error::Parse(format!("cannot derive CITATION.cff URL from: {}", url)))?;

    let client = reqwest::blocking::Client::builder()
        .user_agent(format!(
            "commonmeta-rs/{} (https://github.com/front-matter/commonmeta-rs; mailto:info@front-matter.de)",
            env!("CARGO_PKG_VERSION")
        ))
        .build()
        .map_err(|e| Error::Http(e.to_string()))?;

    let text = client
        .get(&cff_url)
        .send()
        .map_err(|e| Error::Http(e.to_string()))?
        .error_for_status()
        .map_err(|e| Error::Http(e.to_string()))?
        .text()
        .map_err(|e| Error::Http(e.to_string()))?;

    let mut doc: Value =
        serde_yaml::from_str(&text).map_err(|e| Error::Parse(e.to_string()))?;

    // If repository-code is absent, fill it from the canonical repo URL
    if get(&doc, "repository-code") == null_val() {
        if let Some(repo_url) = github_as_repo_url(&cff_url) {
            if let Value::Mapping(ref mut m) = doc {
                m.insert(
                    Value::String("repository-code".to_string()),
                    Value::String(repo_url),
                );
            }
        }
    }

    Ok(from_value(&doc))
}

#[cfg(test)]
mod tests {
    use super::*;

    const CFF_SOFTWARE: &str = r#"
cff-version: 1.2.0
title: My Research Software
authors:
  - family-names: Smith
    given-names: John
    orcid: https://orcid.org/0000-0002-1825-0097
    affiliation: University of Example
  - name: ACME Research Group
doi: 10.5281/zenodo.1234567
version: 2.1.0
date-released: 2024-03-15
abstract: A software tool for research.
license: MIT
keywords:
  - research
  - data science
repository-code: https://github.com/example/my-software
"#;

    #[test]
    fn test_read_cff_basic() {
        let data = read_yaml(CFF_SOFTWARE).unwrap();

        assert_eq!(data.type_, "Software");
        assert_eq!(data.id, "https://doi.org/10.5281/zenodo.1234567");
        assert_eq!(data.url, "https://github.com/example/my-software");
        assert_eq!(data.titles[0].title, "My Research Software");
        assert_eq!(data.version, "2.1.0");
        assert_eq!(data.date.published, "2024-03-15");
        assert_eq!(data.license.id, "MIT");
        assert_eq!(data.publisher.name, "GitHub");
    }

    #[test]
    fn test_cff_contributors() {
        let data = read_yaml(CFF_SOFTWARE).unwrap();

        assert_eq!(data.contributors.len(), 2);

        let person = &data.contributors[0];
        assert_eq!(person.type_, "Person");
        assert_eq!(person.family_name, "Smith");
        assert_eq!(person.given_name, "John");
        assert_eq!(person.id, "https://orcid.org/0000-0002-1825-0097");
        assert_eq!(person.affiliations[0].name, "University of Example");

        let org = &data.contributors[1];
        assert_eq!(org.type_, "Organization");
        assert_eq!(org.name, "ACME Research Group");
    }

    #[test]
    fn test_cff_subjects() {
        let data = read_yaml(CFF_SOFTWARE).unwrap();
        assert_eq!(data.subjects.len(), 2);
        assert_eq!(data.subjects[0].subject, "research");
        assert_eq!(data.subjects[1].subject, "data science");
    }

    #[test]
    fn test_cff_description() {
        let data = read_yaml(CFF_SOFTWARE).unwrap();
        assert_eq!(data.descriptions.len(), 1);
        assert_eq!(data.descriptions[0].description, "A software tool for research.");
        assert_eq!(data.descriptions[0].type_, "Abstract");
    }

    #[test]
    fn test_github_as_cff_url() {
        assert_eq!(
            github_as_cff_url("https://github.com/owner/repo"),
            Some("https://raw.githubusercontent.com/owner/repo/main/CITATION.cff".to_string())
        );
        assert_eq!(
            github_as_cff_url("https://github.com/owner/repo/tree/v1.0/CITATION.cff"),
            Some("https://raw.githubusercontent.com/owner/repo/v1.0/CITATION.cff".to_string())
        );
    }

    #[test]
    fn test_cff_references() {
        let cff = r#"
cff-version: 1.2.0
title: Test
authors:
  - name: Author
references:
  - type: article
    title: Related paper
    identifiers:
      - type: doi
        value: 10.1000/ref.2024
  - type: book
    title: No DOI book
"#;
        let data = read_yaml(cff).unwrap();
        assert_eq!(data.references.len(), 1);
        assert_eq!(data.references[0].id, "https://doi.org/10.1000/ref.2024");
    }

    #[test]
    fn test_cff_license_list() {
        let cff = r#"
cff-version: 1.2.0
title: Test
authors:
  - name: Author
license:
  - Apache-2.0
  - MIT
"#;
        let data = read_yaml(cff).unwrap();
        assert_eq!(data.license.id, "Apache-2.0");
    }
}