|
| 1 | +use crate::regex::{DecomposedRegexConfig, RegexPart}; |
1 | 2 | use anyhow::Result; |
2 | | -use js_sys::Number; |
3 | 3 | use serde::{Deserialize, Serialize}; |
4 | 4 | use serde_json::{json, Value}; |
5 | 5 | use std::collections::VecDeque; |
6 | | -use zk_regex_apis::extract_substrs::{ |
7 | | - extract_substr_idxes, DecomposedRegexConfig, RegexPartConfig, |
8 | | -}; |
9 | 6 | use zk_regex_compiler::{gen_circuit_inputs, NFAGraph, ProverInputs, ProvingFramework}; |
10 | 7 |
|
11 | 8 | use crate::{ |
@@ -58,18 +55,112 @@ struct ClaimCircuitInput { |
58 | 55 | account_code: String, // The account code as a string |
59 | 56 | } |
60 | 57 |
|
| 58 | +/// Serializable wrapper for RegexPart that can be used in JSON files |
61 | 59 | #[derive(Serialize, Deserialize, Debug, Clone)] |
| 60 | +#[serde(untagged)] |
| 61 | +pub enum SerializableRegexPart { |
| 62 | + Pattern(String), |
| 63 | + PublicPattern((String, usize)), |
| 64 | +} |
| 65 | + |
| 66 | +impl From<SerializableRegexPart> for RegexPart { |
| 67 | + fn from(part: SerializableRegexPart) -> Self { |
| 68 | + match part { |
| 69 | + SerializableRegexPart::Pattern(p) => RegexPart::Pattern(p), |
| 70 | + SerializableRegexPart::PublicPattern((p, max)) => RegexPart::PublicPattern((p, max)), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl From<RegexPart> for SerializableRegexPart { |
| 76 | + fn from(part: RegexPart) -> Self { |
| 77 | + match part { |
| 78 | + RegexPart::Pattern(p) => SerializableRegexPart::Pattern(p), |
| 79 | + RegexPart::PublicPattern((p, max)) => SerializableRegexPart::PublicPattern((p, max)), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl From<&RegexPart> for SerializableRegexPart { |
| 85 | + fn from(part: &RegexPart) -> Self { |
| 86 | + match part { |
| 87 | + RegexPart::Pattern(p) => SerializableRegexPart::Pattern(p.clone()), |
| 88 | + RegexPart::PublicPattern((p, max)) => { |
| 89 | + SerializableRegexPart::PublicPattern((p.clone(), *max)) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[derive(Serialize, Deserialize)] |
62 | 96 | #[serde(rename_all = "camelCase")] |
63 | 97 | pub struct DecomposedRegex { |
64 | | - pub parts: Vec<RegexPartConfig>, // The parts of the regex configuration |
65 | | - pub name: String, // The name of the decomposed regex |
66 | | - pub max_match_length: usize, // The maximum length of the regex match |
67 | | - pub max_haystack_length: usize, // The maximum length of the haystack |
| 98 | + #[serde(with = "regex_parts_serde")] |
| 99 | + pub parts: Vec<RegexPart>, // The parts of the regex configuration (using new RegexPart enum) |
| 100 | + pub name: String, // The name of the decomposed regex |
| 101 | + pub max_match_length: usize, // The maximum length of the regex match |
| 102 | + pub max_haystack_length: usize, // The maximum length of the haystack |
68 | 103 | pub haystack_location: String, // The location where the regex is applied (e.g., header or body) |
69 | 104 | pub regex_graph_json: String, |
70 | 105 | pub proving_framework: ProvingFramework, |
71 | 106 | } |
72 | 107 |
|
| 108 | +impl std::fmt::Debug for DecomposedRegex { |
| 109 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 110 | + f.debug_struct("DecomposedRegex") |
| 111 | + .field("name", &self.name) |
| 112 | + .field("max_match_length", &self.max_match_length) |
| 113 | + .field("max_haystack_length", &self.max_haystack_length) |
| 114 | + .field("haystack_location", &self.haystack_location) |
| 115 | + .field("regex_graph_json", &"<json>") |
| 116 | + .field("proving_framework", &self.proving_framework) |
| 117 | + .finish() |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Clone for DecomposedRegex { |
| 122 | + fn clone(&self) -> Self { |
| 123 | + DecomposedRegex { |
| 124 | + parts: self |
| 125 | + .parts |
| 126 | + .iter() |
| 127 | + .map(|p| match p { |
| 128 | + RegexPart::Pattern(s) => RegexPart::Pattern(s.clone()), |
| 129 | + RegexPart::PublicPattern((s, n)) => RegexPart::PublicPattern((s.clone(), *n)), |
| 130 | + }) |
| 131 | + .collect(), |
| 132 | + name: self.name.clone(), |
| 133 | + max_match_length: self.max_match_length, |
| 134 | + max_haystack_length: self.max_haystack_length, |
| 135 | + haystack_location: self.haystack_location.clone(), |
| 136 | + regex_graph_json: self.regex_graph_json.clone(), |
| 137 | + proving_framework: self.proving_framework, |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Custom serde module for Vec<RegexPart> |
| 143 | +mod regex_parts_serde { |
| 144 | + use super::*; |
| 145 | + use serde::{Deserializer, Serializer}; |
| 146 | + |
| 147 | + pub fn serialize<S>(parts: &[RegexPart], serializer: S) -> Result<S::Ok, S::Error> |
| 148 | + where |
| 149 | + S: Serializer, |
| 150 | + { |
| 151 | + let serializable: Vec<SerializableRegexPart> = parts.iter().map(|p| p.into()).collect(); |
| 152 | + serializable.serialize(serializer) |
| 153 | + } |
| 154 | + |
| 155 | + pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<RegexPart>, D::Error> |
| 156 | + where |
| 157 | + D: Deserializer<'de>, |
| 158 | + { |
| 159 | + let serializable: Vec<SerializableRegexPart> = Vec::deserialize(deserializer)?; |
| 160 | + Ok(serializable.into_iter().map(Into::into).collect()) |
| 161 | + } |
| 162 | +} |
| 163 | + |
73 | 164 | /// Asynchronously generates the circuit input for an email. |
74 | 165 | /// |
75 | 166 | /// This function processes an email and its associated account code along with optional |
@@ -225,7 +316,7 @@ pub async fn generate_claim_input( |
225 | 316 | account_code: &str, |
226 | 317 | ) -> Result<String> { |
227 | 318 | // Convert the email address to a padded format |
228 | | - let padded_email_address = PaddedEmailAddr::from_email_addr(email_address); |
| 319 | + let padded_email_address = PaddedEmailAddr::from_email_addr(email_address)?; |
229 | 320 | // Collect the padded bytes into a vector |
230 | 321 | let padded_email_addr_bytes = padded_email_address.padded_bytes; |
231 | 322 |
|
|
0 commit comments