diff --git a/crux_core/src/typegen.rs b/crux_core/src/typegen.rs index df4663603..caca1fcc2 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]) + .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 requests_path = self.extensions_path("csharp/Requests.cs"); + let requests_data = fs::read_to_string(requests_path)?; + + write!( + output, + "{}", + requests_data.replace("SharedTypes", package_name) + )?; + + 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..4fe41837f --- /dev/null +++ b/crux_core/typegen_extensions/csharp/Requests.cs @@ -0,0 +1,28 @@ +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; + } + } +}