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
use std::io::Write;
use std::rc::Rc;
use crate::build::Val;
use crate::convert::traits::{Converter, Result};
pub struct FlagConverter {}
impl FlagConverter {
pub fn new() -> Self {
FlagConverter {}
}
fn write_flag_name(&self, pfx: &str, name: &str, w: &mut Write) -> Result {
if name.chars().count() > 1 || pfx.chars().count() > 0 {
write!(w, "--{}{} ", pfx, name)?;
} else {
write!(w, "-{} ", name)?;
}
return Ok(());
}
fn write_list_flag(&self, pfx: &str, name: &str, def: &Vec<Rc<Val>>, w: &mut Write) -> Result {
for v in def.iter() {
let vref = v.as_ref();
if vref.is_list() || vref.is_tuple() || vref.is_func() {
eprintln!(
"Skipping non primitive val in list for flag {}{}",
pfx, name
);
} else {
self.write_flag_name(pfx, name, w)?;
self.write(pfx, vref, w)?;
}
}
return Ok(());
}
fn write(&self, pfx: &str, v: &Val, w: &mut Write) -> Result {
match v {
&Val::Empty => {
return Ok(());
}
&Val::Boolean(b) => {
write!(w, "{} ", if b { "true" } else { "false" })?;
}
&Val::Float(ref f) => {
write!(w, "{} ", f)?;
}
&Val::Int(ref i) => {
write!(w, "{} ", i)?;
}
&Val::Str(ref s) => {
write!(w, "'{}' ", s)?;
}
&Val::List(ref _def) => {
eprintln!("Skipping List...");
}
&Val::Tuple(ref flds) => {
for &(ref name, ref val) in flds.iter() {
if let &Val::Empty = val.as_ref() {
self.write_flag_name(pfx, &name.val, w)?;
continue;
}
match val.as_ref() {
&Val::Tuple(_) => {
let new_pfx = format!("{}{}.", pfx, name);
self.write(&new_pfx, val, w)?;
}
&Val::List(ref def) => {
self.write_list_flag(pfx, &name.val, def, w)?;
}
_ => {
self.write_flag_name(pfx, &name.val, w)?;
self.write(pfx, &val, w)?;
}
}
}
}
&Val::Func(ref _def) => {
eprintln!("Skipping macro...");
}
&Val::Env(ref _fs) => {
eprintln!("Skipping env...");
}
&Val::Module(ref _def) => {
eprintln!("Skipping module...");
}
}
Ok(())
}
}
impl Converter for FlagConverter {
fn convert(&self, v: Rc<Val>, mut w: &mut Write) -> Result {
self.write("", &v, &mut w)
}
fn file_ext(&self) -> String {
String::from("txt")
}
fn description(&self) -> String {
"Convert ucg Vals into command line flags.".to_string()
}
}