utility_workspaces/cargo/
mod.rs

1use crate::error::ErrorKind;
2
3use cargo_unc::commands::build_command::{build, BuildCommand};
4
5/// Builds the cargo project located at `project_path` and returns the generated wasm file contents.
6///
7/// NOTE: This function does not check whether the resulting wasm file is a valid smart
8/// contract or not.
9pub async fn compile_project(project_path: &str) -> crate::Result<Vec<u8>> {
10    let project_path = std::fs::canonicalize(project_path).map_err(|e| match e.kind() {
11        std::io::ErrorKind::NotFound => ErrorKind::Io.message(format!(
12            "Incorrect file supplied to compile_project('{}')",
13            project_path
14        )),
15        _ => ErrorKind::Io.custom(e),
16    })?;
17
18    let cargo_unc_build_command = BuildCommand {
19        no_release: false,
20        no_abi: true,
21        no_embed_abi: false,
22        no_doc: true,
23        out_dir: None,
24        manifest_path: Some(
25            cargo_unc::types::utf8_path_buf::Utf8PathBuf::from_path_buf(
26                project_path.join("Cargo.toml"),
27            )
28            .map_err(|error_path| {
29                ErrorKind::Io.custom(format!(
30                    "Unable to construct UTF-8 path from: {}",
31                    error_path.display()
32                ))
33            })?,
34        ),
35        features: None,
36        no_default_features: false,
37        color: None,
38    };
39
40    let compile_artifact =
41        build::run(cargo_unc_build_command).map_err(|e| ErrorKind::Io.custom(e))?;
42
43    let file = compile_artifact
44        .path
45        .canonicalize()
46        .map_err(|e| ErrorKind::Io.custom(e))?;
47    tokio::fs::read(file)
48        .await
49        .map_err(|e| ErrorKind::Io.custom(e))
50}