-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconvert.rs
More file actions
85 lines (72 loc) · 2.99 KB
/
convert.rs
File metadata and controls
85 lines (72 loc) · 2.99 KB
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
//! Convert between different HUGR envelope formats.
use anyhow::Result;
use clap::Parser;
use clio::Output;
use hugr::envelope::{EnvelopeConfig, EnvelopeFormat, ZstdConfig};
use crate::CliError;
use crate::hugr_io::HugrInputArgs;
/// Convert between different HUGR envelope formats.
#[derive(Parser, Debug)]
#[clap(version = "1.0", long_about = None)]
#[clap(about = "Convert a HUGR between different envelope formats.")]
#[group(id = "hugr")]
#[non_exhaustive]
pub struct ConvertArgs {
/// Hugr input.
#[command(flatten)]
pub input_args: HugrInputArgs,
/// Output file. Use '-' for stdout.
#[clap(short, long, value_parser, default_value = "-")]
pub output: Output,
/// Output format. One of: json, model, model-exts, model-text, model-text-exts
#[clap(short, long, value_name = "FORMAT")]
pub format: Option<String>,
/// Use default text-based envelope configuration.
/// Cannot be combined with --format or --binary.
#[clap(long, conflicts_with_all = ["format", "binary"])]
pub text: bool,
/// Use default binary envelope configuration.
/// Cannot be combined with --format or --text.
#[clap(long, conflicts_with_all = ["format", "text"])]
pub binary: bool,
/// Enable zstd compression for the output
#[clap(long)]
pub compress: bool,
/// Zstd compression level (1-22, where 1 is fastest and 22 is best compression)
/// Uses the default level if not specified.
#[clap(long, value_name = "LEVEL", requires = "compress")]
pub compression_level: Option<u8>,
}
impl ConvertArgs {
/// Convert a HUGR between different envelope formats
pub fn run_convert(&mut self) -> Result<()> {
let (env_config, package) = self.input_args.get_envelope()?;
// Handle text and binary format flags, which override the format option
let mut config = if self.text {
EnvelopeConfig::text()
} else if self.binary {
EnvelopeConfig::binary()
} else {
// Parse the requested format
let format = match &self.format {
Some(fmt) => match fmt.as_str() {
"json" => EnvelopeFormat::PackageJson,
"model" => EnvelopeFormat::Model,
"model-exts" => EnvelopeFormat::ModelWithExtensions,
"model-text" => EnvelopeFormat::ModelText,
"model-text-exts" => EnvelopeFormat::ModelTextWithExtensions,
_ => Err(CliError::InvalidFormat(fmt.clone()))?,
},
None => env_config.format, // Use input format if not specified
};
EnvelopeConfig::new(format)
};
// Configure compression
if let Some(level) = self.compress.then_some(self.compression_level).flatten() {
config = config.with_zstd(ZstdConfig::new(level));
}
// Write the package with the requested format
hugr::envelope::write_envelope(&mut self.output, &package, config)?;
Ok(())
}
}