1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
//! # CSharp_Binder //! //! CSharp_Binder is a tool written to generate C# bindings for a Rust FFI (Foreign Function Interface). //! By interacting over extern C functions, this allows you to easily call Rust functions from C#, //! without having to write the extern C# functions yourself. //! //! CSharp_Binder will when given a Rust script, parse this script, and extract any functions marked as //! extern "C", enums with a ``[repr(u*)]`` attribute, and structs with a ``#[repr(C)]`` attribute. It //! will then convert these into appropriate representations in C#. //! //! CSharp_Binder will also extract Rust documentation on functions, enums and their variants, and //! on structs and their fields, and convert it into XML Documentation on the generated C# code. //! //! Note that CSharp_Binder uses syn to parse Rust scripts, so macros will not be expanded! If you //! have functions, structs, or enums that need to be extracted inside macros, make sure to run them //! to something like cargo-expand first. //! //! # Examples //! //! Example: //! ``` //! use csharp_binder::{CSharpConfiguration, CSharpBuilder}; //! //! fn main(){ //! // Create C# configuration with C# target version 9. //! let mut configuration = CSharpConfiguration::new(9); //! let rust_file = r#" //! /// Just a random return enum //! #[repr(u8)] //! enum ReturnEnum { //! Val1, //! Val2, //! } //! //! /// An input struct we expect //! #[repr(C)] //! struct InputStruct { //! field_a: u16, //! /// This field is used for floats! //! field_b: f64, //! } //! //! pub extern "C" fn foo(a: InputStruct) -> ReturnEnum { //! } //! "#; //! let mut builder = CSharpBuilder::new(rust_file, "foo", &mut configuration) //! .expect("Failed to parse file"); //! builder.set_namespace("MainNamespace"); //! builder.set_type("InsideClass"); //! let script = builder.build().expect("Failed to build"); //! } //!``` //! //! This would return the following C# code: //! //! ```cs //! // Automatically generated, do not edit! //! using System; //! using System.Runtime.InteropServices; //! //! namespace MainNamespace //! { //! internal static class InsideClass //! { //! /// <summary> //! /// Just a random return enum //! /// </summary> //! public enum ReturnEnum : byte //! { //! Val1, //! Val2, //! } //! //! /// <summary> //! /// An input struct we expect //! /// </summary> //! [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] //! public struct InputStruct //! { //! /// <remarks>u16</remarks> //! public readonly ushort FieldA; //! /// <summary> //! /// This field is used for floats! //! /// </summary> //! /// <remarks>f64</remarks> //! public readonly double FieldB; //! } //! //! /// <param name="a">InputStruct</param> //! /// <returns>ReturnEnum</returns> //! [DllImport("foo", CallingConvention = CallingConvention.Cdecl, EntryPoint="foo")] //! internal static extern ReturnEnum Foo(InputStruct a); //! //! } //! } //! ``` //! use crate::builder::{build_csharp, parse_script}; use std::cell::RefCell; use std::collections::HashMap; use std::fmt::Formatter; mod builder; #[cfg(test)] mod tests; pub(crate) struct CSharpType { pub namespace: Option<String>, pub inside_type: Option<String>, pub real_type_name: String, } /// This struct holds the generic data used between multiple builds. Currently this only holds the /// type registry, but further features such as ignore patterns will likely be added here. pub struct CSharpConfiguration { known_types: HashMap<String, CSharpType>, csharp_version: u8, out_type: Option<String>, generated_warning: String, } impl CSharpConfiguration { /// Create a new C# configuration. Input parameter is the target version of C#, i.e. C# 7, 8, 9, etc. pub fn new(csharp_version: u8) -> Self { Self { known_types: HashMap::new(), csharp_version, out_type: None, generated_warning: "Automatically generated, do not edit!".to_string(), } } /// Register a type the converter should know about. /// /// Useful if you use a type on the Rust side that you know has a C# representation without first /// passing it through the C#builder. This function takes the Rust type name, along with an optional /// C# namespace, optional containing type, and the actual C# type name. pub fn add_known_type( &mut self, rust_type_name: &str, csharp_namespace: Option<String>, csharp_inside_type: Option<String>, csharp_type_name: String, ) { self.known_types.insert( rust_type_name.to_string(), CSharpType { namespace: csharp_namespace, inside_type: csharp_inside_type, real_type_name: csharp_type_name, }, ); } /// Sets a rust type to represent an out parameter in C#. /// /// This allows converting a parameter like ``foo: Out<u8>`` into ``out byte foo``. /// Useful for following patterns such as: <https://github.com/KodrAus/rust-csharp-ffi> pub fn set_out_type(&mut self, rust_type_name: &str) { self.out_type = Some(rust_type_name.to_string()); } /// By default we add a warning on top of each generated C# script, which defaults to /// ``// Automatically generated, do not edit!``. This functions allows you to modify this /// warning. Can be multiline, and can be removed entirely by setting with an empty string. pub fn set_generated_warning(&mut self, generated_warning: &str) { self.generated_warning = generated_warning.to_string(); } pub(crate) fn get_known_type(&self, rust_type_name: &str) -> Option<&CSharpType> { self.known_types.get(rust_type_name) } } /// The CSharpBuilder is used to load a Rust script string, and convert it into the appropriate C# /// script as a string. pub struct CSharpBuilder<'a> { configuration: RefCell<&'a mut CSharpConfiguration>, dll_name: String, usings: Vec<String>, tokens: syn::File, namespace: Option<String>, type_name: Option<String>, } impl<'a> CSharpBuilder<'a> { /// Creates a new C# Builder from a Rust script string, the name of the library C# is going to /// make calls to (the .so/.dll file), and a configuration. /// /// Note that this will immediately parse the rust script and extract its symbols. As such, this /// can return a parse error. pub fn new( script: &str, dll_name: &str, configuration: &'a mut CSharpConfiguration, ) -> Result<CSharpBuilder<'a>, Error> { match parse_script(script) { Ok(tokens) => Ok(CSharpBuilder { configuration: RefCell::new(configuration), dll_name: dll_name.to_string(), // Load the default usings. usings: vec![ "System".to_string(), "System.Runtime.InteropServices".to_string(), ], tokens, namespace: None, type_name: None, }), Err(e) => Err(Error::from(e)), } } /// This function will return the C# script. Should be called after the C# Builder is setup. pub fn build(&mut self) -> Result<String, Error> { build_csharp(self) } /// Sets the namespace the C# script should use to generate its functions in. If not set, no /// namespace will be used. pub fn set_namespace(&mut self, namespace: &str) { self.namespace = Some(namespace.to_string()); } /// Sets the type that will be wrapped around the generated C# script. If not set, no type /// will be used. pub fn set_type(&mut self, type_name: &str) { self.type_name = Some(type_name.to_string()); } /// Adds a using to the top of the C# script. pub fn add_using(&mut self, using: &str) { self.usings.push(using.to_string()); } pub(crate) fn add_known_type(&self, rust_type_name: &str, csharp_type_name: &str) { self.configuration.borrow_mut().add_known_type( rust_type_name, self.namespace.clone(), self.type_name.clone(), csharp_type_name.to_string(), ); } } #[derive(Debug)] pub enum Error { ParseError(syn::Error), IOError(std::io::Error), FmtError(std::fmt::Error), UnsupportedError(String, proc_macro2::Span), UnknownType(String, proc_macro2::Span), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Error::ParseError(e) => e.fmt(f), Error::IOError(e) => e.fmt(f), Error::FmtError(e) => e.fmt(f), Error::UnsupportedError(e, span) => { f.write_str(e)?; f.write_str( format!( ". At line {}, position {}", span.start().line, span.start().column ) .as_str(), ) } Error::UnknownType(e, span) => { f.write_str(e)?; f.write_str( format!( ". At At line {}, position {}", span.start().line, span.start().column ) .as_str(), ) } } } } impl From<syn::Error> for Error { fn from(error: syn::Error) -> Self { Error::ParseError(error) } } impl From<std::io::Error> for Error { fn from(error: std::io::Error) -> Self { Error::IOError(error) } } impl From<std::fmt::Error> for Error { fn from(error: std::fmt::Error) -> Self { Error::FmtError(error) } }