arcis-interface 0.9.6

Defines the data interface and core types for Arcis circuits.
Documentation
mod json;
mod string;
mod types;

pub use json::ManticoreInterface;
use std::{env, io::Write};
pub use types::{CircuitInterface, ScalarKind, Value};

pub fn write_interface(
    interface: String,
    circuit_name: &str,
    out_dir: &Option<String>,
) -> Result<(), std::io::Error> {
    use crate::string::snake_case_to_camel_case;

    const DEFAULT_CIRCUIT_OUT_DIR: &str = "build";

    let current_dir = env::current_dir()?;

    let circuits_dir = current_dir.join(out_dir.as_deref().unwrap_or(DEFAULT_CIRCUIT_OUT_DIR));
    let file_path = circuits_dir.join(format!("{circuit_name}.idarc"));
    let ts_file_path = circuits_dir.join(format!("{circuit_name}.ts"));

    std::fs::create_dir_all(&circuits_dir)?;

    let mut file = std::fs::File::create(file_path)?;
    let interface_bytes = interface.into_bytes();
    file.write_all(&interface_bytes)?;

    let mut ts_file = std::fs::File::create(ts_file_path)?;
    // Write the TypeScript interface with the circuit name in camel case (from snake case)
    let camel_case_name = snake_case_to_camel_case(circuit_name);
    ts_file.write_all(
        &[
            &format!("export type {} = ", camel_case_name).into_bytes()[..],
            &interface_bytes[..],
        ]
        .concat(),
    )?;

    Ok(())
}