-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(candid-utils): Factor out Candid pretty printing function (#3572)
This PR moves Candid pretty printing into a separate function `printing::pretty`, adding a few unit tests to specify its expected behavior. This function will be used in a new crate that will be added in a follow-up PR. < [Previous PR](#3488) | [Next PR](#3439) >
- Loading branch information
Showing
9 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod printing; | ||
pub mod validation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use candid::{CandidType, IDLValue}; | ||
|
||
/// Pretty print `value` into a Candid string. | ||
/// | ||
/// Example: | ||
/// | ||
/// ```rust | ||
/// #[derive(CandidType)] | ||
/// struct DummyCandidStruct { | ||
/// pub status: Option<i32>, | ||
/// pub module_hash: Vec<u8>, | ||
/// pub controllers: String, | ||
/// pub memory_size: Option<u64>, | ||
/// pub cycles: Option<u64>, | ||
/// } | ||
/// | ||
/// let dummy = DummyCandidStruct { | ||
/// status: Some(42), | ||
/// module_hash: vec![1, 2, 3, 4], | ||
/// controllers: "foo".to_string(), | ||
/// memory_size: Some(100), | ||
/// cycles: Some(123), | ||
/// }; | ||
/// println!("{}", pretty(&dummy)); | ||
/// ``` | ||
/// | ||
/// Output: | ||
/// ```candid | ||
/// record { | ||
/// status = opt (42 : int32); | ||
/// controllers = "foo"; | ||
/// memory_size = opt (100 : nat64); | ||
/// cycles = opt (123 : nat64); | ||
/// module_hash = blob "\01\02\03\04"; | ||
/// } | ||
/// ``` | ||
pub fn pretty<T: CandidType>(value: &T) -> Result<String, String> { | ||
let value = IDLValue::try_from_candid_type(value).map_err(|err| err.to_string())?; | ||
|
||
Ok(value.to_string()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use super::*; | ||
use pretty_assertions::{assert_eq, assert_str_eq}; | ||
|
||
#[derive(CandidType)] | ||
struct DummyCandidStruct { | ||
pub status: Option<i32>, | ||
pub module_hash: Vec<u8>, | ||
pub controllers: String, | ||
pub memory_size: Option<u64>, | ||
pub cycles: Option<u64>, | ||
} | ||
|
||
#[derive(CandidType)] | ||
enum DummyCandidVariant { | ||
Foo(String), | ||
Bar { abc: String, xyz: String }, | ||
} | ||
|
||
#[derive(CandidType)] | ||
struct DummyCandidContainer { | ||
foo: DummyCandidVariant, | ||
bar: Result<DummyCandidVariant, String>, | ||
} | ||
|
||
#[track_caller] | ||
fn assert_expectation<T: CandidType>(value: &T, expected_result: Result<String, String>) { | ||
let observed_result = pretty(value); | ||
|
||
match (observed_result, expected_result) { | ||
(Ok(observed), Ok(expected)) => { | ||
assert_str_eq!(observed, expected); | ||
} | ||
(observed, expected) => { | ||
assert_eq!(observed, expected); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_pretty_printing_simple_struct() { | ||
assert_expectation( | ||
&DummyCandidStruct { | ||
status: Some(42), | ||
module_hash: vec![1, 2, 3, 4], | ||
controllers: "foo".to_string(), | ||
memory_size: Some(100), | ||
cycles: Some(123), | ||
}, | ||
Ok(r#"record { | ||
status = opt (42 : int32); | ||
controllers = "foo"; | ||
memory_size = opt (100 : nat64); | ||
cycles = opt (123 : nat64); | ||
module_hash = blob "\01\02\03\04"; | ||
}"# | ||
.to_string()), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_pretty_printing_complex_struct() { | ||
assert_expectation( | ||
&DummyCandidContainer { | ||
foo: DummyCandidVariant::Foo("hello".to_string()), | ||
bar: Ok(DummyCandidVariant::Bar { | ||
abc: "abc".to_string(), | ||
xyz: "xyz".to_string(), | ||
}), | ||
}, | ||
Ok(r#"record { | ||
bar = variant { Ok = variant { Bar = record { abc = "abc"; xyz = "xyz" } } }; | ||
foo = variant { Foo = "hello" }; | ||
}"# | ||
.to_string()), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters