From 758ac098ff9fab33517a2ad305e8bc464a798782 Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Sun, 14 Apr 2024 00:19:40 -0700 Subject: [PATCH] Move Rust bridge to use parameters --- lib/compressor.dart | 10 ++- lib/src/rust/api/compressor.dart | 62 +++++++++++---- lib/src/rust/api/simple.dart | 30 ------- lib/src/rust/frb_generated.dart | 116 +++++++++++++++++----------- lib/src/rust/frb_generated.io.dart | 19 +++++ lib/src/rust/frb_generated.web.dart | 19 +++++ rust/src/api/compressor.rs | 38 ++++----- rust/src/frb_generated.rs | 79 +++++++++++++++---- 8 files changed, 247 insertions(+), 126 deletions(-) delete mode 100644 lib/src/rust/api/simple.dart diff --git a/lib/compressor.dart b/lib/compressor.dart index 36f6568..3036487 100644 --- a/lib/compressor.dart +++ b/lib/compressor.dart @@ -19,9 +19,7 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) { callback(imageFile.copyWith(status: ImageFileStatus.compressing)); final timer = Stopwatch()..start(); - var result = await processImg( - path: imageFile.path, - outPath: outPath, + var params = Parameters( jpegQuality: config.qualityJPEG, pngQuality: config.qualityPNG, gifQuality: config.qualityGIF, @@ -30,6 +28,12 @@ void compressor(ImageFile imageFile, void Function(ImageFile) callback) { resizeWidth: config.maxWidth, resizeHeight: config.maxHeight, ); + + var result = await processImg( + path: imageFile.path, + outPath: outPath, + parameters: params, + ); timer.stop(); debugPrint( 'Compressed ${imageFile.file} in ${timer.elapsedMilliseconds}ms'); diff --git a/lib/src/rust/api/compressor.dart b/lib/src/rust/api/compressor.dart index 7ca657d..8f1c0b6 100644 --- a/lib/src/rust/api/compressor.dart +++ b/lib/src/rust/api/compressor.dart @@ -9,22 +9,50 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; Future processImg( {required String path, required String outPath, - required int jpegQuality, - required int pngQuality, - required int webpQuality, - required int gifQuality, - required bool resize, - required int resizeWidth, - required int resizeHeight, + required Parameters parameters, dynamic hint}) => RustLib.instance.api.processImg( - path: path, - outPath: outPath, - jpegQuality: jpegQuality, - pngQuality: pngQuality, - webpQuality: webpQuality, - gifQuality: gifQuality, - resize: resize, - resizeWidth: resizeWidth, - resizeHeight: resizeHeight, - hint: hint); + path: path, outPath: outPath, parameters: parameters, hint: hint); + +class Parameters { + final int jpegQuality; + final int pngQuality; + final int webpQuality; + final int gifQuality; + final bool resize; + final int resizeWidth; + final int resizeHeight; + + const Parameters({ + required this.jpegQuality, + required this.pngQuality, + required this.webpQuality, + required this.gifQuality, + required this.resize, + required this.resizeWidth, + required this.resizeHeight, + }); + + @override + int get hashCode => + jpegQuality.hashCode ^ + pngQuality.hashCode ^ + webpQuality.hashCode ^ + gifQuality.hashCode ^ + resize.hashCode ^ + resizeWidth.hashCode ^ + resizeHeight.hashCode; + + @override + bool operator ==(Object other) => + identical(this, other) || + other is Parameters && + runtimeType == other.runtimeType && + jpegQuality == other.jpegQuality && + pngQuality == other.pngQuality && + webpQuality == other.webpQuality && + gifQuality == other.gifQuality && + resize == other.resize && + resizeWidth == other.resizeWidth && + resizeHeight == other.resizeHeight; +} diff --git a/lib/src/rust/api/simple.dart b/lib/src/rust/api/simple.dart deleted file mode 100644 index 7ca657d..0000000 --- a/lib/src/rust/api/simple.dart +++ /dev/null @@ -1,30 +0,0 @@ -// This file is automatically generated, so please do not edit it. -// Generated by `flutter_rust_bridge`@ 2.0.0-dev.31. - -// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import - -import '../frb_generated.dart'; -import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart'; - -Future processImg( - {required String path, - required String outPath, - required int jpegQuality, - required int pngQuality, - required int webpQuality, - required int gifQuality, - required bool resize, - required int resizeWidth, - required int resizeHeight, - dynamic hint}) => - RustLib.instance.api.processImg( - path: path, - outPath: outPath, - jpegQuality: jpegQuality, - pngQuality: pngQuality, - webpQuality: webpQuality, - gifQuality: gifQuality, - resize: resize, - resizeWidth: resizeWidth, - resizeHeight: resizeHeight, - hint: hint); diff --git a/lib/src/rust/frb_generated.dart b/lib/src/rust/frb_generated.dart index fdf3bda..c5a6093 100644 --- a/lib/src/rust/frb_generated.dart +++ b/lib/src/rust/frb_generated.dart @@ -69,13 +69,7 @@ abstract class RustLibApi extends BaseApi { Future processImg( {required String path, required String outPath, - required int jpegQuality, - required int pngQuality, - required int webpQuality, - required int gifQuality, - required bool resize, - required int resizeWidth, - required int resizeHeight, + required Parameters parameters, dynamic hint}); } @@ -115,26 +109,14 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { Future processImg( {required String path, required String outPath, - required int jpegQuality, - required int pngQuality, - required int webpQuality, - required int gifQuality, - required bool resize, - required int resizeWidth, - required int resizeHeight, + required Parameters parameters, dynamic hint}) { return handler.executeNormal(NormalTask( callFfi: (port_) { final serializer = SseSerializer(generalizedFrbRustBinding); sse_encode_String(path, serializer); sse_encode_String(outPath, serializer); - sse_encode_u_32(jpegQuality, serializer); - sse_encode_u_32(pngQuality, serializer); - sse_encode_u_32(webpQuality, serializer); - sse_encode_u_32(gifQuality, serializer); - sse_encode_bool(resize, serializer); - sse_encode_u_32(resizeWidth, serializer); - sse_encode_u_32(resizeHeight, serializer); + sse_encode_box_autoadd_parameters(parameters, serializer); pdeCallFfi(generalizedFrbRustBinding, serializer, funcId: 1, port: port_); }, @@ -143,17 +125,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { decodeErrorData: null, ), constMeta: kProcessImgConstMeta, - argValues: [ - path, - outPath, - jpegQuality, - pngQuality, - webpQuality, - gifQuality, - resize, - resizeWidth, - resizeHeight - ], + argValues: [path, outPath, parameters], apiImpl: this, hint: hint, )); @@ -161,17 +133,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { TaskConstMeta get kProcessImgConstMeta => const TaskConstMeta( debugName: "process_img", - argNames: [ - "path", - "outPath", - "jpegQuality", - "pngQuality", - "webpQuality", - "gifQuality", - "resize", - "resizeWidth", - "resizeHeight" - ], + argNames: ["path", "outPath", "parameters"], ); @protected @@ -186,12 +148,35 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return raw as bool; } + @protected + Parameters dco_decode_box_autoadd_parameters(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + return dco_decode_parameters(raw); + } + @protected Uint8List dco_decode_list_prim_u_8_strict(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs return raw as Uint8List; } + @protected + Parameters dco_decode_parameters(dynamic raw) { + // Codec=Dco (DartCObject based), see doc to use other codecs + final arr = raw as List; + if (arr.length != 7) + throw Exception('unexpected arr length: expect 7 but see ${arr.length}'); + return Parameters( + jpegQuality: dco_decode_u_32(arr[0]), + pngQuality: dco_decode_u_32(arr[1]), + webpQuality: dco_decode_u_32(arr[2]), + gifQuality: dco_decode_u_32(arr[3]), + resize: dco_decode_bool(arr[4]), + resizeWidth: dco_decode_u_32(arr[5]), + resizeHeight: dco_decode_u_32(arr[6]), + ); + } + @protected int dco_decode_u_32(dynamic raw) { // Codec=Dco (DartCObject based), see doc to use other codecs @@ -223,6 +208,12 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return deserializer.buffer.getUint8() != 0; } + @protected + Parameters sse_decode_box_autoadd_parameters(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + return (sse_decode_parameters(deserializer)); + } + @protected Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -230,6 +221,26 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { return deserializer.buffer.getUint8List(len_); } + @protected + Parameters sse_decode_parameters(SseDeserializer deserializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + var var_jpegQuality = sse_decode_u_32(deserializer); + var var_pngQuality = sse_decode_u_32(deserializer); + var var_webpQuality = sse_decode_u_32(deserializer); + var var_gifQuality = sse_decode_u_32(deserializer); + var var_resize = sse_decode_bool(deserializer); + var var_resizeWidth = sse_decode_u_32(deserializer); + var var_resizeHeight = sse_decode_u_32(deserializer); + return Parameters( + jpegQuality: var_jpegQuality, + pngQuality: var_pngQuality, + webpQuality: var_webpQuality, + gifQuality: var_gifQuality, + resize: var_resize, + resizeWidth: var_resizeWidth, + resizeHeight: var_resizeHeight); + } + @protected int sse_decode_u_32(SseDeserializer deserializer) { // Codec=Sse (Serialization based), see doc to use other codecs @@ -265,6 +276,13 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { serializer.buffer.putUint8(self ? 1 : 0); } + @protected + void sse_encode_box_autoadd_parameters( + Parameters self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_parameters(self, serializer); + } + @protected void sse_encode_list_prim_u_8_strict( Uint8List self, SseSerializer serializer) { @@ -273,6 +291,18 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi { serializer.buffer.putUint8List(self); } + @protected + void sse_encode_parameters(Parameters self, SseSerializer serializer) { + // Codec=Sse (Serialization based), see doc to use other codecs + sse_encode_u_32(self.jpegQuality, serializer); + sse_encode_u_32(self.pngQuality, serializer); + sse_encode_u_32(self.webpQuality, serializer); + sse_encode_u_32(self.gifQuality, serializer); + sse_encode_bool(self.resize, serializer); + sse_encode_u_32(self.resizeWidth, serializer); + sse_encode_u_32(self.resizeHeight, serializer); + } + @protected void sse_encode_u_32(int self, SseSerializer serializer) { // Codec=Sse (Serialization based), see doc to use other codecs diff --git a/lib/src/rust/frb_generated.io.dart b/lib/src/rust/frb_generated.io.dart index 7f7fea0..e532252 100644 --- a/lib/src/rust/frb_generated.io.dart +++ b/lib/src/rust/frb_generated.io.dart @@ -24,9 +24,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool dco_decode_bool(dynamic raw); + @protected + Parameters dco_decode_box_autoadd_parameters(dynamic raw); + @protected Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); + @protected + Parameters dco_decode_parameters(dynamic raw); + @protected int dco_decode_u_32(dynamic raw); @@ -42,9 +48,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + Parameters sse_decode_box_autoadd_parameters(SseDeserializer deserializer); + @protected Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); + @protected + Parameters sse_decode_parameters(SseDeserializer deserializer); + @protected int sse_decode_u_32(SseDeserializer deserializer); @@ -63,10 +75,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_bool(bool self, SseSerializer serializer); + @protected + void sse_encode_box_autoadd_parameters( + Parameters self, SseSerializer serializer); + @protected void sse_encode_list_prim_u_8_strict( Uint8List self, SseSerializer serializer); + @protected + void sse_encode_parameters(Parameters self, SseSerializer serializer); + @protected void sse_encode_u_32(int self, SseSerializer serializer); diff --git a/lib/src/rust/frb_generated.web.dart b/lib/src/rust/frb_generated.web.dart index acc39ee..fc0ce6f 100644 --- a/lib/src/rust/frb_generated.web.dart +++ b/lib/src/rust/frb_generated.web.dart @@ -23,9 +23,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool dco_decode_bool(dynamic raw); + @protected + Parameters dco_decode_box_autoadd_parameters(dynamic raw); + @protected Uint8List dco_decode_list_prim_u_8_strict(dynamic raw); + @protected + Parameters dco_decode_parameters(dynamic raw); + @protected int dco_decode_u_32(dynamic raw); @@ -41,9 +47,15 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected bool sse_decode_bool(SseDeserializer deserializer); + @protected + Parameters sse_decode_box_autoadd_parameters(SseDeserializer deserializer); + @protected Uint8List sse_decode_list_prim_u_8_strict(SseDeserializer deserializer); + @protected + Parameters sse_decode_parameters(SseDeserializer deserializer); + @protected int sse_decode_u_32(SseDeserializer deserializer); @@ -62,10 +74,17 @@ abstract class RustLibApiImplPlatform extends BaseApiImpl { @protected void sse_encode_bool(bool self, SseSerializer serializer); + @protected + void sse_encode_box_autoadd_parameters( + Parameters self, SseSerializer serializer); + @protected void sse_encode_list_prim_u_8_strict( Uint8List self, SseSerializer serializer); + @protected + void sse_encode_parameters(Parameters self, SseSerializer serializer); + @protected void sse_encode_u_32(int self, SseSerializer serializer); diff --git a/rust/src/api/compressor.rs b/rust/src/api/compressor.rs index e9f3882..895d7ed 100644 --- a/rust/src/api/compressor.rs +++ b/rust/src/api/compressor.rs @@ -2,17 +2,17 @@ use caesium; use image; use image::DynamicImage; -pub fn process_img( - path: String, - out_path: String, - jpeg_quality: u32, - png_quality: u32, - webp_quality: u32, - gif_quality: u32, - resize: bool, - resize_width: u32, - resize_height: u32, -) -> String { +pub struct Parameters { + pub jpeg_quality: u32, + pub png_quality: u32, + pub webp_quality: u32, + pub gif_quality: u32, + pub resize: bool, + pub resize_width: u32, + pub resize_height: u32, +} + +pub fn process_img(path: String, out_path: String, parameters: Parameters) -> String { let result = read_image(path.clone()); if result.is_err() { @@ -24,12 +24,12 @@ pub fn process_img( let mut width = 0; // set the largest dimension to the resize value, only if the image size is larger than the resize value - if resize { - if img.width() > resize_width || img.height() > resize_height { + if parameters.resize { + if img.width() > parameters.resize_width || img.height() > parameters.resize_height { if img.width() > img.height() { - width = resize_width; + width = parameters.resize_width; } else { - height = resize_height; + height = parameters.resize_height; } } } @@ -37,10 +37,10 @@ pub fn process_img( let result = compress_image( path.clone(), out_path, - jpeg_quality, - png_quality, - webp_quality, - gif_quality, + parameters.jpeg_quality, + parameters.png_quality, + parameters.webp_quality, + parameters.gif_quality, width, height, ); diff --git a/rust/src/frb_generated.rs b/rust/src/frb_generated.rs index 01b15ba..a06a3d3 100644 --- a/rust/src/frb_generated.rs +++ b/rust/src/frb_generated.rs @@ -93,26 +93,15 @@ fn wire_process_img_impl( flutter_rust_bridge::for_generated::SseDeserializer::new(message); let api_path = ::sse_decode(&mut deserializer); let api_out_path = ::sse_decode(&mut deserializer); - let api_jpeg_quality = ::sse_decode(&mut deserializer); - let api_png_quality = ::sse_decode(&mut deserializer); - let api_webp_quality = ::sse_decode(&mut deserializer); - let api_gif_quality = ::sse_decode(&mut deserializer); - let api_resize = ::sse_decode(&mut deserializer); - let api_resize_width = ::sse_decode(&mut deserializer); - let api_resize_height = ::sse_decode(&mut deserializer); + let api_parameters = + ::sse_decode(&mut deserializer); deserializer.end(); move |context| { transform_result_sse((move || { Result::<_, ()>::Ok(crate::api::compressor::process_img( api_path, api_out_path, - api_jpeg_quality, - api_png_quality, - api_webp_quality, - api_gif_quality, - api_resize, - api_resize_width, - api_resize_height, + api_parameters, )) })()) } @@ -149,6 +138,28 @@ impl SseDecode for Vec { } } +impl SseDecode for crate::api::compressor::Parameters { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { + let mut var_jpegQuality = ::sse_decode(deserializer); + let mut var_pngQuality = ::sse_decode(deserializer); + let mut var_webpQuality = ::sse_decode(deserializer); + let mut var_gifQuality = ::sse_decode(deserializer); + let mut var_resize = ::sse_decode(deserializer); + let mut var_resizeWidth = ::sse_decode(deserializer); + let mut var_resizeHeight = ::sse_decode(deserializer); + return crate::api::compressor::Parameters { + jpeg_quality: var_jpegQuality, + png_quality: var_pngQuality, + webp_quality: var_webpQuality, + gif_quality: var_gifQuality, + resize: var_resize, + resize_width: var_resizeWidth, + resize_height: var_resizeHeight, + }; + } +} + impl SseDecode for u32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self { @@ -204,6 +215,33 @@ fn pde_ffi_dispatcher_sync_impl( // Section: rust2dart +// Codec=Dco (DartCObject based), see doc to use other codecs +impl flutter_rust_bridge::IntoDart for crate::api::compressor::Parameters { + fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi { + [ + self.jpeg_quality.into_into_dart().into_dart(), + self.png_quality.into_into_dart().into_dart(), + self.webp_quality.into_into_dart().into_dart(), + self.gif_quality.into_into_dart().into_dart(), + self.resize.into_into_dart().into_dart(), + self.resize_width.into_into_dart().into_dart(), + self.resize_height.into_into_dart().into_dart(), + ] + .into_dart() + } +} +impl flutter_rust_bridge::for_generated::IntoDartExceptPrimitive + for crate::api::compressor::Parameters +{ +} +impl flutter_rust_bridge::IntoIntoDart + for crate::api::compressor::Parameters +{ + fn into_into_dart(self) -> crate::api::compressor::Parameters { + self + } +} + impl SseEncode for String { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { @@ -228,6 +266,19 @@ impl SseEncode for Vec { } } +impl SseEncode for crate::api::compressor::Parameters { + // Codec=Sse (Serialization based), see doc to use other codecs + fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) { + ::sse_encode(self.jpeg_quality, serializer); + ::sse_encode(self.png_quality, serializer); + ::sse_encode(self.webp_quality, serializer); + ::sse_encode(self.gif_quality, serializer); + ::sse_encode(self.resize, serializer); + ::sse_encode(self.resize_width, serializer); + ::sse_encode(self.resize_height, serializer); + } +} + impl SseEncode for u32 { // Codec=Sse (Serialization based), see doc to use other codecs fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {