Skip to content

Commit

Permalink
feat: Rust CLI improvements and HTTP lockfile example (#72)
Browse files Browse the repository at this point in the history
* feat: combined CLI

* refactor and add example http lock

* http lock example

* include `search/witness.json`

* fix tests

* clean up basic lock file
  • Loading branch information
Autoparallel authored Sep 3, 2024
1 parent 5d6aac5 commit cc47938
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 225 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ circuits/test/*.circom
circuits/main/*

# Rust generated
inputs/**/*.json
inputs/**/*.json
!inputs/search/witness.json
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "witness"
name = "wpbuild"
edition = "2021"

[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
clap = { version = "4.5.14", features = ["derive"] }
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
clap = { version = "4.5.16", features = ["derive"] }
4 changes: 2 additions & 2 deletions circuits/test/json/extractor/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { spawn } from "child_process";

function executeCodegen(inputFilename: string, outputFilename: string) {
return new Promise((resolve, reject) => {
const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "json", "test", "codegen", inputFilename);
const inputPath = join(__dirname, "..", "..", "..", "..", "examples", "extractor", inputFilename);

const codegen = spawn("cargo", ["run", "--bin", "codegen", "--", "--json-file", inputPath, "--output-filename", outputFilename]);
const codegen = spawn("cargo", ["run", "extractor", "--template", inputPath, "--output-filename", outputFilename]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions examples/lockfile/test.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"request": {
"method": "GET",
"target": "/api",
"version": "HTTP/1.1",
"headers": [
[
"Host",
"localhost"
],
[
"Accept",
"application/json"
]
]
},
"response": {
"version": "HTTP/1.1",
"status": "200",
"headers": [
[
"Content-Type",
"application/json"
]
]
}
}
51 changes: 0 additions & 51 deletions notes.md

This file was deleted.

130 changes: 0 additions & 130 deletions src/bin/witness.rs

This file was deleted.

39 changes: 13 additions & 26 deletions src/bin/codegen.rs → src/extractor.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::fs::{self, create_dir_all};
use std::path::PathBuf;
use std::str::FromStr;

#[derive(Parser, Debug)]
#[command(name = "codegen")]
struct Args {
/// Path to the JSON file
#[arg(short, long)]
json_file: PathBuf,

/// Output circuit file name
#[arg(short, long, default_value = "extractor")]
output_filename: String,
}
use std::{
fs::{self, create_dir_all},
str::FromStr,
};

use super::*;

const PRAGMA: &str = "pragma circom 2.1.9;\n\n";

#[derive(Debug, Deserialize)]
enum ValueType {
pub enum ValueType {
#[serde(rename = "string")]
String,
#[serde(rename = "number")]
Expand All @@ -26,19 +17,17 @@ enum ValueType {

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Key {
pub enum Key {
String(String),
Num(i64),
}

#[derive(Debug, Deserialize)]
struct Data {
pub struct Data {
keys: Vec<Key>,
value_type: ValueType,
}

const PRAGMA: &str = "pragma circom 2.1.9;\n\n";

fn extract_string(data: Data, circuit_buffer: &mut String) {
*circuit_buffer += "template ExtractStringValue(DATA_BYTES, MAX_STACK_HEIGHT, ";
for (i, key) in data.keys.iter().enumerate() {
Expand Down Expand Up @@ -500,10 +489,8 @@ fn parse_json_request(
Ok(())
}

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();

let data = std::fs::read(&args.json_file)?;
pub fn extractor(args: ExtractorArgs) -> Result<(), Box<dyn std::error::Error>> {
let data = std::fs::read(&args.template)?;
let json_data: Data = serde_json::from_slice(&data)?;

parse_json_request(json_data, args.output_filename)?;
Expand Down
Loading

0 comments on commit cc47938

Please sign in to comment.