From 861f4cbdc5c6853f1a6d09a55f6ea739c947e51f Mon Sep 17 00:00:00 2001 From: Kristupas Antanavicius Date: Fri, 9 Feb 2024 13:51:37 +0200 Subject: [PATCH] Change default access modifier to internal (#69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Access modifier may be configured via `access_modifier` configuration value. Signed-off-by: Kristupas Antanavičius --- bindgen/src/gen_cs/mod.rs | 16 ++++++++++++---- bindgen/templates/CallbackInterfaceTemplate.cs | 2 +- bindgen/templates/CustomTypeTemplate.cs | 2 +- bindgen/templates/EnumTemplate.cs | 4 ++-- bindgen/templates/ErrorTemplate.cs | 4 ++-- bindgen/templates/Helpers.cs | 16 ++++++++-------- bindgen/templates/ObjectRuntime.cs | 4 ++-- bindgen/templates/ObjectTemplate.cs | 6 +++--- bindgen/templates/RecordTemplate.cs | 2 +- bindgen/templates/wrapper.cs | 4 ++-- docs/CONFIGURATION.md | 2 ++ 11 files changed, 36 insertions(+), 26 deletions(-) diff --git a/bindgen/src/gen_cs/mod.rs b/bindgen/src/gen_cs/mod.rs index 2a06fbf..e9233b4 100644 --- a/bindgen/src/gen_cs/mod.rs +++ b/bindgen/src/gen_cs/mod.rs @@ -37,6 +37,7 @@ pub struct Config { #[serde(default)] external_packages: HashMap, global_methods_class_name: Option, + access_modifier: Option, } #[derive(Debug, Default, Clone, Serialize, Deserialize)] @@ -77,6 +78,13 @@ impl Config { .expect("`cdylib_name` not specified") .clone() } + + pub fn access_modifier(&self) -> String { + match self.access_modifier.as_ref() { + Some(value) => value.clone(), + None => "internal".to_string(), + } + } } // Generate C# bindings for the given ComponentInterface, as a string. @@ -93,7 +101,7 @@ pub fn generate_bindings(config: &Config, ci: &ComponentInterface) -> Result { - cs_config: &'a Config, + config: &'a Config, ci: &'a ComponentInterface, // Track included modules for the `include_once()` macro include_once_names: RefCell>, @@ -110,9 +118,9 @@ pub struct TypeAlias { } impl<'a> TypeRenderer<'a> { - fn new(cs_config: &'a Config, ci: &'a ComponentInterface) -> Self { + fn new(config: &'a Config, ci: &'a ComponentInterface) -> Self { Self { - cs_config, + config, ci, include_once_names: RefCell::new(HashSet::new()), imports: RefCell::new(BTreeSet::new()), @@ -124,7 +132,7 @@ impl<'a> TypeRenderer<'a> { fn external_type_package_name(&self, module_path: &str, namespace: &str) -> String { // config overrides are keyed by the crate name, default fallback is the namespace. let crate_name = module_path.split("::").next().unwrap(); - match self.cs_config.external_packages.get(crate_name) { + match self.config.external_packages.get(crate_name) { Some(name) => name.clone(), // unreachable in library mode - all deps are in our config with correct namespace. None => format!("uniffi.{namespace}"), diff --git a/bindgen/templates/CallbackInterfaceTemplate.cs b/bindgen/templates/CallbackInterfaceTemplate.cs index 9c65698..2a6cb8d 100644 --- a/bindgen/templates/CallbackInterfaceTemplate.cs +++ b/bindgen/templates/CallbackInterfaceTemplate.cs @@ -9,7 +9,7 @@ {% if self.include_once_check("CallbackInterfaceRuntime.cs") %}{% include "CallbackInterfaceRuntime.cs" %}{% endif %} {%- call cs::docstring(cbi, 0) %} -public interface {{ type_name }} { +{{ config.access_modifier() }} interface {{ type_name }} { {%- for meth in cbi.methods() %} {%- call cs::docstring(meth, 4) %} {%- call cs::method_throws_annotation(meth.throws_type()) %} diff --git a/bindgen/templates/CustomTypeTemplate.cs b/bindgen/templates/CustomTypeTemplate.cs index 27fac9a..7f55b5b 100644 --- a/bindgen/templates/CustomTypeTemplate.cs +++ b/bindgen/templates/CustomTypeTemplate.cs @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */#} -{%- match cs_config.custom_types.get(name.as_str()) %} +{%- match config.custom_types.get(name.as_str()) %} {%- when None %} {#- Define the type using typealiases to the builtin #} /** diff --git a/bindgen/templates/EnumTemplate.cs b/bindgen/templates/EnumTemplate.cs index 97d0272..435d4b9 100644 --- a/bindgen/templates/EnumTemplate.cs +++ b/bindgen/templates/EnumTemplate.cs @@ -10,7 +10,7 @@ {%- if e.is_flat() %} {%- call cs::docstring(e, 0) %} -public enum {{ type_name }}: int { +{{ config.access_modifier() }} enum {{ type_name }}: int { {% for variant in e.variants() -%} {%- call cs::docstring(variant, 4) %} {{ variant.name()|enum_variant }}{% if !loop.last %},{% endif %} @@ -41,7 +41,7 @@ public override void Write({{ type_name }} value, BigEndianStream stream) { {% else %} {%- call cs::docstring(e, 0) %} -public record {{ type_name }}{% if contains_object_references %}: IDisposable {% endif %} { +{{ config.access_modifier() }} record {{ type_name }}{% if contains_object_references %}: IDisposable {% endif %} { {% for variant in e.variants() -%} {%- call cs::docstring(variant, 4) %} {% if !variant.has_fields() -%} diff --git a/bindgen/templates/ErrorTemplate.cs b/bindgen/templates/ErrorTemplate.cs index 3cd01b8..8e75e6c 100644 --- a/bindgen/templates/ErrorTemplate.cs +++ b/bindgen/templates/ErrorTemplate.cs @@ -8,7 +8,7 @@ {% if e.is_flat() %} {%- call cs::docstring(e, 0) %} -public class {{ type_name }}: UniffiException { +{{ config.access_modifier() }} class {{ type_name }}: UniffiException { {{ type_name }}(string message): base(message) {} // Each variant is a nested class @@ -54,7 +54,7 @@ public override void Write({{ type_name }} value, BigEndianStream stream) { {%- else %} {%- call cs::docstring(e, 0) %} -public class {{ type_name }}: UniffiException{% if contains_object_references %}, IDisposable {% endif %} { +{{ config.access_modifier() }} class {{ type_name }}: UniffiException{% if contains_object_references %}, IDisposable {% endif %} { // Each variant is a nested class {% for variant in e.variants() -%} {%- call cs::docstring(variant, 4) %} diff --git a/bindgen/templates/Helpers.cs b/bindgen/templates/Helpers.cs index 7416150..5ee7429 100644 --- a/bindgen/templates/Helpers.cs +++ b/bindgen/templates/Helpers.cs @@ -24,38 +24,38 @@ public bool IsPanic() { } // Base class for all uniffi exceptions -public class UniffiException: Exception { +{{ config.access_modifier() }} class UniffiException: Exception { public UniffiException(): base() {} public UniffiException(string message): base(message) {} } -public class UndeclaredErrorException: UniffiException { +{{ config.access_modifier() }} class UndeclaredErrorException: UniffiException { public UndeclaredErrorException(string message): base(message) {} } -public class PanicException: UniffiException { +{{ config.access_modifier() }} class PanicException: UniffiException { public PanicException(string message): base(message) {} } -public class AllocationException: UniffiException { +{{ config.access_modifier() }} class AllocationException: UniffiException { public AllocationException(string message): base(message) {} } -public class InternalException: UniffiException { +{{ config.access_modifier() }} class InternalException: UniffiException { public InternalException(string message): base(message) {} } -public class InvalidEnumException: InternalException { +{{ config.access_modifier() }} class InvalidEnumException: InternalException { public InvalidEnumException(string message): base(message) { } } -public class UniffiContractVersionException: UniffiException { +{{ config.access_modifier() }} class UniffiContractVersionException: UniffiException { public UniffiContractVersionException(string message): base(message) { } } -public class UniffiContractChecksumException: UniffiException { +{{ config.access_modifier() }} class UniffiContractChecksumException: UniffiException { public UniffiContractChecksumException(string message): base(message) { } } diff --git a/bindgen/templates/ObjectRuntime.cs b/bindgen/templates/ObjectRuntime.cs index b7f0846..cb081bf 100644 --- a/bindgen/templates/ObjectRuntime.cs +++ b/bindgen/templates/ObjectRuntime.cs @@ -7,7 +7,7 @@ // https://github.com/mozilla/uniffi-rs/blob/0dc031132d9493ca812c3af6e7dd60ad2ea95bf0/uniffi_bindgen/src/bindings/kotlin/templates/ObjectRuntime.kt#L31 // https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.criticalhandle -public abstract class FFIObject: IDisposable where THandle : FFISafeHandle { +{{ config.access_modifier() }} abstract class FFIObject: IDisposable where THandle : FFISafeHandle { private THandle handle; public FFIObject(THandle handle) { @@ -23,7 +23,7 @@ public void Dispose() { } } -public abstract class FFISafeHandle: SafeHandle { +{{ config.access_modifier() }} abstract class FFISafeHandle: SafeHandle { public FFISafeHandle(): base(new IntPtr(0), true) { } diff --git a/bindgen/templates/ObjectTemplate.cs b/bindgen/templates/ObjectTemplate.cs index 0dc03cf..7ca3f20 100644 --- a/bindgen/templates/ObjectTemplate.cs +++ b/bindgen/templates/ObjectTemplate.cs @@ -7,7 +7,7 @@ {%- if self.include_once_check("ObjectRuntime.cs") %}{% include "ObjectRuntime.cs" %}{% endif %} {%- call cs::docstring(obj, 0) %} -public interface I{{ type_name }} { +{{ config.access_modifier() }} interface I{{ type_name }} { {% for meth in obj.methods() -%} {%- call cs::docstring(meth, 4) %} {%- call cs::method_throws_annotation(meth.throws_type()) %} @@ -15,7 +15,7 @@ public interface I{{ type_name }} { {% endfor %} } -public class {{ safe_handle_type }}: FFISafeHandle { +{{ config.access_modifier() }} class {{ safe_handle_type }}: FFISafeHandle { public {{ safe_handle_type }}(): base() { } public {{ safe_handle_type }}(IntPtr pointer): base(pointer) { @@ -29,7 +29,7 @@ override protected bool ReleaseHandle() { } {%- call cs::docstring(obj, 0) %} -public class {{ type_name }}: FFIObject<{{ safe_handle_type }}>, I{{ type_name }} { +{{ config.access_modifier() }} class {{ type_name }}: FFIObject<{{ safe_handle_type }}>, I{{ type_name }} { public {{ type_name }}({{ safe_handle_type }} pointer): base(pointer) {} {%- match obj.primary_constructor() %} diff --git a/bindgen/templates/RecordTemplate.cs b/bindgen/templates/RecordTemplate.cs index f17899f..7b673f9 100644 --- a/bindgen/templates/RecordTemplate.cs +++ b/bindgen/templates/RecordTemplate.cs @@ -13,7 +13,7 @@ /// future ordering changes. /// {%- endif %} -public record {{ type_name }} ( +{{ config.access_modifier() }} record {{ type_name }} ( {%- for field in ordered_fields %} {%- call cs::docstring(field, 4) %} {{ field|type_name }} {{ field.name()|var_name -}} diff --git a/bindgen/templates/wrapper.cs b/bindgen/templates/wrapper.cs index 005e15e..d07457f 100644 --- a/bindgen/templates/wrapper.cs +++ b/bindgen/templates/wrapper.cs @@ -53,9 +53,9 @@ namespace {{ config.namespace() }}; {%- match config.global_methods_class_name %} {%- when Some(class_name) %} -public static class {{ class_name }} { +{{ config.access_modifier() }} static class {{ class_name }} { {%- when None %} -public static class {{ ci.namespace().to_upper_camel_case() }}Methods { +{{ config.access_modifier() }} static class {{ ci.namespace().to_upper_camel_case() }}Methods { {%- endmatch %} {%- for func in ci.function_definitions() %} {%- include "TopLevelFunctionTemplate.cs" %} diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index cfee689..62ac6b6 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -53,3 +53,5 @@ uniffi-bindgen-cs path/to/definitions.udl --config path/to/uniffi.toml [bindings.csharp] namespace = "LibGreeter" ``` + +- `access_modifier` - override the default `internal` access modifier for "exported" uniffi symbols.