From 2a29e9bf1c22e751df6981cfc208b2336d2dd65b Mon Sep 17 00:00:00 2001 From: Jarrod Overson Date: Mon, 16 Oct 2023 18:05:43 -0400 Subject: [PATCH] refactor: removed old, dead code --- .../flow-graph-interpreter/src/default.rs | 10 - crates/wick/flow-graph-interpreter/src/lib.rs | 1 - crates/wick/wick-config/src/default.rs | 214 ------------------ crates/wick/wick-config/src/lib.rs | 3 - 4 files changed, 228 deletions(-) delete mode 100644 crates/wick/flow-graph-interpreter/src/default.rs delete mode 100644 crates/wick/wick-config/src/default.rs diff --git a/crates/wick/flow-graph-interpreter/src/default.rs b/crates/wick/flow-graph-interpreter/src/default.rs deleted file mode 100644 index 4ddc2f0ea..000000000 --- a/crates/wick/flow-graph-interpreter/src/default.rs +++ /dev/null @@ -1,10 +0,0 @@ -// use std::borrow::Cow; - -// use wick_config::process_default; - -// pub(crate) fn make_default_transport(json: &serde_json::Value, message: &str) -> Result, String> { -// process_default(Cow::Borrowed(json), message).map_or(Err("Error processing default value".to_owned()), |result| { -// wasmrs_codec::messagepack::serialize(&result) -// .map_or(Err("Error serializing default value".to_owned()), |bytes| Ok(bytes)) -// }) -// } diff --git a/crates/wick/flow-graph-interpreter/src/lib.rs b/crates/wick/flow-graph-interpreter/src/lib.rs index 5ad1a20c2..c0e80ef25 100644 --- a/crates/wick/flow-graph-interpreter/src/lib.rs +++ b/crates/wick/flow-graph-interpreter/src/lib.rs @@ -112,7 +112,6 @@ // Add exceptions here #![allow(missing_docs)] -mod default; pub mod error; pub mod graph; mod interpreter; diff --git a/crates/wick/wick-config/src/default.rs b/crates/wick/wick-config/src/default.rs deleted file mode 100644 index aa6891ab6..000000000 --- a/crates/wick/wick-config/src/default.rs +++ /dev/null @@ -1,214 +0,0 @@ -use std::borrow::Cow; - -/// Parse a JSON template. -pub fn parse_default(json_str: &str) -> Result { - serde_json::from_str(json_str) -} - -/// The string to replace in the JSON template. -pub const ERROR_STR: &str = "$ERROR"; - -/// Render the JSON template while replacing any instance of [ERROR_STR] with the passed message. -pub fn process_default<'a>( - obj: Cow<'a, serde_json::Value>, - message: &str, -) -> Result, serde_json::Error> { - let result = if obj.is_string() { - let s = obj.as_str().unwrap(); - if s.contains(ERROR_STR) { - Cow::Owned(serde_json::Value::String(s.replace(ERROR_STR, message))) - } else { - obj - } - } else if obj.is_object() { - let object = obj.as_object().unwrap(); - let mut new_values = vec![]; - for (k, v) in object.iter() { - let cow_v = Cow::Borrowed(v); - let new_v = process_default(cow_v.clone(), message)?; - if cow_v != new_v { - new_values.push((k, new_v)); - } - } - if !new_values.is_empty() { - let mut object = object.clone(); - for (k, v) in new_values { - object.insert(k.clone(), v.into_owned()); - } - Cow::Owned(serde_json::Value::Object(object)) - } else { - obj - } - } else if obj.is_array() { - let array = obj.as_array().unwrap(); - let mut new_values = vec![]; - - #[allow(clippy::needless_range_loop)] - for i in 0..array.len() { - let cow_v = Cow::Borrowed(&array[i]); - let new_v = process_default(cow_v.clone(), message)?; - if cow_v != new_v { - new_values.push((i, new_v)); - } - } - if !new_values.is_empty() { - let mut array = array.clone(); - for (i, v) in new_values { - array[i] = v.into_owned(); - } - Cow::Owned(serde_json::Value::Array(array)) - } else { - obj - } - } else { - obj - }; - - Ok(result) -} - -#[cfg(test)] -mod tests { - use anyhow::Result as TestResult; - use pretty_assertions::assert_eq as equals; - use serde::{Deserialize, Serialize}; - use serde_json::json; - - use super::*; - - fn process_json(json_str: &str, err: &str, assert_same: bool) -> TestResult { - let json = parse_default(json_str)?; - let new_json = process_default(Cow::Borrowed(&json), err)?; - if assert_same { - equals!(Cow::Borrowed(&json), new_json); - } - Ok(new_json.into_owned()) - } - - #[test_logger::test] - fn test_cow_impl() -> TestResult<()> { - let json_str = r#" - "Error: $ERROR" - "#; - - let json = parse_default(json_str)?; - - let err = "This is my error message"; - let new_json = process_default(Cow::Borrowed(&json), err)?; - equals!(new_json.into_owned(), json!(format!("Error: {}", err))); - - let err = "This another error message"; - let new_json = process_default(Cow::Borrowed(&json), err)?; - equals!(new_json.into_owned(), json!(format!("Error: {}", err))); - - Ok(()) - } - - #[test_logger::test] - fn test_string_no_sub() -> TestResult<()> { - let json_str = r#" - "My default" - "#; - - let real = process_json(json_str, "This is my error message", true)?; - - equals!(real, "My default"); - - Ok(()) - } - - #[test_logger::test] - fn test_string_with_sub() -> TestResult<()> { - let json_str = r#" - "Error: $ERROR" - "#; - - let err = "This is my error message"; - - let real = process_json(json_str, err, false)?; - - equals!(real, format!("Error: {}", err)); - - Ok(()) - } - - #[test_logger::test] - fn test_obj_no_sub() -> TestResult<()> { - let json_str = r#" - {"method":"GET"} - "#; - - #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] - struct Request { - method: String, - } - - let err = "This is my error message"; - - let real = process_json(json_str, err, true)?; - - equals!( - real, - serde_json::to_value(Request { - method: "GET".to_owned() - })? - ); - - Ok(()) - } - - #[test_logger::test] - fn test_obj_with_sub() -> TestResult<()> { - let json_str = r#" - {"method":"GET: $ERROR"} - "#; - - #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] - struct Request { - method: String, - } - - let err = "This is my error message"; - - let real = process_json(json_str, err, false)?; - - equals!( - real, - serde_json::to_value(Request { - method: format!("GET: {}", err) - })? - ); - - Ok(()) - } - - #[test_logger::test] - fn test_arr_no_sub() -> TestResult<()> { - let json_str = r#" - ["this", "that"] - "#; - - let err = "This is my error message"; - - let real = process_json(json_str, err, true)?; - - equals!(real, serde_json::to_value(vec!["this".to_owned(), "that".to_owned()])?); - - Ok(()) - } - - #[test_logger::test] - fn test_arr_with_sub() -> TestResult<()> { - let json_str = r#" - ["this", "$ERROR"] - "#; - - let err = "This is my error message"; - - let real = process_json(json_str, err, false)?; - - equals!(real, serde_json::to_value(vec!["this".to_owned(), err.to_owned()])?); - - Ok(()) - } -} diff --git a/crates/wick/wick-config/src/lib.rs b/crates/wick/wick-config/src/lib.rs index 52b8b9aa2..3ddaf6f56 100644 --- a/crates/wick/wick-config/src/lib.rs +++ b/crates/wick/wick-config/src/lib.rs @@ -122,7 +122,6 @@ /// Structures and functions for auditing Wick Manifests. #[cfg(feature = "config")] pub mod audit; -mod default; mod helpers; mod traits; @@ -151,8 +150,6 @@ pub mod v1; /// Methods to read and load raw configurations. pub mod load; -pub use default::{parse_default, process_default, ERROR_STR}; - /// The crate's error type. pub type Error = crate::error::ManifestError;