circomspect_program_structure/program_library/
program_archive.rs

1use super::ast::{Definition, Expression, MainComponent};
2use super::file_definition::{FileID, FileLibrary};
3use super::function_data::{FunctionData, FunctionInfo};
4use super::program_merger::Merger;
5use super::template_data::{TemplateData, TemplateInfo};
6use crate::abstract_syntax_tree::ast::FillMeta;
7use crate::report::Report;
8use std::collections::{HashMap, HashSet};
9
10type Contents = HashMap<FileID, Vec<Definition>>;
11
12#[derive(Clone)]
13pub struct ProgramArchive {
14    pub id_max: usize,
15    pub file_id_main: FileID,
16    pub file_library: FileLibrary,
17    pub functions: FunctionInfo,
18    pub templates: TemplateInfo,
19    pub function_keys: HashSet<String>,
20    pub template_keys: HashSet<String>,
21    pub public_inputs: Vec<String>,
22    pub initial_template_call: Expression,
23    pub custom_gates: bool,
24}
25impl ProgramArchive {
26    pub fn new(
27        file_library: FileLibrary,
28        file_id_main: FileID,
29        main_component: &MainComponent,
30        program_contents: &Contents,
31        custom_gates: bool,
32    ) -> Result<ProgramArchive, (FileLibrary, Vec<Report>)> {
33        let mut merger = Merger::new();
34        let mut reports = vec![];
35        for (file_id, definitions) in program_contents {
36            if let Err(mut errs) = merger.add_definitions(*file_id, definitions) {
37                reports.append(&mut errs);
38            }
39        }
40        let (mut fresh_id, functions, templates) = merger.decompose();
41        let mut function_keys = HashSet::new();
42        let mut template_keys = HashSet::new();
43        for key in functions.keys() {
44            function_keys.insert(key.clone());
45        }
46        for key in templates.keys() {
47            template_keys.insert(key.clone());
48        }
49        let (public_inputs, mut initial_template_call) = main_component.clone();
50        initial_template_call.fill(file_id_main, &mut fresh_id);
51        if reports.is_empty() {
52            Ok(ProgramArchive {
53                id_max: fresh_id,
54                file_id_main,
55                file_library,
56                functions,
57                templates,
58                initial_template_call,
59                function_keys,
60                template_keys,
61                public_inputs,
62                custom_gates,
63            })
64        } else {
65            Err((file_library, reports))
66        }
67    }
68    //file_id_main
69    pub fn get_file_id_main(&self) -> &FileID {
70        &self.file_id_main
71    }
72    //template functions
73    pub fn contains_template(&self, template_name: &str) -> bool {
74        self.templates.contains_key(template_name)
75    }
76    pub fn get_template_data(&self, template_name: &str) -> &TemplateData {
77        assert!(self.contains_template(template_name));
78        self.templates.get(template_name).unwrap()
79    }
80    pub fn get_mut_template_data(&mut self, template_name: &str) -> &mut TemplateData {
81        assert!(self.contains_template(template_name));
82        self.templates.get_mut(template_name).unwrap()
83    }
84    pub fn get_template_names(&self) -> &HashSet<String> {
85        &self.template_keys
86    }
87    pub fn get_templates(&self) -> &TemplateInfo {
88        &self.templates
89    }
90    pub fn get_mut_templates(&mut self) -> &mut TemplateInfo {
91        &mut self.templates
92    }
93
94    pub fn remove_template(&mut self, id: &str) {
95        self.template_keys.remove(id);
96        self.templates.remove(id);
97    }
98
99    //functions functions
100    pub fn contains_function(&self, function_name: &str) -> bool {
101        self.get_functions().contains_key(function_name)
102    }
103    pub fn get_function_data(&self, function_name: &str) -> &FunctionData {
104        assert!(self.contains_function(function_name));
105        self.get_functions().get(function_name).unwrap()
106    }
107    pub fn get_mut_function_data(&mut self, function_name: &str) -> &mut FunctionData {
108        assert!(self.contains_function(function_name));
109        self.functions.get_mut(function_name).unwrap()
110    }
111    pub fn get_function_names(&self) -> &HashSet<String> {
112        &self.function_keys
113    }
114    pub fn get_functions(&self) -> &FunctionInfo {
115        &self.functions
116    }
117    pub fn get_mut_functions(&mut self) -> &mut FunctionInfo {
118        &mut self.functions
119    }
120    pub fn remove_function(&mut self, id: &str) {
121        self.function_keys.remove(id);
122        self.functions.remove(id);
123    }
124
125    //main_component functions
126    pub fn get_public_inputs_main_component(&self) -> &Vec<String> {
127        &self.public_inputs
128    }
129    pub fn main_expression(&self) -> &Expression {
130        &self.initial_template_call
131    }
132    // FileLibrary functions
133    pub fn get_file_library(&self) -> &FileLibrary {
134        &self.file_library
135    }
136}