-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from matthiasbeyer/specialize-property-types
Specialize property types
- Loading branch information
Showing
14 changed files
with
154 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
|
||
use winnow::error::ParserError; | ||
use winnow::Bytes; | ||
|
||
use super::write::WResult; | ||
use super::write::WriteMqttPacket; | ||
use super::MResult; | ||
|
||
pub fn parse_bool(input: &mut &Bytes) -> MResult<bool> { | ||
winnow::binary::u8(input).and_then(|byte| match byte { | ||
0 => Ok(false), | ||
1 => Ok(true), | ||
_ => Err({ | ||
winnow::error::ErrMode::from_error_kind(input, winnow::error::ErrorKind::Verify) | ||
}), | ||
}) | ||
} | ||
|
||
#[inline] | ||
pub fn write_bool<W: WriteMqttPacket>(buffer: &mut W, b: bool) -> WResult<W> { | ||
buffer.write_byte(b as u8) | ||
} |
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
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,58 @@ | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
|
||
use winnow::error::FromExternalError; | ||
use winnow::Bytes; | ||
|
||
use super::write::WResult; | ||
use super::write::WriteMqttPacket; | ||
use super::MResult; | ||
|
||
#[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)] | ||
#[repr(u8)] | ||
#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
pub enum QualityOfService { | ||
AtMostOnce = 0, | ||
AtLeastOnce = 1, | ||
ExactlyOnce = 2, | ||
} | ||
|
||
pub fn parse_qos(input: &mut &Bytes) -> MResult<QualityOfService> { | ||
winnow::binary::u8(input).and_then(|byte| { | ||
QualityOfService::try_from(byte).map_err(|e| { | ||
winnow::error::ErrMode::from_external_error(input, winnow::error::ErrorKind::Verify, e) | ||
}) | ||
}) | ||
} | ||
|
||
#[inline] | ||
pub fn write_qos<W: WriteMqttPacket>(buffer: &mut W, qos: QualityOfService) -> WResult<W> { | ||
crate::v5::variable_header::write_u8(buffer, qos.into()) | ||
} | ||
|
||
#[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)] | ||
#[repr(u8)] | ||
#[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
pub enum MaximumQualityOfService { | ||
AtMostOnce = 0, | ||
AtLeastOnce = 1, | ||
} | ||
|
||
pub fn parse_maximum_quality_of_service(input: &mut &Bytes) -> MResult<MaximumQualityOfService> { | ||
winnow::binary::u8(input).and_then(|byte| { | ||
MaximumQualityOfService::try_from(byte).map_err(|e| { | ||
winnow::error::ErrMode::from_external_error(input, winnow::error::ErrorKind::Verify, e) | ||
}) | ||
}) | ||
} | ||
|
||
#[inline] | ||
pub fn write_maximum_quality_of_service<W: WriteMqttPacket>( | ||
buffer: &mut W, | ||
qos: MaximumQualityOfService, | ||
) -> WResult<W> { | ||
crate::v5::variable_header::write_u8(buffer, qos.into()) | ||
} |
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
Oops, something went wrong.