hdp_primitives/task/
module.rs1use serde::{Deserialize, Serialize};
6use serde_with::serde_as;
7use starknet::core::{serde::unsigned_field_element::UfeHex, types::FromStrError};
8use starknet_crypto::FieldElement;
9use std::path::PathBuf;
10
11#[serde_as]
12#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
13#[serde(rename_all = "camelCase")]
14pub struct Module {
15 #[serde_as(as = "UfeHex")]
17 pub program_hash: FieldElement,
18 #[serde_as(as = "Vec<UfeHex>")]
19 pub inputs: Vec<FieldElement>,
20 pub local_class_path: Option<PathBuf>,
21}
22
23pub enum ModuleTag {
24 AccountBalanceExample,
25}
26
27impl Module {
28 pub fn from_tag(tag: ModuleTag, inputs: Vec<FieldElement>) -> Self {
29 let program_hash = match tag {
30 ModuleTag::AccountBalanceExample => FieldElement::from_hex_be(
31 "0x034d4ff54bc5c6cfee6719bfaa94ffa374071e8d656b74823681a955e9033dd9",
32 ),
33 }
34 .expect("Invalid module tag");
35 Self {
36 program_hash,
37 inputs,
38 local_class_path: None,
39 }
40 }
41
42 pub fn new_from_string(
43 class_hash: String,
44 inputs: Vec<String>,
45 local_class_path: Option<PathBuf>,
46 ) -> Result<Self, FromStrError> {
47 let program_hash = FieldElement::from_hex_be(&class_hash)?;
48 let inputs = inputs
49 .iter()
50 .map(|x| FieldElement::from_hex_be(x))
51 .collect::<Result<Vec<_>, _>>()?;
52 Ok(Self {
53 program_hash,
54 inputs,
55 local_class_path,
56 })
57 }
58
59 pub fn new(
60 program_hash: FieldElement,
61 inputs: Vec<FieldElement>,
62 local_class_path: Option<PathBuf>,
63 ) -> Self {
64 Self {
65 program_hash,
66 inputs,
67 local_class_path,
68 }
69 }
70
71 pub fn get_program_hash(&self) -> FieldElement {
72 self.program_hash
73 }
74
75 pub fn get_module_inputs(&self) -> Vec<FieldElement> {
76 self.inputs.clone()
77 }
78}