1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::fmt;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use error::{CodeSpawnError, Result};
use raw_code::{CodeItem, CodeConfig, print_code_item};
use string_gen::keywords::*;
use string_gen::{code_to_str};
#[derive(PartialEq, Eq, Hash)]
pub enum Lang {
Cpp,
Rust
}
pub struct FormattedCode {
pub language: Lang,
pub elements: Vec<CodeItem>,
pub num_tabs: u8,
pub tab_char: char,
}
impl FormattedCode {
#[doc(hidden)]
pub fn new(lang: Lang, cfg: &Option<&CodeConfig>, data: &Vec<CodeItem>) -> Result<FormattedCode> {
let mut fmt_code = FormattedCode {
language: lang,
elements: data.to_vec(),
num_tabs: 4,
tab_char: ' '
};
match *cfg {
Some(config) => {
try!(fmt_code.process_config(&config));
},
None => {}
};
Ok(fmt_code)
}
fn process_config(&mut self, config: &CodeConfig) -> Result<()> {
for (k, v) in config.type_dict.iter() {
for e in self.elements.iter_mut() {
FormattedCode::update_element(e, &k, &v);
}
}
for (k, v) in config.name_dict.iter() {
for e in self.elements.iter_mut() {
FormattedCode::update_element(e, &k, &v);
}
}
for (k, v) in config.global_cfg.iter() {
match k.as_str() {
NUM_TABS => if !v.is_empty() { self.num_tabs = try!(v.parse::<u8>()); },
TAB_CHAR => if !v.is_empty() { self.tab_char = some!(v.chars().next(), "Invalid iterator"); },
_ => {}
}
}
Ok(())
}
fn update_element(e: &mut CodeItem, key: &String, value: &String) {
for child in e.children.iter_mut() {
FormattedCode::update_element(child, key, value);
}
for a in e.attributes.iter_mut() {
if a.1 == *key {
a.1 = value.clone();
}
}
}
pub fn to_file(&self, filename: &str) -> Result<()> {
let code = self.to_string();
let path = Path::new(&filename);
let mut file = try!(File::create(&path));
try!(file.write_all(code.as_bytes()));
Ok(())
}
pub fn to_string(&self) -> String {
code_to_str(self)
}
}
impl fmt::Display for Lang {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str_name = match *self {
Lang::Cpp => "C/C++", Lang::Rust => "Rust"
};
write!(f, "{}", str_name)
}
}
impl fmt::Display for FormattedCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let _ = write!(f, "Target: {}\n", self.language);
let _ = write!(f, "*\n");
for e in self.elements.iter() {
let mut empty_spaces = Vec::<u8>::new();
print_code_item(e, f, 0, &mut empty_spaces);
}
write!(f, "*\n")
}
}