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 1 commit
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
26 changes: 13 additions & 13 deletions crux_core/src/typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
Expand All @@ -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);
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 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(())
}
Expand Down
29 changes: 28 additions & 1 deletion crux_core/typegen_extensions/csharp/Requests.cs
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
// hi
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