Skip to content

Commit

Permalink
Move uniffi_in_range into module
Browse files Browse the repository at this point in the history
  • Loading branch information
saks authored and badboy committed Jun 14, 2023
1 parent cf36641 commit 26f77b4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
26 changes: 13 additions & 13 deletions uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ mod filters {
Ok(nm.to_string().to_shouty_snake_case())
}

pub fn coerce_rb(nm: &str, type_: &Type) -> Result<String, askama::Error> {
pub fn coerce_rb(nm: &str, ns: &str, type_: &Type) -> Result<String, askama::Error> {
Ok(match type_ {
Type::Int8 => format!("uniffi_in_range({nm}, \"i8\", -2**7, 2**7)"),
Type::Int16 => format!("uniffi_in_range({nm}, \"i16\", -2**15, 2**15)"),
Type::Int32 => format!("uniffi_in_range({nm}, \"i32\", -2**31, 2**31)"),
Type::Int64 => format!("uniffi_in_range({nm}, \"i64\", -2**63, 2**63)"),
Type::UInt8 => format!("uniffi_in_range({nm}, \"u8\", 0, 2**8)"),
Type::UInt16 => format!("uniffi_in_range({nm}, \"u16\", 0, 2**16)"),
Type::UInt32 => format!("uniffi_in_range({nm}, \"u32\", 0, 2**32)"),
Type::UInt64 => format!("uniffi_in_range({nm}, \"u64\", 0, 2**64)"),
Type::Int8 => format!("{ns}::uniffi_in_range({nm}, \"i8\", -2**7, 2**7)"),
Type::Int16 => format!("{ns}::uniffi_in_range({nm}, \"i16\", -2**15, 2**15)"),
Type::Int32 => format!("{ns}::uniffi_in_range({nm}, \"i32\", -2**31, 2**31)"),
Type::Int64 => format!("{ns}::uniffi_in_range({nm}, \"i64\", -2**63, 2**63)"),
Type::UInt8 => format!("{ns}::uniffi_in_range({nm}, \"u8\", 0, 2**8)"),
Type::UInt16 => format!("{ns}::uniffi_in_range({nm}, \"u16\", 0, 2**16)"),
Type::UInt32 => format!("{ns}::uniffi_in_range({nm}, \"u32\", 0, 2**32)"),
Type::UInt64 => format!("{ns}::uniffi_in_range({nm}, \"u64\", 0, 2**64)"),
Type::Float32 | Type::Float64 => format!("{nm}.to_f"),
Type::Boolean => format!("{nm} ? true : false"),
Type::Object { .. } | Type::Enum(_) | Type::Error(_) | Type::Record(_) => {
Expand All @@ -177,18 +177,18 @@ mod filters {
Type::String | Type::Bytes => format!("{nm}.to_s"),
Type::Timestamp | Type::Duration => nm.to_string(),
Type::CallbackInterface(_) => panic!("No support for coercing callback interfaces yet"),
Type::Optional(t) => format!("({nm} ? {} : nil)", coerce_rb(nm, t)?),
Type::Optional(t) => format!("({nm} ? {} : nil)", coerce_rb(nm, ns, t)?),
Type::Sequence(t) => {
let coerce_code = coerce_rb("v", t)?;
let coerce_code = coerce_rb("v", ns, t)?;
if coerce_code == "v" {
nm.to_string()
} else {
format!("{nm}.map {{ |v| {coerce_code} }}")
}
}
Type::Map(_k, t) => {
let k_coerce_code = coerce_rb("k", &Type::String)?;
let v_coerce_code = coerce_rb("v", t)?;
let k_coerce_code = coerce_rb("k", ns, &Type::String)?;
let v_coerce_code = coerce_rb("v", ns, t)?;

if k_coerce_code == "k" && v_coerce_code == "v" {
nm.to_string()
Expand Down
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/ruby/templates/Helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def uniffi_in_range(i, type_name, min, max)
def self.uniffi_in_range(i, type_name, min, max)
i = i.to_i
raise RangeError, "#{type_name} requires #{min} <= value < #{max}" unless (min <= i && i < max)
i
Expand Down
16 changes: 8 additions & 8 deletions uniffi_bindgen/src/bindings/ruby/templates/RustBufferBuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,56 +34,56 @@ def write(value)
{% when Type::Int8 -%}

def write_I8(v)
v = uniffi_in_range(v, "i8", -2**7, 2**7)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "i8", -2**7, 2**7)
pack_into(1, 'c', v)
end

{% when Type::UInt8 -%}
def write_U8(v)
v = uniffi_in_range(v, "u8", 0, 2**8)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "u8", 0, 2**8)
pack_into(1, 'c', v)
end

{% when Type::Int16 -%}
def write_I16(v)
v = uniffi_in_range(v, "i16", -2**15, 2**15)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "i16", -2**15, 2**15)
pack_into(2, 's>', v)
end

{% when Type::UInt16 -%}
def write_U16(v)
v = uniffi_in_range(v, "u16", 0, 2**16)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "u16", 0, 2**16)
pack_into(2, 'S>', v)
end

{% when Type::Int32 -%}
def write_I32(v)
v = uniffi_in_range(v, "i32", -2**31, 2**31)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "i32", -2**31, 2**31)
pack_into(4, 'l>', v)
end

{% when Type::UInt32 -%}
def write_U32(v)
v = uniffi_in_range(v, "u32", 0, 2**32)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "u32", 0, 2**32)
pack_into(4, 'L>', v)
end

{% when Type::Int64 -%}
def write_I64(v)
v = uniffi_in_range(v, "i64", -2**63, 2**63)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "i64", -2**63, 2**63)
pack_into(8, 'q>', v)
end

{% when Type::UInt64 -%}
def write_U64(v)
v = uniffi_in_range(v, "u64", 0, 2**64)
v = {{ ci.namespace()|class_name_rb }}::uniffi_in_range(v, "u64", 0, 2**64)
pack_into(8, 'Q>', v)
end

Expand Down
4 changes: 2 additions & 2 deletions uniffi_bindgen/src/bindings/ruby/templates/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@

{%- macro coerce_args(func) %}
{%- for arg in func.arguments() %}
{{ arg.name() }} = {{ arg.name()|coerce_rb(arg.as_type().borrow()) -}}
{{ arg.name() }} = {{ arg.name()|coerce_rb(ci.namespace()|class_name_rb, arg.as_type().borrow()) -}}
{% endfor -%}
{%- endmacro -%}

{%- macro coerce_args_extra_indent(func) %}
{%- for arg in func.arguments() %}
{{ arg.name() }} = {{ arg.name()|coerce_rb(arg.as_type().borrow()) }}
{{ arg.name() }} = {{ arg.name()|coerce_rb(ci.namespace()|class_name_rb, arg.as_type().borrow()) }}
{%- endfor %}
{%- endmacro -%}
3 changes: 2 additions & 1 deletion uniffi_bindgen/src/bindings/ruby/templates/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

require 'ffi'

{% include "Helpers.rb" %}

module {{ ci.namespace()|class_name_rb }}
{% include "Helpers.rb" %}
{% include "RustBufferTemplate.rb" %}
{% include "RustBufferStream.rb" %}
{% include "RustBufferBuilder.rb" %}
Expand Down

0 comments on commit 26f77b4

Please sign in to comment.