generated from BroderickCarlin/template-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Add Command trait to represent invocable commands
- Loading branch information
1 parent
1bb9099
commit 4c0ffc7
Showing
9 changed files
with
282 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::{id, FromByteArray, ToByteArray}; | ||
|
||
/// The core trait to be implemented for all types that represent an invokable command | ||
/// | ||
/// All [`Command`]s specify the parameters for both the command and the response body. In the | ||
/// case where either the command or response has no parameters, the [`NoParameters`](crate::NoParameters) | ||
/// can be specified. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use regiface::{Command, ToByteArray, FromByteArray, NoParameters}; | ||
/// | ||
/// struct GetTemperature; | ||
/// | ||
/// impl Command for GetTemperature { | ||
/// type IdType = u8; | ||
/// type CommandParameters = NoParameters; | ||
/// type ResponseParameters = Temperature; | ||
/// | ||
/// fn id() -> Self::IdType { | ||
/// 0x42 | ||
/// } | ||
/// | ||
/// fn parameters(&self) -> Self::CommandParameters { | ||
/// NoParameters::default() | ||
/// } | ||
/// } | ||
/// | ||
/// struct Temperature { | ||
/// celsius: f32 | ||
/// } | ||
/// | ||
/// impl FromByteArray for Temperature { | ||
/// type Error = core::convert::Infallible; | ||
/// type Array = [u8; 4]; | ||
/// | ||
/// fn from_bytes(bytes: Self::Array) -> Result<Self, Self::Error> { | ||
/// let celsius = f32::from_be_bytes(bytes); | ||
/// Ok(Self { celsius }) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub trait Command { | ||
/// The type used to represent the command's ID. | ||
/// | ||
/// Command ID types are any type that implement the [`Id`](id::Id) trait. This | ||
/// trait provides default implementations for [`u8`], [`u16`], [`u32`], [`u64`], and [`u128`]. | ||
type IdType: id::Id; | ||
|
||
/// The parameters included as part of the command invocation | ||
/// | ||
/// If the command has no parameters, the [`NoParameters`](crate::NoParameters) type can be used | ||
type CommandParameters: ToByteArray; | ||
|
||
/// The parameters expected as the response to the command | ||
/// | ||
/// If the response has no parameters, the [`NoParameters`](crate::NoParameters) type can be used | ||
type ResponseParameters: FromByteArray; | ||
|
||
/// A method that returns the ID of the [`Command`] | ||
fn id() -> Self::IdType; | ||
|
||
/// A method to retrieve the parameters from an instance of the [`Command`] | ||
fn parameters(&self) -> Self::CommandParameters; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use std::convert::Infallible; | ||
|
||
use crate::ToByteArray; | ||
|
||
pub trait Id: ToByteArray<Error = Infallible> {} | ||
|
||
impl Id for u8 {} | ||
impl Id for u16 {} | ||
impl Id for u32 {} | ||
impl Id for u64 {} | ||
impl Id for u128 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.