Skip to content

Commit f85fac5

Browse files
committed
better fallback handling
1 parent b87cef0 commit f85fac5

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

hugr-core/src/envelope.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,27 @@ pub fn get_generator<H: HugrView>(modules: &[H]) -> Option<String> {
8888

8989
/// Format a generator value from the metadata.
9090
pub fn format_generator(json_val: &serde_json::Value) -> String {
91-
let mut out = String::new();
92-
93-
if let Some(generator) = json_val.as_str() {
94-
// just a string, use it directly
95-
out.push_str(generator);
96-
} else if let Some(obj) = json_val.as_object() {
97-
// for expected structure print "generator vX.Y.Z"
98-
if let Some(name) = obj.get("name").and_then(|v| v.as_str()) {
99-
out.push_str(name);
100-
}
101-
if let Some(version) = obj.get("version").and_then(|v| v.as_str()) {
102-
out.push_str("-v");
103-
out.push_str(version);
91+
match json_val {
92+
serde_json::Value::String(s) => s.clone(),
93+
serde_json::Value::Object(obj) => {
94+
if let (Some(name), version) = (
95+
obj.get("name").and_then(|v| v.as_str()),
96+
obj.get("version").and_then(|v| v.as_str()),
97+
) {
98+
if let Some(version) = version {
99+
// Expected format: {"name": "generator", "version": "1.0.0"}
100+
format!("{}-v{}", name, version)
101+
} else {
102+
name.to_string()
103+
}
104+
} else {
105+
// just print the whole object as a string
106+
json_val.to_string()
107+
}
104108
}
105-
} else {
106-
// fallback to just printing the JSON value
107-
out.push_str(json_val.to_string().as_str());
109+
// Raw JSON string fallback
110+
_ => json_val.to_string(),
108111
}
109-
110-
out
111112
}
112113

113114
fn gen_str(generator: &Option<String>) -> String {

0 commit comments

Comments
 (0)