From 47ffc6eb33ce49275eca05ea402ae7d9a37ebfc7 Mon Sep 17 00:00:00 2001 From: Bono Fox Date: Thu, 27 Nov 2025 12:11:39 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9E=95=20#111:=20Added=20"bon"=20for=20a?= =?UTF-8?q?utomated=20builder=20patterns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + 2 files changed, 84 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index ca405b1..a8b19ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,6 +406,31 @@ dependencies = [ "piper", ] +[[package]] +name = "bon" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebeb9aaf9329dff6ceb65c689ca3db33dbf15f324909c60e4e5eef5701ce31b1" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "3.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77e9d642a7e3a318e37c2c9427b5a6a48aa1ad55dcd986f3034ab2239045a645" +dependencies = [ + "darling", + "ident_case", + "prettyplease", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.111", +] + [[package]] name = "bumpalo" version = "3.19.0" @@ -905,6 +930,41 @@ dependencies = [ "zbus", ] +[[package]] +name = "darling" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.111", +] + +[[package]] +name = "darling_macro" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.111", +] + [[package]] name = "dconf_rs" version = "0.3.0" @@ -1197,6 +1257,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "font-types" version = "0.7.3" @@ -1804,6 +1870,12 @@ dependencies = [ "winit", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "image" version = "0.24.9" @@ -2712,6 +2784,16 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8cf8e6a8aa66ce33f63993ffc4ea4271eb5b0530a9002db8455ea6050c77bfa" +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.111", +] + [[package]] name = "proc-macro-crate" version = "3.4.0" @@ -3447,6 +3529,7 @@ dependencies = [ name = "tiny4linux" version = "2.1.2" dependencies = [ + "bon", "clap", "clap_complete", "dialoguer", diff --git a/Cargo.toml b/Cargo.toml index b28d2a3..a8b52b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ tiny4linux_assets = { git = "https://github.com/OpenFoxes/Tiny4Linux-Icons", rev iced = { version = "0.13.0", features = ["image", "tokio"], optional = true } iced_font_awesome = { version = "0.3.0", optional = true } nix = { version = "0.30.0", features = ["ioctl"] } +bon = "3.8.1" errno = "0.3.14" hex = "0.4.3" glob = "0.3.3" From 3dd07c3eee873f53824124d3ce00c82e507b5fa6 Mon Sep 17 00:00:00 2001 From: Bono Fox Date: Thu, 27 Nov 2025 17:22:32 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20#111:=20Refactor=20com?= =?UTF-8?q?mand02=20to=20functional=20builder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - used bon-framework - replaced uses of old struct builder --- src/libs/camera/command02.rs | 90 ++++++------------- .../camera/commands/exposure_mode_type.rs | 5 +- .../camera/commands/goto_preset_position.rs | 4 +- src/libs/camera/commands/sleep.rs | 5 +- src/libs/camera/commands/tracking_speed.rs | 5 +- src/libs/camera/mod.rs | 2 +- 6 files changed, 34 insertions(+), 77 deletions(-) diff --git a/src/libs/camera/command02.rs b/src/libs/camera/command02.rs index 4c90431..e70b048 100644 --- a/src/libs/camera/command02.rs +++ b/src/libs/camera/command02.rs @@ -1,68 +1,28 @@ // SPDX-License-Identifier: EUPL-1.2 -pub struct Command02 { - pub function_group: Option<[u8; 6]>, - pub sequence_nr: Option<[u8; 2]>, - pub checksum: Option<[u8; 2]>, - pub command: Option<[u8; 6]>, - pub appendix: Option<[u8; 16]>, -} - -impl Command02 { - pub fn new() -> Self { - Self { - function_group: None, - sequence_nr: None, - checksum: None, - command: None, - appendix: None, - } - } - - pub fn function_group(mut self, function_group: [u8; 6]) -> Self { - self.function_group = Some(function_group); - self - } - - pub fn sequence_nr(mut self, sequence_number: [u8; 2]) -> Self { - self.sequence_nr = Some(sequence_number); - self - } - - pub fn checksum(mut self, checksum: [u8; 2]) -> Self { - self.checksum = Some(checksum); - self - } - - pub fn command(mut self, cmd: [u8; 6]) -> Self { - self.command = Some(cmd); - self - } - - pub fn appendix(mut self, app: [u8; 16]) -> Self { - self.appendix = Some(app); - self - } - - pub fn build(self) -> [u8; 36] { - const FRAME_ID: [u8; 2] = [0xaa, 0x25]; - const SEGMENT_SIZE: [u8; 2] = [0x0c, 0x00]; - - [ - FRAME_ID.as_slice(), - self.sequence_nr - .expect("sequence_nr is required") - .as_slice(), - SEGMENT_SIZE.as_slice(), - self.checksum.expect("checksum is required").as_slice(), - self.function_group - .expect("function_group is required") - .as_slice(), - self.command.expect("command is required").as_slice(), - self.appendix.unwrap_or([0x00; 16]).as_slice(), - ] - .concat() - .try_into() - .unwrap() - } +use bon::builder; + +#[builder(finish_fn = build)] +pub fn command02( + function_group: [u8; 6], + sequence_nr: [u8; 2], + checksum: [u8; 2], + command: [u8; 6], + appendix: Option<[u8; 16]>, +) -> [u8; 36] { + const FRAME_ID: [u8; 2] = [0xaa, 0x25]; + const SEGMENT_SIZE: [u8; 2] = [0x0c, 0x00]; + + [ + FRAME_ID.as_slice(), + sequence_nr.as_slice(), + SEGMENT_SIZE.as_slice(), + checksum.as_slice(), + function_group.as_slice(), + command.as_slice(), + appendix.unwrap_or([0x00; 16]).as_slice(), + ] + .concat() + .try_into() + .unwrap() } diff --git a/src/libs/camera/commands/exposure_mode_type.rs b/src/libs/camera/commands/exposure_mode_type.rs index 5f2d8ed..7f77140 100644 --- a/src/libs/camera/commands/exposure_mode_type.rs +++ b/src/libs/camera/commands/exposure_mode_type.rs @@ -1,7 +1,6 @@ // SPDX-License-Identifier: EUPL-1.2 -use crate::Command02; -use crate::ExposureMode; +use crate::{ExposureMode, command02}; pub struct ExposureModeTypeCommand; @@ -23,7 +22,7 @@ impl ExposureModeTypeCommand { ) }; - Command02::new() + command02() .function_group(FUNCTION_GROUP_EXPOSURE_MODE_TYPE) .sequence_nr(sequence_nr) .checksum(checksum) diff --git a/src/libs/camera/commands/goto_preset_position.rs b/src/libs/camera/commands/goto_preset_position.rs index 0fc89f7..f643b4b 100644 --- a/src/libs/camera/commands/goto_preset_position.rs +++ b/src/libs/camera/commands/goto_preset_position.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: EUPL-1.2 -use crate::Command02; +use crate::command02; use crate::libs::errors::T4lError; pub struct GotoPresetPositionCommand; @@ -35,7 +35,7 @@ impl GotoPresetPositionCommand { } }; - Ok(Command02::new() + Ok(command02() .function_group(FUNCTION_GROUP_PRESETS) .sequence_nr(sequence_nr) .checksum(checksum) diff --git a/src/libs/camera/commands/sleep.rs b/src/libs/camera/commands/sleep.rs index a9c0fc9..9a58783 100644 --- a/src/libs/camera/commands/sleep.rs +++ b/src/libs/camera/commands/sleep.rs @@ -1,8 +1,7 @@ // SPDX-License-Identifier: EUPL-1.2 -use crate::Command02; -use crate::SleepMode; use crate::libs::errors::T4lError; +use crate::{SleepMode, command02}; pub struct SleepCommand; @@ -28,7 +27,7 @@ impl SleepCommand { SleepMode::Unknown => panic!(), }; - Ok(Command02::new() + Ok(command02() .function_group(FUNCTION_GROUP_SLEEP) .sequence_nr(sequence_nr) .checksum(checksum) diff --git a/src/libs/camera/commands/tracking_speed.rs b/src/libs/camera/commands/tracking_speed.rs index 43b33c2..2450b00 100644 --- a/src/libs/camera/commands/tracking_speed.rs +++ b/src/libs/camera/commands/tracking_speed.rs @@ -1,8 +1,7 @@ // SPDX-License-Identifier: EUPL-1.2 -use crate::Command02; -use crate::TrackingSpeed; use crate::libs::errors::T4lError; +use crate::{TrackingSpeed, command02}; pub struct TrackingSpeedCommand; @@ -29,7 +28,7 @@ impl TrackingSpeedCommand { ), }; - Ok(Command02::new() + Ok(command02() .function_group(FUNCTION_GROUP_TRACKING_SPEED) .sequence_nr(sequence_nr) .checksum(checksum) diff --git a/src/libs/camera/mod.rs b/src/libs/camera/mod.rs index 0df8c2c..0a61e47 100644 --- a/src/libs/camera/mod.rs +++ b/src/libs/camera/mod.rs @@ -9,7 +9,7 @@ mod transport; pub use camera::Camera; pub use camera::Tiny2Camera; -pub use command02::Command02; +pub use command02::command02; pub use commands::*; pub use enums::*; pub use status::CameraStatus;