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
use regex::Regex;
use std::fs::File;
use std::io::Write;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use std::str::FromStr;
use crate::spaces::FuncSpace;
#[derive(Debug, Clone)]
pub enum Format {
Cbor,
Json,
Toml,
Yaml,
}
impl Format {
pub fn all() -> &'static [&'static str] {
&["cbor", "json", "toml", "yaml"]
}
}
impl FromStr for Format {
type Err = String;
fn from_str(format: &str) -> Result<Self, Self::Err> {
match format {
"cbor" => Ok(Format::Cbor),
"json" => Ok(Format::Json),
"toml" => Ok(Format::Toml),
"yaml" => Ok(Format::Yaml),
format => Err(format!("{:?} is not a supported format", format)),
}
}
}
pub(crate) fn dump_formats(
space: &FuncSpace,
path: &PathBuf,
output_path: &Option<PathBuf>,
output_format: Format,
pretty: bool,
) -> std::io::Result<()> {
if output_path.is_none() {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
match output_format {
Format::Cbor => Err(Error::new(
ErrorKind::Other,
"Cbor format cannot be printed to stdout",
)),
Format::Json => {
let json_data = if pretty {
serde_json::to_string_pretty(&space).unwrap()
} else {
serde_json::to_string(&space).unwrap()
};
write!(stdout, "{}", json_data)
}
Format::Toml => {
let toml_data = if pretty {
toml::to_string_pretty(&space).unwrap()
} else {
toml::to_string(&space).unwrap()
};
write!(stdout, "{}", toml_data)
}
Format::Yaml => write!(stdout, "{}", serde_yaml::to_string(&space).unwrap()),
}
} else {
let format_ext = match output_format {
Format::Cbor => ".cbor",
Format::Json => ".json",
Format::Toml => ".toml",
Format::Yaml => ".yml",
};
let output_path = output_path.as_ref().unwrap();
let mut file = path.as_path().file_name().unwrap().to_os_string();
file.push(format_ext);
let mut format_path = output_path.clone();
format_path.push(file);
if format_path.as_path().exists() {
let mut new_filename = path.to_str().unwrap().to_string();
let re = Regex::new(r"[\\:/]").unwrap();
new_filename = re.replace_all(&new_filename, "_").to_string();
new_filename.push_str(format_ext);
format_path.pop();
format_path.push(new_filename);
}
let mut format_file = File::create(format_path)?;
match output_format {
Format::Cbor => serde_cbor::to_writer(format_file, &space)
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
Format::Json => {
if pretty {
serde_json::to_writer_pretty(format_file, &space)
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
} else {
serde_json::to_writer(format_file, &space)
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
}
}
Format::Toml => {
let toml_data = if pretty {
toml::to_string_pretty(&space).unwrap()
} else {
toml::to_string(&space).unwrap()
};
format_file.write_all(toml_data.as_bytes())
}
Format::Yaml => serde_yaml::to_writer(format_file, &space)
.map_err(|e| Error::new(ErrorKind::Other, e.to_string())),
}
}
}