unc_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        release: true,
20        embed_abi: true,
21        doc: false,
22        color: None,
23        no_abi: true,
24        out_dir: None,
25        manifest_path: Some(
26            cargo_unc::types::utf8_path_buf::Utf8PathBuf::from_path_buf(
27                project_path.join("Cargo.toml"),
28            )
29            .map_err(|error_path| {
30                ErrorKind::Io.custom(format!(
31                    "Unable to construct UTF-8 path from: {}",
32                    error_path.display()
33                ))
34            })?,
35        ),
36    };
37
38    let compile_artifact =
39        build::run(cargo_unc_build_command).map_err(|e| ErrorKind::Io.custom(e))?;
40
41    let file = compile_artifact
42        .path
43        .canonicalize()
44        .map_err(|e| ErrorKind::Io.custom(e))?;
45    tokio::fs::read(file)
46        .await
47        .map_err(|e| ErrorKind::Io.custom(e))
48}