From 2ee30b84684c94e2bd1b97bc5c2de649ff9c5877 Mon Sep 17 00:00:00 2001 From: "Rashid J. Almheiri" Date: Fri, 5 Jul 2024 21:32:12 +0400 Subject: [PATCH 1/2] feat(typegen): support c# --- crux_core/src/typegen.rs | 46 ++++++++++++++++++- crux_core/tests/typegen.rs | 3 ++ .../typegen_extensions/csharp/Requests.cs | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 crux_core/typegen_extensions/csharp/Requests.cs diff --git a/crux_core/src/typegen.rs b/crux_core/src/typegen.rs index df4663603..72e187265 100644 --- a/crux_core/src/typegen.rs +++ b/crux_core/src/typegen.rs @@ -114,7 +114,7 @@ //! ``` use serde::Deserialize; -use serde_generate::{java, swift, typescript, Encoding, SourceInstaller}; +use serde_generate::{csharp, java, swift, typescript, Encoding, SourceInstaller}; use serde_reflection::{Registry, Tracer, TracerConfig}; use std::{ fs::{self, File}, @@ -546,6 +546,50 @@ The 2 common cases are: Ok(()) } + pub fn csharp(&mut self, package_name: &str, path: impl AsRef) -> Result { + self.ensure_registry()?; + + let path = path.as_ref().join(package_name); + + fs::create_dir_all(&path)?; + + let installer = csharp::Installer::new(path.clone()); + + installer + .install_serde_runtime() + .map_err(|e| TypeGenError::Generation(e.to_string()))?; + + installer + .install_bincode_runtime() + .map_err(|e| TypeGenError::Generation(e.to_string()))?; + + let registry = match &self.state { + State::Generating(registry) => registry, + _ => panic!("registry creation failed"), + }; + + + let config = serde_generate::CodeGeneratorConfig::new( + package_name.to_string()) + .with_encodings(vec![Encoding::Bincode]); + + installer + .install_module(&config, registry) + .map_err(|e| TypeGenError::Generation(e.to_string()))?; + + let mut output = File::create( + path.join(package_name) + .join("Requests.cs") + )?; + + let requests_path = self.extensions_path("csharp/Requests.cs"); + let requests_data = fs::read_to_string(requests_path)?; + + write!(output, "{}", requests_data)?; + + Ok(()) + } + fn ensure_registry(&mut self) -> Result { if let State::Registering(_, _) = self.state { // replace the current state with a dummy tracer diff --git a/crux_core/tests/typegen.rs b/crux_core/tests/typegen.rs index 25ed651dd..64b36535d 100644 --- a/crux_core/tests/typegen.rs +++ b/crux_core/tests/typegen.rs @@ -60,6 +60,9 @@ mod test { gen.typescript("shared_types", output_root.join("typescript")) .expect("typescript type gen failed"); + + gen.csharp("SharedTypes", output_root.join("csharp")) + .expect("csharp type gen failed"); } // TODO: instead of using the Render capability here, it would be better to also test against a custom diff --git a/crux_core/typegen_extensions/csharp/Requests.cs b/crux_core/typegen_extensions/csharp/Requests.cs new file mode 100644 index 000000000..81ab89bd4 --- /dev/null +++ b/crux_core/typegen_extensions/csharp/Requests.cs @@ -0,0 +1 @@ +// hi From 009f4930acc3514d90ae244862ea170bbd64017d Mon Sep 17 00:00:00 2001 From: "Rashid J. Almheiri" Date: Fri, 5 Jul 2024 22:36:07 +0400 Subject: [PATCH 2/2] feat(typegen): implement Requests.cs and enable C Style Enums for unit enum variants Implements a class otherwise, always for enums. Which isn't ideal, however passing the option to the user might be wiser. --- crux_core/src/typegen.rs | 26 ++++++++--------- .../typegen_extensions/csharp/Requests.cs | 29 ++++++++++++++++++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/crux_core/src/typegen.rs b/crux_core/src/typegen.rs index 72e187265..caca1fcc2 100644 --- a/crux_core/src/typegen.rs +++ b/crux_core/src/typegen.rs @@ -552,13 +552,13 @@ The 2 common cases are: let path = path.as_ref().join(package_name); fs::create_dir_all(&path)?; - + let installer = csharp::Installer::new(path.clone()); - + installer .install_serde_runtime() .map_err(|e| TypeGenError::Generation(e.to_string()))?; - + installer .install_bincode_runtime() .map_err(|e| TypeGenError::Generation(e.to_string()))?; @@ -568,24 +568,24 @@ The 2 common cases are: _ => panic!("registry creation failed"), }; - - let config = serde_generate::CodeGeneratorConfig::new( - package_name.to_string()) - .with_encodings(vec![Encoding::Bincode]); - + let config = serde_generate::CodeGeneratorConfig::new(package_name.to_string()) + .with_encodings(vec![Encoding::Bincode]) + .with_c_style_enums(true); + installer .install_module(&config, registry) .map_err(|e| TypeGenError::Generation(e.to_string()))?; - let mut output = File::create( - path.join(package_name) - .join("Requests.cs") - )?; + let mut output = File::create(path.join(package_name).join("Requests.cs"))?; let requests_path = self.extensions_path("csharp/Requests.cs"); let requests_data = fs::read_to_string(requests_path)?; - write!(output, "{}", requests_data)?; + write!( + output, + "{}", + requests_data.replace("SharedTypes", package_name) + )?; Ok(()) } diff --git a/crux_core/typegen_extensions/csharp/Requests.cs b/crux_core/typegen_extensions/csharp/Requests.cs index 81ab89bd4..4fe41837f 100644 --- a/crux_core/typegen_extensions/csharp/Requests.cs +++ b/crux_core/typegen_extensions/csharp/Requests.cs @@ -1 +1,28 @@ -// hi +using System; +using System.Collections.Generic; + +namespace SharedTypes +{ + public static class Requests + { + public static List BincodeDeserialize(ArraySegment input) + { + Serde.IDeserializer deserializer = new Bincode.BincodeDeserializer(input); + deserializer.increase_container_depth(); + + var length = deserializer.deserialize_len(); + var requests = new List(); + for (var i = 0; i < length; ++i) + { + while (deserializer.get_buffer_offset() < input.Count) + { + var req = Request.Deserialize(deserializer); + requests.Add(req); + } + } + + deserializer.decrease_container_depth(); + return requests; + } + } +}