starknet_abigen_parser_stopgap/
cairo_enum.rsuse std::collections::HashMap;
use starknet::core::types::contract::AbiNamedMember;
use super::abi_types::{AbiType, AbiTypeAny};
#[derive(Debug, Clone)]
pub struct CairoEnum {
pub abi: AbiTypeAny,
pub variants: Vec<(String, AbiTypeAny)>,
pub generic_variants: HashMap<String, (String, bool)>,
}
impl CairoEnum {
pub fn get_name(&self) -> String {
self.abi.get_cairo_type_name()
}
pub fn is_generic(&self) -> bool {
matches!(self.abi, AbiTypeAny::Generic(_))
}
pub fn get_gentys(&self) -> Vec<String> {
if let AbiTypeAny::Generic(g) = &self.abi {
g.get_gentys_only()
} else {
vec![]
}
}
pub fn new(abi_name: &str, abi_variants: &Vec<AbiNamedMember>) -> CairoEnum {
let abi = AbiTypeAny::from_string(abi_name);
let mut variants: Vec<(String, AbiTypeAny)> = vec![];
let mut generic_variants: HashMap<String, (String, bool)> = HashMap::new();
for v in abi_variants {
let name = v.name.clone();
let mut v_abi = AbiTypeAny::from_string(&v.r#type.clone());
if let AbiTypeAny::Generic(ref g) = abi {
let cairo_gentys = g.get_cairo_types_gentys();
let cairo_gentys = cairo_gentys
.iter()
.map(|(v1, v2)| (&v1[..], &v2[..]))
.collect();
let (type_str, is_generic) = v_abi.apply_generic(cairo_gentys);
generic_variants.insert(name.clone(), (type_str.clone(), is_generic));
}
variants.push((name.clone(), v_abi.clone()));
}
CairoEnum {
abi,
variants,
generic_variants,
}
}
pub fn compare_generic_types(&self, existing_ce: &mut CairoEnum) {
if let AbiTypeAny::Generic(_) = &self.abi {
for (ev_name, ev_abi) in &mut existing_ce.variants {
for (v_name, v_abi) in &self.variants {
if v_name != ev_name {
continue;
}
ev_abi.compare_generic(v_abi);
}
}
}
}
}