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
#![allow(clippy::new_ret_no_self)]
use std::fs;
use std::path::PathBuf;
use crate::manifest::CrateData;
use anyhow::{anyhow, bail, Context, Result};
use console::style;
use toml;
#[derive(Clone, Debug, Deserialize)]
pub struct Lockfile {
package: Vec<Package>,
}
#[derive(Clone, Debug, Deserialize)]
struct Package {
name: String,
version: String,
}
impl Lockfile {
pub fn new(crate_data: &CrateData) -> Result<Lockfile> {
let lock_path = get_lockfile_path(crate_data)?;
let lockfile = fs::read_to_string(&lock_path)
.with_context(|| anyhow!("failed to read: {}", lock_path.display()))?;
let lockfile = toml::from_str(&lockfile)
.with_context(|| anyhow!("failed to parse: {}", lock_path.display()))?;
Ok(lockfile)
}
pub fn wasm_bindgen_version(&self) -> Option<&str> {
self.get_package_version("wasm-bindgen")
}
pub fn require_wasm_bindgen(&self) -> Result<&str> {
self.wasm_bindgen_version().ok_or_else(|| {
anyhow!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
[dependencies]\n\
wasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim(),
)
})
}
pub fn wasm_bindgen_test_version(&self) -> Option<&str> {
self.get_package_version("wasm-bindgen-test")
}
fn get_package_version(&self, package: &str) -> Option<&str> {
self.package
.iter()
.find(|p| p.name == package)
.map(|p| &p.version[..])
}
}
fn get_lockfile_path(crate_data: &CrateData) -> Result<PathBuf> {
let lockfile_path = crate_data.workspace_root().join("Cargo.lock");
if !lockfile_path.is_file() {
bail!("Could not find lockfile at {:?}", lockfile_path)
} else {
Ok(lockfile_path)
}
}