From 958ea7f9e394554e6c83e62378297484e38a21e2 Mon Sep 17 00:00:00 2001 From: an-altosian Date: Tue, 17 Dec 2024 22:32:37 +0000 Subject: [PATCH] improve logging for converting custom chem hm to json --- resources/custom_chemistries.json | 18 ++- src/simpleaf_commands/chemistry.rs | 4 +- src/simpleaf_commands/inspect.rs | 4 +- src/utils/af_utils.rs | 188 +++++++++++++++++------------ 4 files changed, 130 insertions(+), 84 deletions(-) diff --git a/resources/custom_chemistries.json b/resources/custom_chemistries.json index e29e482..705ea82 100644 --- a/resources/custom_chemistries.json +++ b/resources/custom_chemistries.json @@ -3,36 +3,42 @@ "geometry": "1{b[16]u[12]x:}2{r[50]x:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/851sydyypct2cy4nri98yhelb4a9q3sg.txt" } , "visiumv1" : { "geometry": "1{b[16]u[12]x:}2{r:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/851sydyypct2cy4nri98yhelb4a9q3sg.txt" } , "visiumv4-probe" : { "geometry": "1{b[16]u[12]x:}2{r[50]x:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/i4udjwaceixd1em71fol50c3izl1wbzk.txt" } , "visiumv4" : { "geometry": "1{b[16]u[12]x:}2{r:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/i4udjwaceixd1em71fol50c3izl1wbzk.txt" } , "visiumv5-probe" : { "geometry": "1{b[16]u[12]x:}2{r[50]x:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/518elp0hmb0es0qbhmp2o34evup4qwwr.txt" } , "visiumv5" : { "geometry": "1{b[16]u[12]x:}2{r:}", "version" : "0.1.0", "expected_ori" : "fw", - "local_pl_path" : null + "local_pl_path" : null, + "remote_pl_path" : "https://umd.box.com/shared/static/518elp0hmb0es0qbhmp2o34evup4qwwr.txt" } } \ No newline at end of file diff --git a/src/simpleaf_commands/chemistry.rs b/src/simpleaf_commands/chemistry.rs index df90528..d47904d 100644 --- a/src/simpleaf_commands/chemistry.rs +++ b/src/simpleaf_commands/chemistry.rs @@ -29,8 +29,8 @@ pub fn add_chemistry(af_home_path: PathBuf, add_chem_cmd: Commands) -> Result<() name: name.clone(), geometry: geometry.clone(), expected_ori: Some(ExpectedOri::from_str(&expected_ori)?), - local_pl_path: local_pl_path.clone(), - remote_pl_url: remote_pl_url.clone(), + local_pl_path: local_pl_path, + remote_pl_url: remote_pl_url, version: None }; diff --git a/src/simpleaf_commands/inspect.rs b/src/simpleaf_commands/inspect.rs index 588f69e..ebdcb8a 100644 --- a/src/simpleaf_commands/inspect.rs +++ b/src/simpleaf_commands/inspect.rs @@ -11,7 +11,7 @@ use std::path::PathBuf; pub fn inspect_simpleaf(version: &str, af_home_path: PathBuf) -> Result<()> { // Read the JSON contents of the file as an instance of `User`. - let v: Value = inspect_af_home(af_home_path.as_path())?; + let simpleaf_info: Value = inspect_af_home(af_home_path.as_path())?; // do we have a custom chemistry file let custom_chem_p = af_home_path.join("custom_chemistries.json"); let chem_info_value = if custom_chem_p.is_file() { @@ -35,7 +35,7 @@ pub fn inspect_simpleaf(version: &str, af_home_path: PathBuf) -> Result<()> { let inspect_v = json!({ "simpleaf_version" : version, - "simpleaf_info" : v, + "simpleaf_info" : simpleaf_info, "custom_chem_info" : chem_info_value, "builtin_chemistries" : { "rna" : rna_chem_list, diff --git a/src/utils/af_utils.rs b/src/utils/af_utils.rs index f4085ff..a0ff653 100644 --- a/src/utils/af_utils.rs +++ b/src/utils/af_utils.rs @@ -733,7 +733,8 @@ pub fn parse_single_custom_chem_from_value(key: &str, value: &Value) -> Result Result Result Result Result { - let prog_ver_string = v - .as_str() - .with_context(|| { + if v.is_null() { + None + } else { + // if it exists, it should be string + let prog_ver_string = v + .as_str() + .with_context(|| { + format!( + "Couldn't parse the version for the custom chemistry {} as a string: {}", + key, + v, + ) + })?; + + // check if the version is valid + Version::parse(prog_ver_string).with_context(|| { format!( - "Couldn't parse the version for the custom chemistry {} as a string: {}", + "Found invalid version string for the custom chemistry {}: {}", key, - v, + prog_ver_string ) })?; - - // check if the version is valid - Version::parse(prog_ver_string).with_context(|| { - format!( - "Found invalid version string for the custom chemistry {}: {}", - key, - prog_ver_string - ) - })?; - Some(prog_ver_string.to_string()) + Some(prog_ver_string.to_string()) + } } None => None }; // check if the local_pl_path field exists and is valid let local_pl_path = if let Some(lpp) = obj.get(LOCAL_PL_PATH_KEY) { - // if it exists, it should be string - let local_pl_path_str = lpp.as_str().with_context(|| { - format!( - "Couldn't parse the local_pl_path field for {}: {}", - key, - lpp - ) - })?; - - // check if the local_pl_path exists - if !PathBuf::from(local_pl_path_str).is_file() { - Err(anyhow!( - "Couldn't find the local_pl_path for the custom chemistry record for {}: {}", - key, - local_pl_path_str - ))?; + if lpp.is_null() { + None + } else { + // if it exists, it should be string + let local_pl_path_str = lpp.as_str().with_context(|| { + format!( + "Couldn't parse the local_pl_path field for {}: {}", + key, + lpp + ) + })?; + + // check if the local_pl_path exists + if !PathBuf::from(local_pl_path_str).is_file() { + Err(anyhow!( + "Couldn't find the local_pl_path for the custom chemistry record for {}: {}", + key, + local_pl_path_str + ))?; + } + + Some(local_pl_path_str.to_string()) } - - Some(local_pl_path_str.to_string()) } else { None }; @@ -832,15 +858,19 @@ pub fn parse_single_custom_chem_from_value(key: &str, value: &Value) -> Result) let mut value = json!({ GEOMETRY_KEY: v.geometry.clone() }); - if let Some(eo) = &v.expected_ori { - value[EXPECTED_ORI_KEY] = json!(eo.as_str()); - } - if let Some(ver) = &v.version { - value[VERSION_KEY] = json!(ver); - } - if let Some(lpp) = &v.local_pl_path { - value[LOCAL_PL_PATH_KEY] = json!(lpp); - } - if let Some(rpu) = &v.remote_pl_url { - value[REMOTE_PL_URL_KEY] = json!(rpu); - } + value[EXPECTED_ORI_KEY] = if let Some(eo) = &v.expected_ori { + json!(eo.as_str()) + } else { + info!("`expected_ori` is missing for custom chemistry {}; Set as {}", k, ExpectedOri::Both.as_str()); + json!(ExpectedOri::Both.as_str()) + }; + value[VERSION_KEY] = if let Some(ver) = &v.version { + json!(ver) + } else { + info!("`version` is missing for custom chemistry {}; Set as {}", k, "0.0.1"); + json!("0.0.1") + }; + value[LOCAL_PL_PATH_KEY] = if let Some(lpp) = &v.local_pl_path { + json!(lpp) + } else { + json!(null) + }; + value[REMOTE_PL_URL_KEY] = if let Some(rpu) = &v.remote_pl_url { + json!(rpu) + } else { + json!(null) + }; (k.clone(), value) }) .collect();