commonmeta 0.2.3

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
use serde_json::Value;

use crate::data::{Data, Date, Description, License, Publisher, Subject, Title};
use crate::error::{Error, Result};
use crate::utils::{normalize_id, sanitize};

use super::schemaorg::{get_contributor, so_to_cm_type, value_to_contributors};

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

fn github_from_url(url: &str) -> Option<(String, String, Option<String>, Option<String>)> {
    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());
    let path = if words.len() > 4 {
        let p = words[4..].join("/");
        if p.is_empty() { None } else { Some(p) }
    } else {
        None
    };
    Some((owner, repo, release, path))
}

/// Convert any GitHub URL to the raw codemeta.json download URL.
/// Defaults to the `main` branch.
fn github_as_codemeta_url(url: &str) -> Option<String> {
    let (owner, repo, release, path) = github_from_url(url)?;
    if let Some(p) = &path
        && p.ends_with("codemeta.json") {
            let branch = release.as_deref().unwrap_or("main");
            return Some(format!(
                "https://raw.githubusercontent.com/{}/{}/{}/{}",
                owner, repo, branch, p
            ));
        }
    Some(format!(
        "https://raw.githubusercontent.com/{}/{}/main/codemeta.json",
        owner, repo
    ))
}

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

// ── JSON value helpers ────────────────────────────────────────────────────────

fn str_field<'a>(v: &'a Value, key: &str) -> &'a str {
    v.get(key).and_then(|f| f.as_str()).unwrap_or("")
}

fn str_field_owned(v: &Value, key: &str) -> String {
    match v.get(key) {
        Some(Value::String(s)) => s.clone(),
        Some(Value::Number(n)) => n.to_string(),
        _ => String::new(),
    }
}

// ── Publisher parsing ─────────────────────────────────────────────────────────

fn parse_publisher(v: &Value) -> Publisher {
    match v.get("publisher") {
        Some(Value::String(s)) if !s.is_empty() => Publisher {
            name: s.clone(),
            ..Default::default()
        },
        Some(obj) if obj.is_object() => {
            let name = obj
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or("")
                .to_string();
            Publisher { name, ..Default::default() }
        }
        _ => Publisher::default(),
    }
}

// ── Keyword / subject parsing ─────────────────────────────────────────────────

fn parse_keywords(v: &Value) -> Vec<Subject> {
    match v.get("keywords") {
        Some(Value::String(s)) => s
            .split(',')
            .map(|k| Subject { subject: k.trim().to_string() })
            .filter(|s| !s.subject.is_empty())
            .collect(),
        Some(Value::Array(arr)) => arr
            .iter()
            .filter_map(|k| k.as_str())
            .map(|k| Subject { subject: k.trim().to_string() })
            .filter(|s| !s.subject.is_empty())
            .collect(),
        _ => vec![],
    }
}

// ── Core conversion ───────────────────────────────────────────────────────────

fn from_value(doc: &Value) -> Data {
    // ID — from @id or identifier field
    let id_raw = {
        let from_id = str_field(doc, "@id");
        let from_identifier = str_field(doc, "identifier");
        if !from_id.is_empty() { from_id } else { from_identifier }
    };
    let id = normalize_id(id_raw);

    // Type — @type via SO_TO_CM_TRANSLATIONS; default "Software"
    let type_raw = str_field(doc, "@type");
    let type_ = {
        let mapped = so_to_cm_type(type_raw);
        if mapped.is_empty() { "Software" } else { mapped }
    }
    .to_string();

    // URL — from codeRepository
    let url = normalize_id(str_field(doc, "codeRepository"));

    // Title — prefer "title", fall back to "name"
    let title = {
        let t = str_field(doc, "title");
        if !t.is_empty() { t } else { str_field(doc, "name") }
    }
    .to_string();
    let titles = if !title.is_empty() {
        vec![Title { title, ..Default::default() }]
    } else {
        vec![]
    };

    // Contributors — "agents" takes priority over "authors"
    let author_val = doc
        .get("agents")
        .filter(|v| !v.is_null())
        .or_else(|| doc.get("authors"))
        .filter(|v| !v.is_null())
        .cloned()
        .unwrap_or(Value::Null);
    let mut contributors: Vec<_> = value_to_contributors(&author_val)
        .into_iter()
        .map(|c| get_contributor(c, "Author"))
        .collect();

    // Editors
    if let Some(ed_val) = doc.get("editor").filter(|v| !v.is_null()) {
        let editors: Vec<_> = value_to_contributors(ed_val)
            .into_iter()
            .map(|c| get_contributor(c, "Editor"))
            .collect();
        contributors.extend(editors);
    }

    // Dates
    let date = Date {
        created: str_field(doc, "dateCreated").to_string(),
        published: str_field(doc, "datePublished").to_string(),
        updated: str_field(doc, "dateModified").to_string(),
        ..Default::default()
    };

    // Publisher
    let publisher = parse_publisher(doc);

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

    // License — codemeta uses "licenseId" as an SPDX identifier
    let license_id = str_field(doc, "licenseId").to_string();
    let license = if !license_id.is_empty() {
        License { id: license_id, ..Default::default() }
    } else {
        License::default()
    };

    // Version
    let version = str_field_owned(doc, "version");

    // Keywords → subjects
    let subjects = parse_keywords(doc);

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

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

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

/// Fetch a codemeta.json from a GitHub repository URL and parse it.
pub fn fetch(url: &str) -> Result<Data> {
    let codemeta_url = github_as_codemeta_url(url).ok_or_else(|| {
        Error::Parse(format!("cannot derive codemeta.json 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 mut doc: Value = client
        .get(&codemeta_url)
        .send()
        .map_err(|e| Error::Http(e.to_string()))?
        .error_for_status()
        .map_err(|e| Error::Http(e.to_string()))?
        .json()
        .map_err(|e| Error::Parse(e.to_string()))?;

    // If codeRepository is absent, fill from the canonical repo URL
    if doc.get("codeRepository").is_none_or(|v| v.is_null())
        && let Some(repo_url) = github_as_repo_url(&codemeta_url) {
            doc["codeRepository"] = Value::String(repo_url);
        }

    Ok(from_value(&doc))
}

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

    const CODEMETA_SOFTWARE: &str = r#"{
  "@context": "https://doi.org/10.5063/schema/codemeta-2.0",
  "@type": "SoftwareSourceCode",
  "@id": "https://doi.org/10.5281/zenodo.6154694",
  "name": "Commonmeta Ruby",
  "authors": [
    {
      "@type": "Person",
      "@id": "https://orcid.org/0000-0003-1419-2405",
      "givenName": "Martin",
      "familyName": "Fenner",
      "affiliation": {
        "@type": "Organization",
        "name": "Front Matter"
      }
    }
  ],
  "editor": [
    {
      "@type": "Person",
      "givenName": "Jane",
      "familyName": "Doe"
    }
  ],
  "dateCreated": "2022-01-01",
  "datePublished": "2022-02-15",
  "dateModified": "2023-05-10",
  "description": "Ruby library for conversion of scholarly metadata.",
  "licenseId": "MIT",
  "version": "2.1.0",
  "keywords": ["metadata", "scholarly"],
  "codeRepository": "https://github.com/front-matter/commonmeta-ruby",
  "publisher": "GitHub"
}"#;

    #[test]
    fn test_read_codemeta_basic() {
        let data = read_json(CODEMETA_SOFTWARE).unwrap();

        assert_eq!(data.type_, "Software");
        assert_eq!(data.id, "https://doi.org/10.5281/zenodo.6154694");
        assert_eq!(data.url, "https://github.com/front-matter/commonmeta-ruby");
        assert_eq!(data.titles[0].title, "Commonmeta Ruby");
        assert_eq!(data.version, "2.1.0");
        assert_eq!(data.license.id, "MIT");
        assert_eq!(data.publisher.name, "GitHub");
    }

    #[test]
    fn test_codemeta_dates() {
        let data = read_json(CODEMETA_SOFTWARE).unwrap();
        assert_eq!(data.date.created, "2022-01-01");
        assert_eq!(data.date.published, "2022-02-15");
        assert_eq!(data.date.updated, "2023-05-10");
    }

    #[test]
    fn test_codemeta_contributors() {
        let data = read_json(CODEMETA_SOFTWARE).unwrap();

        // Author
        assert_eq!(data.contributors.len(), 2);
        let author = &data.contributors[0];
        assert_eq!(author.type_, "Person");
        assert_eq!(author.given_name, "Martin");
        assert_eq!(author.family_name, "Fenner");
        assert_eq!(author.id, "https://orcid.org/0000-0003-1419-2405");
        assert_eq!(author.affiliations[0].name, "Front Matter");
        assert!(author.contributor_roles.contains(&"Author".to_string()));

        // Editor
        let editor = &data.contributors[1];
        assert_eq!(editor.family_name, "Doe");
        assert!(editor.contributor_roles.contains(&"Editor".to_string()));
    }

    #[test]
    fn test_codemeta_subjects() {
        let data = read_json(CODEMETA_SOFTWARE).unwrap();
        assert_eq!(data.subjects.len(), 2);
        assert_eq!(data.subjects[0].subject, "metadata");
        assert_eq!(data.subjects[1].subject, "scholarly");
    }

    #[test]
    fn test_codemeta_description() {
        let data = read_json(CODEMETA_SOFTWARE).unwrap();
        assert_eq!(data.descriptions.len(), 1);
        assert_eq!(
            data.descriptions[0].description,
            "Ruby library for conversion of scholarly metadata."
        );
        assert_eq!(data.descriptions[0].type_, "Abstract");
    }

    #[test]
    fn test_codemeta_agents_priority() {
        let json = r#"{
  "@type": "SoftwareSourceCode",
  "name": "Test",
  "agents": [{"@type": "Person", "givenName": "A", "familyName": "Agent"}],
  "authors": [{"@type": "Person", "givenName": "B", "familyName": "Author"}]
}"#;
        let data = read_json(json).unwrap();
        // agents takes priority over authors
        assert_eq!(data.contributors[0].family_name, "Agent");
    }

    #[test]
    fn test_codemeta_title_fallback() {
        // "title" takes priority; falls back to "name"
        let with_title = r#"{"@type":"SoftwareSourceCode","title":"Title Field","name":"Name Field"}"#;
        let data = read_json(with_title).unwrap();
        assert_eq!(data.titles[0].title, "Title Field");

        let with_name_only = r#"{"@type":"SoftwareSourceCode","name":"Name Field"}"#;
        let data = read_json(with_name_only).unwrap();
        assert_eq!(data.titles[0].title, "Name Field");
    }

    #[test]
    fn test_codemeta_keyword_string() {
        let json = r#"{"@type":"SoftwareSourceCode","name":"T","keywords":"foo, bar, baz"}"#;
        let data = read_json(json).unwrap();
        assert_eq!(data.subjects.len(), 3);
        assert_eq!(data.subjects[0].subject, "foo");
    }

    #[test]
    fn test_github_as_codemeta_url() {
        assert_eq!(
            github_as_codemeta_url("https://github.com/owner/repo"),
            Some("https://raw.githubusercontent.com/owner/repo/main/codemeta.json".to_string())
        );
        assert_eq!(
            github_as_codemeta_url("https://github.com/owner/repo/tree/v1.0/codemeta.json"),
            Some("https://raw.githubusercontent.com/owner/repo/v1.0/codemeta.json".to_string())
        );
    }
}