Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support C# Typegen +WinUI 3 Sample #255

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion crux_core/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -546,6 +546,50 @@ The 2 common cases are:
Ok(())
}

pub fn csharp(&mut self, package_name: &str, path: impl AsRef<Path>) -> 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);
Copy link
Author

@huwaireb huwaireb Jul 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An enum like

enum Event {
    Increment,
    Decrement,
    Reset,
}

would translate to

enum Event {
        Increment = 0,
        Decrement = 1,
        Reset = 2,
    }

instead of generating classes for each variant e.g Increment. Limited to enums that don't store data, those who do serde-reflection will generate a class for them.

Should we make it a parameter of the function for flexibility, or is it fine as is?


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
Expand Down
3 changes: 3 additions & 0 deletions crux_core/tests/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions crux_core/typegen_extensions/csharp/Requests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;

namespace SharedTypes
{
public static class Requests
{
public static List<Request> BincodeDeserialize(ArraySegment<byte> input)
{
Serde.IDeserializer deserializer = new Bincode.BincodeDeserializer(input);
deserializer.increase_container_depth();

var length = deserializer.deserialize_len();
var requests = new List<Request>();
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;
}
}
}
Loading