From d0d70c079e279656d43b7247a5c7dce1622d0b2a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 10 Nov 2024 15:08:39 +0100 Subject: [PATCH] add uniffi CharGrid --- crates/servicepoint_binding_uniffi/Cargo.toml | 2 +- .../ServicePoint.Tests/CharGridTests.cs | 31 + .../csharp/ServicePoint.Tests/CommandTests.cs | 2 +- .../servicepoint_binding_uniffi.cs | 543 ++++++++++++++++++ .../servicepoint_binding_uniffi/src/bitmap.rs | 2 +- .../src/brightness_grid.rs | 8 +- .../src/char_grid.rs | 163 ++++++ .../src/cp437_grid.rs | 13 +- crates/servicepoint_binding_uniffi/src/lib.rs | 1 + 9 files changed, 755 insertions(+), 10 deletions(-) create mode 100644 crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CharGridTests.cs create mode 100644 crates/servicepoint_binding_uniffi/src/char_grid.rs diff --git a/crates/servicepoint_binding_uniffi/Cargo.toml b/crates/servicepoint_binding_uniffi/Cargo.toml index 58248c3..8cce76c 100644 --- a/crates/servicepoint_binding_uniffi/Cargo.toml +++ b/crates/servicepoint_binding_uniffi/Cargo.toml @@ -17,7 +17,7 @@ uniffi = { version = "0.25.0", features = ["build"] } [dependencies] uniffi = { version = "0.25.0" } -thiserror = "1.0.66" +thiserror.workspace = true [dependencies.servicepoint] version = "0.11.0" diff --git a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CharGridTests.cs b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CharGridTests.cs new file mode 100644 index 0000000..de849c6 --- /dev/null +++ b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CharGridTests.cs @@ -0,0 +1,31 @@ +namespace ServicePoint.Tests; + +public class CharGridTests +{ + [Fact] + public void BasicFunctions() + { + var grid = new CharGrid(8, 2); + Assert.Equal("\0", grid.Get(0, 0)); + Assert.Equal("\0", grid.Get(grid.Width() - 1, grid.Height() - 1)); + grid.Fill(" "); + Assert.Equal(" ", grid.Get(1, 1)); + grid.Set(1, 1, "-"); + Assert.Equal("-", grid.Get(1, 1)); + Assert.Throws(() => grid.Get(8, 2)); + } + + [Fact] + public void RowAndCol() + { + var grid = new CharGrid(3, 2); + Assert.Equal("\0\0\0", grid.GetRow(0)); + grid.Fill(" "); + Assert.Equal(" ", grid.GetCol(1)); + Assert.Throws(() => grid.GetCol(3)); + Assert.Throws(() => grid.SetRow(1, "Text")); + grid.SetRow(1, "Foo"); + Assert.Equal("Foo", grid.GetRow(1)); + Assert.Equal(" o", grid.GetCol(2)); + } +} diff --git a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CommandTests.cs b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CommandTests.cs index b17deb3..4687162 100644 --- a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CommandTests.cs +++ b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint.Tests/CommandTests.cs @@ -37,6 +37,6 @@ public void HardResetSendable() [Fact] public void BitmapLinearWinSendable() { - _connection.Send(Command.BitmapLinearWin(0, 0, Bitmap.NewMaxSized())); + _connection.Send(Command.BitmapLinearWin(0, 0, Bitmap.NewMaxSized(), CompressionCode.Uncompressed)); } } diff --git a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs index 2a7679c..f9a6975 100644 --- a/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs +++ b/crates/servicepoint_binding_uniffi/libraries/csharp/ServicePoint/servicepoint_binding_uniffi.cs @@ -563,6 +563,71 @@ public static extern void uniffi_servicepoint_binding_uniffi_fn_method_brightnes public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_brightnessgrid_width(BrightnessGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err ); + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_free_chargrid( + IntPtr ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern CharGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_clone(CharGridSafeHandle @other,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern CharGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_load(RustBuffer @data,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern CharGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_new(ulong @width,ulong @height,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern RustBuffer uniffi_servicepoint_binding_uniffi_fn_method_chargrid_as_string(CharGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern sbyte uniffi_servicepoint_binding_uniffi_fn_method_chargrid_equals(CharGridSafeHandle @ptr,CharGridSafeHandle @other,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_chargrid_fill(CharGridSafeHandle @ptr,RustBuffer @value,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern RustBuffer uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get(CharGridSafeHandle @ptr,ulong @x,ulong @y,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern RustBuffer uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get_col(CharGridSafeHandle @ptr,ulong @x,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern RustBuffer uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get_row(CharGridSafeHandle @ptr,ulong @y,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_chargrid_height(CharGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set(CharGridSafeHandle @ptr,ulong @x,ulong @y,RustBuffer @value,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set_col(CharGridSafeHandle @ptr,ulong @x,RustBuffer @col,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern void uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set_row(CharGridSafeHandle @ptr,ulong @y,RustBuffer @row,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern Cp437GridSafeHandle uniffi_servicepoint_binding_uniffi_fn_method_chargrid_to_cp437(CharGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_chargrid_width(CharGridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + [DllImport("servicepoint_binding_uniffi")] public static extern void uniffi_servicepoint_binding_uniffi_fn_free_command( IntPtr ptr,ref RustCallStatus _uniffi_out_err @@ -678,6 +743,10 @@ public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_cp437gri public static extern void uniffi_servicepoint_binding_uniffi_fn_method_cp437grid_set(Cp437GridSafeHandle @ptr,ulong @x,ulong @y,byte @value,ref RustCallStatus _uniffi_out_err ); + [DllImport("servicepoint_binding_uniffi")] + public static extern CharGridSafeHandle uniffi_servicepoint_binding_uniffi_fn_method_cp437grid_to_utf8(Cp437GridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ulong uniffi_servicepoint_binding_uniffi_fn_method_cp437grid_width(Cp437GridSafeHandle @ptr,ref RustCallStatus _uniffi_out_err ); @@ -990,6 +1059,54 @@ public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_b public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_width( ); + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_as_string( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_equals( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_fill( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_col( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_row( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_height( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_col( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_row( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_to_cp437( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_width( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_command_equals( ); @@ -1022,6 +1139,10 @@ public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_c public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_set( ); + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_to_utf8( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_width( ); @@ -1066,6 +1187,18 @@ public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_construc public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_new( ); + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_clone( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_load( + ); + + [DllImport("servicepoint_binding_uniffi")] + public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_new( + ); + [DllImport("servicepoint_binding_uniffi")] public static extern ushort uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear( ); @@ -1268,6 +1401,78 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_brightnessgrid_width` checksum `26384`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_as_string(); + if (checksum != 46581) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_as_string` checksum `46581`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_equals(); + if (checksum != 17533) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_equals` checksum `17533`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_fill(); + if (checksum != 56996) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_fill` checksum `56996`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get(); + if (checksum != 1334) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get` checksum `1334`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_col(); + if (checksum != 64158) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_col` checksum `64158`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_row(); + if (checksum != 39411) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_get_row` checksum `39411`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_height(); + if (checksum != 13068) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_height` checksum `13068`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set(); + if (checksum != 64815) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set` checksum `64815`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_col(); + if (checksum != 43727) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_col` checksum `43727`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_row(); + if (checksum != 19756) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_set_row` checksum `19756`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_to_cp437(); + if (checksum != 19261) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_to_cp437` checksum `19261`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_width(); + if (checksum != 48963) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_chargrid_width` checksum `48963`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_command_equals(); if (checksum != 20763) { @@ -1316,6 +1521,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_set` checksum `8371`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_to_utf8(); + if (checksum != 21516) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_to_utf8` checksum `21516`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_method_cp437grid_width(); if (checksum != 36872) { @@ -1382,6 +1593,24 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_brightnessgrid_new` checksum `4979`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_clone(); + if (checksum != 61241) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_clone` checksum `61241`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_load(); + if (checksum != 47815) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_load` checksum `47815`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_new(); + if (checksum != 13303) { + throw new UniffiContractChecksumException($"ServicePoint: uniffi bindings expected function `uniffi_servicepoint_binding_uniffi_checksum_constructor_chargrid_new` checksum `13303`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_servicepoint_binding_uniffi_checksum_constructor_command_bitmap_linear(); if (checksum != 18079) { @@ -2122,6 +2351,196 @@ public override void Write(BrightnessGrid value, BigEndianStream stream) { +public interface ICharGrid { + + String AsString(); + + bool Equals(CharGrid @other); + + /// + void Fill(String @value); + + String Get(ulong @x, ulong @y); + + /// + String GetCol(ulong @x); + + /// + String GetRow(ulong @y); + + ulong Height(); + + /// + void Set(ulong @x, ulong @y, String @value); + + /// + void SetCol(ulong @x, String @col); + + /// + void SetRow(ulong @y, String @row); + + Cp437Grid ToCp437(); + + ulong Width(); + +} + +public class CharGridSafeHandle: FFISafeHandle { + public CharGridSafeHandle(): base() { + } + public CharGridSafeHandle(IntPtr pointer): base(pointer) { + } + override protected bool ReleaseHandle() { + _UniffiHelpers.RustCall((ref RustCallStatus status) => { + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_free_chargrid(this.handle, ref status); + }); + return true; + } +} +public class CharGrid: FFIObject, ICharGrid { + public CharGrid(CharGridSafeHandle pointer): base(pointer) {} + public CharGrid(ulong @width, ulong @height) : + this( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_new(FfiConverterUInt64.INSTANCE.Lower(@width), FfiConverterUInt64.INSTANCE.Lower(@height), ref _status) +)) {} + + + public String AsString() { + return FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_as_string(this.GetHandle(), ref _status) +)); + } + + public bool Equals(CharGrid @other) { + return FfiConverterBoolean.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_equals(this.GetHandle(), FfiConverterTypeCharGrid.INSTANCE.Lower(@other), ref _status) +)); + } + + /// + public void Fill(String @value) { + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_fill(this.GetHandle(), FfiConverterString.INSTANCE.Lower(@value), ref _status) +); + } + + + public String Get(ulong @x, ulong @y) { + return FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterUInt64.INSTANCE.Lower(@y), ref _status) +)); + } + + /// + public String GetCol(ulong @x) { + return FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get_col(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), ref _status) +)); + } + + /// + public String GetRow(ulong @y) { + return FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_get_row(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@y), ref _status) +)); + } + + public ulong Height() { + return FfiConverterUInt64.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_height(this.GetHandle(), ref _status) +)); + } + + /// + public void Set(ulong @x, ulong @y, String @value) { + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterUInt64.INSTANCE.Lower(@y), FfiConverterString.INSTANCE.Lower(@value), ref _status) +); + } + + + /// + public void SetCol(ulong @x, String @col) { + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set_col(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@x), FfiConverterString.INSTANCE.Lower(@col), ref _status) +); + } + + + /// + public void SetRow(ulong @y, String @row) { + _UniffiHelpers.RustCallWithError(FfiConverterTypeCharGridException.INSTANCE, (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_set_row(this.GetHandle(), FfiConverterUInt64.INSTANCE.Lower(@y), FfiConverterString.INSTANCE.Lower(@row), ref _status) +); + } + + + public Cp437Grid ToCp437() { + return FfiConverterTypeCp437Grid.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_to_cp437(this.GetHandle(), ref _status) +)); + } + + public ulong Width() { + return FfiConverterUInt64.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_chargrid_width(this.GetHandle(), ref _status) +)); + } + + + + public static CharGrid Clone(CharGrid @other) { + return new CharGrid( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_clone(FfiConverterTypeCharGrid.INSTANCE.Lower(@other), ref _status) +)); + } + + public static CharGrid Load(String @data) { + return new CharGrid( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_constructor_chargrid_load(FfiConverterString.INSTANCE.Lower(@data), ref _status) +)); + } + + +} + +class FfiConverterTypeCharGrid: FfiConverter { + public static FfiConverterTypeCharGrid INSTANCE = new FfiConverterTypeCharGrid(); + + public override CharGridSafeHandle Lower(CharGrid value) { + return value.GetHandle(); + } + + public override CharGrid Lift(CharGridSafeHandle value) { + return new CharGrid(value); + } + + public override CharGrid Read(BigEndianStream stream) { + return Lift(new CharGridSafeHandle(new IntPtr(stream.ReadLong()))); + } + + public override int AllocationSize(CharGrid value) { + return 8; + } + + public override void Write(CharGrid value, BigEndianStream stream) { + stream.WriteLong(Lower(value).DangerousGetRawFfiValue().ToInt64()); + } +} + + + public interface ICommand { bool Equals(Command @other); @@ -2355,6 +2774,8 @@ public interface ICp437Grid { void Set(ulong @x, ulong @y, byte @value); + CharGrid ToUtf8(); + ulong Width(); } @@ -2422,6 +2843,13 @@ public void Set(ulong @x, ulong @y, byte @value) { } + public CharGrid ToUtf8() { + return FfiConverterTypeCharGrid.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref RustCallStatus _status) => + _UniFFILib.uniffi_servicepoint_binding_uniffi_fn_method_cp437grid_to_utf8(this.GetHandle(), ref _status) +)); + } + public ulong Width() { return FfiConverterUInt64.INSTANCE.Lift( _UniffiHelpers.RustCall( (ref RustCallStatus _status) => @@ -2476,6 +2904,121 @@ public override void Write(Cp437Grid value, BigEndianStream stream) { +public class CharGridException: UniffiException { + // Each variant is a nested class + + + public class StringNotOneChar : CharGridException { + // Members + public String @value; + + // Constructor + public StringNotOneChar( + String @value) { + this.@value = @value; + } + } + + + public class InvalidSeriesLength : CharGridException { + // Members + public ulong @actual; + public ulong @expected; + + // Constructor + public InvalidSeriesLength( + ulong @actual, + ulong @expected) { + this.@actual = @actual; + this.@expected = @expected; + } + } + + + public class OutOfBounds : CharGridException { + // Members + public ulong @index; + public ulong @size; + + // Constructor + public OutOfBounds( + ulong @index, + ulong @size) { + this.@index = @index; + this.@size = @size; + } + } + + + +} + +class FfiConverterTypeCharGridException : FfiConverterRustBuffer, CallStatusErrorHandler { + public static FfiConverterTypeCharGridException INSTANCE = new FfiConverterTypeCharGridException(); + + public override CharGridException Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: + return new CharGridException.StringNotOneChar( + FfiConverterString.INSTANCE.Read(stream)); + case 2: + return new CharGridException.InvalidSeriesLength( + FfiConverterUInt64.INSTANCE.Read(stream), + FfiConverterUInt64.INSTANCE.Read(stream)); + case 3: + return new CharGridException.OutOfBounds( + FfiConverterUInt64.INSTANCE.Read(stream), + FfiConverterUInt64.INSTANCE.Read(stream)); + default: + throw new InternalException(String.Format("invalid error value '{0}' in FfiConverterTypeCharGridException.Read()", value)); + } + } + + public override int AllocationSize(CharGridException value) { + switch (value) { + case CharGridException.StringNotOneChar variant_value: + return 4 + + FfiConverterString.INSTANCE.AllocationSize(variant_value.@value); + case CharGridException.InvalidSeriesLength variant_value: + return 4 + + FfiConverterUInt64.INSTANCE.AllocationSize(variant_value.@actual) + + FfiConverterUInt64.INSTANCE.AllocationSize(variant_value.@expected); + case CharGridException.OutOfBounds variant_value: + return 4 + + FfiConverterUInt64.INSTANCE.AllocationSize(variant_value.@index) + + FfiConverterUInt64.INSTANCE.AllocationSize(variant_value.@size); + default: + throw new InternalException(String.Format("invalid error value '{0}' in FfiConverterTypeCharGridException.AllocationSize()", value)); + } + } + + public override void Write(CharGridException value, BigEndianStream stream) { + switch (value) { + case CharGridException.StringNotOneChar variant_value: + stream.WriteInt(1); + FfiConverterString.INSTANCE.Write(variant_value.@value, stream); + break; + case CharGridException.InvalidSeriesLength variant_value: + stream.WriteInt(2); + FfiConverterUInt64.INSTANCE.Write(variant_value.@actual, stream); + FfiConverterUInt64.INSTANCE.Write(variant_value.@expected, stream); + break; + case CharGridException.OutOfBounds variant_value: + stream.WriteInt(3); + FfiConverterUInt64.INSTANCE.Write(variant_value.@index, stream); + FfiConverterUInt64.INSTANCE.Write(variant_value.@size, stream); + break; + default: + throw new InternalException(String.Format("invalid error value '{0}' in FfiConverterTypeCharGridException.Write()", value)); + } + } +} + + + + + public enum CompressionCode: int { Uncompressed, diff --git a/crates/servicepoint_binding_uniffi/src/bitmap.rs b/crates/servicepoint_binding_uniffi/src/bitmap.rs index 394b8a0..1291f7b 100644 --- a/crates/servicepoint_binding_uniffi/src/bitmap.rs +++ b/crates/servicepoint_binding_uniffi/src/bitmap.rs @@ -70,7 +70,7 @@ impl Bitmap { let b = other.actual.read().unwrap(); *a == *b } - + pub fn copy_raw(&self) -> Vec { self.actual.read().unwrap().data_ref().to_vec() } diff --git a/crates/servicepoint_binding_uniffi/src/brightness_grid.rs b/crates/servicepoint_binding_uniffi/src/brightness_grid.rs index 6660fdf..afa0e3f 100644 --- a/crates/servicepoint_binding_uniffi/src/brightness_grid.rs +++ b/crates/servicepoint_binding_uniffi/src/brightness_grid.rs @@ -75,6 +75,12 @@ impl BrightnessGrid { } pub fn copy_raw(&self) -> Vec { - self.actual.read().unwrap().data_ref().iter().map(u8::from).collect() + self.actual + .read() + .unwrap() + .data_ref() + .iter() + .map(u8::from) + .collect() } } diff --git a/crates/servicepoint_binding_uniffi/src/char_grid.rs b/crates/servicepoint_binding_uniffi/src/char_grid.rs new file mode 100644 index 0000000..9dafce4 --- /dev/null +++ b/crates/servicepoint_binding_uniffi/src/char_grid.rs @@ -0,0 +1,163 @@ +use servicepoint::{Grid, SeriesError}; +use std::convert::Into; +use std::sync::{Arc, RwLock}; +use crate::cp437_grid::Cp437Grid; + +#[derive(uniffi::Object)] +pub struct CharGrid { + pub(crate) actual: RwLock, +} + +#[derive(uniffi::Error, thiserror::Error, Debug)] +pub enum CharGridError { + #[error("Exactly one character was expected, but {value:?} was provided")] + StringNotOneChar { value: String }, + #[error("The provided series was expected to have a length of {expected}, but was {actual}")] + InvalidSeriesLength { actual: u64, expected: u64 }, + #[error("The index {index} was out of bounds for size {size}")] + OutOfBounds { index: u64, size: u64 }, +} + +#[uniffi::export] +impl CharGrid { + #[uniffi::constructor] + pub fn new(width: u64, height: u64) -> Arc { + Self::internal_new(servicepoint::CharGrid::new( + width as usize, + height as usize, + )) + } + + #[uniffi::constructor] + pub fn load(data: String) -> Arc { + Self::internal_new(servicepoint::CharGrid::from(&*data)) + } + + #[uniffi::constructor] + pub fn clone(other: &Arc) -> Arc { + Self::internal_new(other.actual.read().unwrap().clone()) + } + + pub fn set( + &self, + x: u64, + y: u64, + value: String, + ) -> Result<(), CharGridError> { + let value = Self::str_to_char(value)?; + self.actual + .write() + .unwrap() + .set(x as usize, y as usize, value); + Ok(()) + } + + pub fn get(&self, x: u64, y: u64) -> String { + self.actual + .read() + .unwrap() + .get(x as usize, y as usize) + .into() + } + + pub fn fill(&self, value: String) -> Result<(), CharGridError> { + let value = Self::str_to_char(value)?; + self.actual.write().unwrap().fill(value); + Ok(()) + } + + pub fn width(&self) -> u64 { + self.actual.read().unwrap().width() as u64 + } + + pub fn height(&self) -> u64 { + self.actual.read().unwrap().height() as u64 + } + + pub fn equals(&self, other: &CharGrid) -> bool { + let a = self.actual.read().unwrap(); + let b = other.actual.read().unwrap(); + *a == *b + } + + pub fn as_string(&self) -> String { + let grid = self.actual.read().unwrap(); + String::from(&*grid) + } + + pub fn set_row(&self, y: u64, row: String) -> Result<(), CharGridError> { + self.actual + .write() + .unwrap() + .set_row(y as usize, &*row.chars().collect::>()) + .map_err(CharGridError::from) + } + + pub fn set_col(&self, x: u64, col: String) -> Result<(), CharGridError> { + self.actual + .write() + .unwrap() + .set_row(x as usize, &*col.chars().collect::>()) + .map_err(CharGridError::from) + } + + pub fn get_row(&self, y: u64) -> Result { + self.actual + .read() + .unwrap() + .get_row(y as usize) + .map(move |vec| String::from_iter(vec)) + .ok_or(CharGridError::OutOfBounds {index: y, size: self.height()}) + } + + pub fn get_col(&self, x: u64) -> Result { + self.actual + .read() + .unwrap() + .get_col(x as usize) + .map(move |vec| String::from_iter(vec)) + .ok_or(CharGridError::OutOfBounds {index: x, size: self.width()}) + } + + pub fn to_cp437(&self) -> Arc { + Cp437Grid::internal_new(servicepoint::Cp437Grid::from(&*self.actual.read().unwrap())) + } +} + +impl CharGrid { + pub(crate) fn internal_new(actual: servicepoint::CharGrid) -> Arc { + Arc::new(Self { + actual: RwLock::new(actual), + }) + } + + fn str_to_char(value: String) -> Result { + if value.len() != 1 { + return Err(CharGridError::StringNotOneChar { + value, + }); + } + + let value = value.chars().nth(0).unwrap(); + Ok(value) + } +} + +impl From for CharGridError { + fn from(e: SeriesError) -> Self { + match e { + SeriesError::OutOfBounds { index, size } => { + CharGridError::OutOfBounds { + index: index as u64, + size: size as u64, + } + } + SeriesError::InvalidLength { actual, expected } => { + CharGridError::InvalidSeriesLength { + actual: actual as u64, + expected: expected as u64, + } + } + } + } +} diff --git a/crates/servicepoint_binding_uniffi/src/cp437_grid.rs b/crates/servicepoint_binding_uniffi/src/cp437_grid.rs index fa11d92..3819176 100644 --- a/crates/servicepoint_binding_uniffi/src/cp437_grid.rs +++ b/crates/servicepoint_binding_uniffi/src/cp437_grid.rs @@ -1,5 +1,6 @@ use servicepoint::{DataRef, Grid}; use std::sync::{Arc, RwLock}; +use crate::char_grid::CharGrid; #[derive(uniffi::Object)] pub struct Cp437Grid { @@ -7,7 +8,7 @@ pub struct Cp437Grid { } impl Cp437Grid { - fn internal_new(actual: servicepoint::Cp437Grid) -> Arc { + pub(crate) fn internal_new(actual: servicepoint::Cp437Grid) -> Arc { Arc::new(Self { actual: RwLock::new(actual), }) @@ -46,11 +47,7 @@ impl Cp437Grid { } pub fn get(&self, x: u64, y: u64) -> u8 { - self.actual - .read() - .unwrap() - .get(x as usize, y as usize) - .into() + self.actual.read().unwrap().get(x as usize, y as usize) } pub fn fill(&self, value: u8) { @@ -73,4 +70,8 @@ impl Cp437Grid { pub fn copy_raw(&self) -> Vec { self.actual.read().unwrap().data_ref().to_vec() } + + pub fn to_utf8(&self) -> Arc { + CharGrid::internal_new(servicepoint::CharGrid::from(&*self.actual.read().unwrap())) + } } diff --git a/crates/servicepoint_binding_uniffi/src/lib.rs b/crates/servicepoint_binding_uniffi/src/lib.rs index 702cd00..29564aa 100644 --- a/crates/servicepoint_binding_uniffi/src/lib.rs +++ b/crates/servicepoint_binding_uniffi/src/lib.rs @@ -3,6 +3,7 @@ uniffi::setup_scaffolding!(); mod bitmap; mod bitvec; mod brightness_grid; +mod char_grid; mod command; mod compression_code; mod connection;