upstream-ontologist 0.3.0

tracking of upstream project metadata
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::{
    Certainty, GuesserSettings, Person, ProviderError, UpstreamDatum, UpstreamDatumWithMetadata,
    UpstreamMetadata,
};
use serde::Deserialize;
use std::collections::HashMap;

#[cfg(feature = "cargo")]
#[derive(Deserialize)]
struct CargoToml {
    package: Option<CargoPackage>,

    workspace: Option<CargoWorkspace>,
}

#[cfg(feature = "cargo")]
#[derive(Deserialize)]
struct CargoWorkspace {
    #[serde(default)]
    package: Option<CargoPackage>,
}

#[cfg(feature = "cargo")]
/// Allow either specifying setting T directly or "workspace = true"
pub enum DirectOrWorkspace<T> {
    /// Direct value specification
    Direct(T),
    /// Workspace inheritance
    Workspace,
}

#[cfg(feature = "cargo")]
impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for DirectOrWorkspace<T> {
    fn deserialize<D>(deserializer: D) -> Result<DirectOrWorkspace<T>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        // Assume deserializing T, but if that fails, check for a table with "workspace = true"
        let v: toml::value::Value = serde::Deserialize::deserialize(deserializer)?;
        match T::deserialize(v.clone()) {
            Ok(t) => Ok(DirectOrWorkspace::Direct(t)),
            Err(_) => {
                let table = v.as_table().ok_or_else(|| {
                    serde::de::Error::custom("expected either a value or a table")
                })?;
                if table.get("workspace").and_then(|v| v.as_bool()) == Some(true) {
                    Ok(DirectOrWorkspace::Workspace)
                } else {
                    Err(serde::de::Error::custom(
                        "expected either a value or a table",
                    ))
                }
            }
        }
    }
}

#[cfg(feature = "cargo")]
#[derive(Deserialize)]
struct CargoPackage {
    name: Option<String>,
    #[serde(default)]
    version: Option<DirectOrWorkspace<String>>,
    #[serde(default)]
    authors: Option<Vec<String>>,
    #[serde(default)]
    description: Option<DirectOrWorkspace<String>>,
    #[serde(default)]
    homepage: Option<DirectOrWorkspace<String>>,
    #[serde(default)]
    repository: Option<DirectOrWorkspace<String>>,
    #[serde(default)]
    license: Option<DirectOrWorkspace<String>>,
}

#[cfg(feature = "cargo")]
macro_rules! resolve {
    ($workspace:expr, $package:expr, $field:ident) => {
        match $package.$field {
            Some(DirectOrWorkspace::Direct(ref s)) => Some(s.clone()),
            Some(DirectOrWorkspace::Workspace) => {
                if let Some(DirectOrWorkspace::Direct(ref s)) =
                    $workspace.package.as_ref().and_then(|p| p.$field.as_ref())
                {
                    Some(s.clone())
                } else {
                    None
                }
            }
            None => None,
        }
    };
}

/// Extracts upstream metadata from Cargo.toml file
#[cfg(feature = "cargo")]
pub fn guess_from_cargo(
    path: &std::path::Path,
    _settings: &GuesserSettings,
) -> std::result::Result<Vec<UpstreamDatumWithMetadata>, ProviderError> {
    // see https://doc.rust-lang.org/cargo/reference/manifest.html
    let doc: CargoToml = toml::from_str(&std::fs::read_to_string(path)?)
        .map_err(|e| ProviderError::ParseError(e.to_string()))?;

    let package = match doc.package {
        Some(p) => p,
        None => {
            log::debug!("No package section in Cargo.toml");
            return Ok(Vec::new());
        }
    };

    let workspace = match doc.workspace {
        Some(w) => w,
        None => CargoWorkspace { package: None },
    };

    let mut results = Vec::new();

    if let Some(name) = package.name {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Name(name.clone()),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::CargoCrate(name),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(description) = resolve!(workspace, package, description) {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Summary(description),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(homepage) = resolve!(workspace, package, homepage) {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Homepage(homepage),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(license) = resolve!(workspace, package, license) {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::License(license),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(repository) = resolve!(workspace, package, repository) {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Repository(repository),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(version) = resolve!(workspace, package, version) {
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Version(version),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    if let Some(authors) = package.authors {
        let authors = authors.iter().map(|a| Person::from(a.as_str())).collect();
        results.push(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Author(authors),
            certainty: Some(Certainty::Certain),
            origin: Some(path.into()),
        });
    }

    Ok(results)
}

/// Translates crate names with dashes to their canonical form on crates.io
pub async fn cargo_translate_dashes(
    crate_name: &str,
) -> Result<Option<String>, crate::HTTPJSONError> {
    let url = format!("https://crates.io/api/v1/crates?q={}", crate_name)
        .parse()
        .unwrap();
    let json: serde_json::Value = crate::load_json_url(&url, None).await?;

    // Navigate through the JSON response to find the crate name.
    if let Some(crates) = json.get("crates").and_then(|c| c.as_array()) {
        for krate in crates {
            if let Some(name) = krate.get("id").and_then(|n| n.as_str()) {
                return Ok(Some(name.to_string()));
            }
        }
    }

    Ok(None)
}

/// Crate metadata from crates.io
#[derive(Deserialize)]
pub struct Crate {
    /// Crate badges
    pub badges: Vec<String>,
    /// Creation timestamp
    pub created_at: String,
    /// Crate description
    pub description: Option<String>,
    /// Documentation URL
    pub documentation: Option<String>,
    /// Total downloads
    pub downloads: i64,
    /// Homepage URL
    pub homepage: Option<String>,
    /// Crate identifier
    pub id: String,
    /// Keywords
    pub keywords: Vec<String>,
    /// License identifier
    pub license: Option<String>,
    /// Various links
    pub links: HashMap<String, Option<String>>,
    /// Maximum stable version
    pub max_stable_version: semver::Version,
    /// Maximum version
    pub max_version: semver::Version,
    /// Crate name
    pub name: String,
    /// Newest version
    pub newest_version: semver::Version,
    /// Recent downloads
    pub recent_downloads: i64,
    /// Repository URL
    pub repository: Option<String>,
    /// Last update timestamp
    pub updated_at: String,
    /// Version IDs
    pub versions: Option<Vec<i32>>,
}

/// User information from crates.io
#[derive(Deserialize)]
pub struct User {
    /// User avatar URL
    pub avatar: String,
    /// User ID
    pub id: i32,
    /// User login name
    pub login: String,
    /// User display name
    pub name: String,
    /// User profile URL
    pub url: String,
}

/// Audit action information
#[derive(Deserialize)]
pub struct AuditAction {
    /// Action type
    pub action: String,
    /// Timestamp of the action
    pub time: String,
    /// User who performed the action
    pub user: User,
}

/// Information about a specific version of a crate
#[derive(Deserialize)]
pub struct CrateVersion {
    /// Audit actions for this version
    pub audit_actions: Vec<AuditAction>,
    /// Names of binary targets
    pub bin_names: Vec<String>,
    /// Checksum of the crate
    pub checksum: String,
    /// Name of the crate
    #[serde(rename = "crate")]
    pub crate_: String,
    /// Size of the crate in bytes
    pub crate_size: i64,
    /// Creation timestamp
    pub created_at: String,
    /// Download path
    pub dl_path: String,
    /// Number of downloads
    pub downloads: i64,
    /// Feature flags
    pub features: HashMap<String, Vec<String>>,
    /// Whether the crate has a library
    pub has_lib: bool,
    /// Version ID
    pub id: i32,
    /// Library links
    pub lib_links: Option<HashMap<String, String>>,
    /// License identifier
    pub license: Option<String>,
    /// Various links
    pub links: HashMap<String, Option<String>>,
    /// Version number
    pub num: semver::Version,
    /// User who published this version
    pub published_by: Option<User>,
    /// Path to README file
    pub readme_path: String,
    /// Minimum Rust version required
    pub rust_version: Option<String>,
    /// Last update timestamp
    pub updated_at: String,
    /// Whether this version is yanked
    pub yanked: bool,
}

/// Information about a crate from crates.io
#[derive(Deserialize)]
pub struct CrateInfo {
    /// Categories the crate belongs to
    pub categories: Vec<String>,
    #[serde(rename = "crate")]
    crate_: Crate,
    /// Keywords associated with the crate
    pub keywords: Vec<String>,
    /// All versions of the crate
    pub versions: Vec<CrateVersion>,
}

impl TryFrom<CrateInfo> for UpstreamMetadata {
    type Error = crate::ProviderError;

    fn try_from(value: CrateInfo) -> Result<Self, Self::Error> {
        let mut ret = UpstreamMetadata::default();

        ret.insert(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Name(value.crate_.name.to_string()),
            certainty: Some(Certainty::Certain),
            origin: None,
        });

        if let Some(homepage) = value.crate_.homepage {
            ret.insert(UpstreamDatumWithMetadata {
                datum: UpstreamDatum::Homepage(homepage),
                certainty: Some(Certainty::Certain),
                origin: None,
            });
        }

        if let Some(repository) = value.crate_.repository {
            ret.insert(UpstreamDatumWithMetadata {
                datum: UpstreamDatum::Repository(repository),
                certainty: Some(Certainty::Certain),
                origin: None,
            });
        }

        if let Some(description) = value.crate_.description {
            ret.insert(UpstreamDatumWithMetadata {
                datum: UpstreamDatum::Summary(description),
                certainty: Some(Certainty::Certain),
                origin: None,
            });
        }

        if let Some(license) = value.crate_.license {
            ret.insert(UpstreamDatumWithMetadata {
                datum: UpstreamDatum::License(license),
                certainty: Some(Certainty::Certain),
                origin: None,
            });
        }

        ret.insert(UpstreamDatumWithMetadata {
            datum: UpstreamDatum::Version(value.crate_.newest_version.to_string()),
            certainty: Some(Certainty::Certain),
            origin: None,
        });

        Ok(ret)
    }
}

/// Loads crate information from crates.io API
pub async fn load_crate_info(cratename: &str) -> Result<Option<CrateInfo>, crate::ProviderError> {
    let http_url = format!("https://crates.io/api/v1/crates/{}", cratename);

    let data = crate::load_json_url(&http_url.parse().unwrap(), None).await?;

    Ok(Some(serde_json::from_value(data).unwrap()))
}

// TODO: dedupe with TryFrom implementation above
fn parse_crates_io(data: &CrateInfo) -> Vec<UpstreamDatum> {
    let crate_data = &data.crate_;
    let mut results = Vec::new();
    results.push(UpstreamDatum::Name(crate_data.name.to_string()));
    if let Some(homepage) = crate_data.homepage.as_ref() {
        results.push(UpstreamDatum::Homepage(homepage.to_string()));
    }
    if let Some(repository) = crate_data.repository.as_ref() {
        results.push(UpstreamDatum::Repository(repository.to_string()));
    }
    if let Some(description) = crate_data.description.as_ref() {
        results.push(UpstreamDatum::Summary(description.to_string()));
    }
    if let Some(license) = crate_data.license.as_ref() {
        results.push(UpstreamDatum::License(license.to_string()));
    }
    results.push(UpstreamDatum::Version(
        crate_data.newest_version.to_string(),
    ));

    results
}

/// Crates.io metadata provider
pub struct CratesIo;

impl Default for CratesIo {
    fn default() -> Self {
        Self::new()
    }
}

impl CratesIo {
    /// Creates a new CratesIo provider
    pub fn new() -> Self {
        Self
    }
}

#[async_trait::async_trait]
impl crate::ThirdPartyRepository for CratesIo {
    fn name(&self) -> &'static str {
        "crates.io"
    }

    fn max_supported_certainty(&self) -> Certainty {
        Certainty::Certain
    }

    fn supported_fields(&self) -> &'static [&'static str] {
        &["Homepage", "Name", "Repository", "Version", "Summary"][..]
    }

    async fn guess_metadata(&self, name: &str) -> Result<Vec<UpstreamDatum>, ProviderError> {
        let data = load_crate_info(name).await?;
        if data.is_none() {
            return Ok(Vec::new());
        }
        Ok(parse_crates_io(&data.unwrap()))
    }
}

/// Fetches upstream metadata for a crate from crates.io
pub async fn remote_crate_data(name: &str) -> Result<UpstreamMetadata, crate::ProviderError> {
    let data = load_crate_info(name).await?;

    if let Some(data) = data {
        Ok(data.try_into()?)
    } else {
        Ok(UpstreamMetadata::default())
    }
}

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

    #[test]
    fn test_load_crate_info() {
        let data = include_str!("../testdata/crates.io.json");

        let crate_info: CrateInfo = serde_json::from_str(data).unwrap();

        assert_eq!(crate_info.crate_.name, "breezy");
    }
}