Skip to main content

arcis_interface/
lib.rs

1mod json;
2pub mod path_with_hash;
3mod string;
4mod types;
5
6use crate::path_with_hash::PathWithHash;
7pub use json::ManticoreInterface;
8use std::env;
9pub use types::{CircuitInterface, ScalarKind, Value};
10
11pub fn write_interface(
12    interface: String,
13    circuit_name: &str,
14    out_dir: &Option<String>,
15) -> Result<[PathWithHash; 2], std::io::Error> {
16    use crate::string::snake_case_to_camel_case;
17
18    const DEFAULT_CIRCUIT_OUT_DIR: &str = "build";
19
20    let current_dir = env::current_dir()?;
21
22    let circuits_dir = current_dir.join(out_dir.as_deref().unwrap_or(DEFAULT_CIRCUIT_OUT_DIR));
23    let file_path = circuits_dir.join(format!("{circuit_name}.idarc"));
24    let ts_file_path = circuits_dir.join(format!("{circuit_name}.ts"));
25
26    std::fs::create_dir_all(&circuits_dir)?;
27
28    let interface_bytes = interface.into_bytes();
29    let idarc_file = PathWithHash::new(
30        &interface_bytes,
31        file_path
32            .into_os_string()
33            .into_string()
34            .expect("current directory contains non-UTF-8 characters"),
35    )?;
36
37    // Write the TypeScript interface with the circuit name in camel case (from snake case)
38    let camel_case_name = snake_case_to_camel_case(circuit_name);
39    let ts_bytes = [
40        &format!("export type {} = ", camel_case_name).into_bytes()[..],
41        &interface_bytes[..],
42    ]
43    .concat();
44    let ts_file = PathWithHash::new(
45        &ts_bytes,
46        ts_file_path
47            .into_os_string()
48            .into_string()
49            .expect("current directory contains non-UTF-8 characters"),
50    )?;
51
52    Ok([idarc_file, ts_file])
53}