-
Notifications
You must be signed in to change notification settings - Fork 93
/
export.rs
56 lines (43 loc) · 1.58 KB
/
export.rs
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
use std::{fs, io::Write, path::PathBuf};
use nickel_lang_core::{
error::{Error, IOError},
eval::cache::lazy::CBNCache,
program::Program,
serialize::{self, ExportFormat},
};
use crate::{customize::CustomizeMode, global::GlobalContext, input::InputOptions};
#[derive(clap::Parser, Debug)]
pub struct ExportCommand {
#[arg(long, short, value_enum, default_value_t)]
pub format: ExportFormat,
/// Output file. Standard output by default
#[arg(short, long)]
pub output: Option<PathBuf>,
#[command(flatten)]
pub input: InputOptions<CustomizeMode>,
}
impl ExportCommand {
pub fn run(self, ctxt: &mut GlobalContext) {
ctxt.with_program(&self.input, |program| self.export(program));
}
fn export(&self, program: &mut Program<CBNCache>) -> Result<(), Error> {
let rt = program.eval_full_for_export()?;
// We only add a trailing newline for JSON exports. Both YAML and TOML
// exporters already append a trailing newline by default.
let trailing_newline = self.format == ExportFormat::Json;
serialize::validate(self.format, &rt)?;
if let Some(file) = &self.output {
let mut file = fs::File::create(file).map_err(IOError::from)?;
serialize::to_writer(&mut file, self.format, &rt)?;
if trailing_newline {
writeln!(file).map_err(IOError::from)?;
}
} else {
serialize::to_writer(std::io::stdout(), self.format, &rt)?;
if trailing_newline {
println!();
}
}
Ok(())
}
}