diff --git a/.gitignore b/.gitignore index 677c1be..707b5cd 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ node_modules /lib /lib-commonjs +/rust/target # ----------------------------------------------------------------------------------------------------------------- Test diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..66f80fe --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rust" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59bf04c28bee9043ed9ea1e41afc0552288d3aba9c6efdd78903b802926f4879" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..6681e70 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0.188", features = ["derive"] } +serde_json = "1.0.107" diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..4354f46 --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,5 @@ +mod serde; + +fn main() { + println!("Hello, world!"); +} diff --git a/rust/src/serde.rs b/rust/src/serde.rs new file mode 100644 index 0000000..82641cd --- /dev/null +++ b/rust/src/serde.rs @@ -0,0 +1,30 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_result_to_json() -> serde_json::Result<()> { + let result1: Result = Ok(1); + let result2: Result = Err("Some error message"); + + let json1 = serde_json::to_string(&result1)?; + let json2 = serde_json::to_string(&result2)?; + + assert_eq!(json1.as_str(), "{\"Ok\":1}"); + assert_eq!(json2.as_str(), "{\"Err\":\"Some error message\"}"); + + Ok(()) + } + + #[test] + fn it_result_from_json() -> serde_json::Result<()> { + let str1 = "{\"Ok\":1}"; + let str2 = "{\"Err\":\"Some error message\"}"; + + let result1: Result = serde_json::from_str(str1)?; + let result2: Result = serde_json::from_str(str2)?; + + assert_eq!(result1, Ok(1)); + assert_eq!(result2, Err("Some error message")); + + Ok(()) + } +}