Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasKoch committed Aug 25, 2021
1 parent 6c788d1 commit 2444b8d
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 66 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --all --all-features --target thumbv7m-none-eabi
args: --all --target thumbv7m-none-eabi

- name: Test
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -130,7 +130,7 @@ jobs:
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features -- ${{ env.CLIPPY_PARAMS }}
args: -- ${{ env.CLIPPY_PARAMS }}
grcov:
name: Coverage
runs-on: ubuntu-latest
Expand Down Expand Up @@ -220,7 +220,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: doc
args: --verbose --no-deps --all-features
args: --verbose --no-deps

# - name: Finalize documentation
# run: |
Expand Down
7 changes: 5 additions & 2 deletions mqttrust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ name = "mqttrust"
maintenance = { status = "actively-developed" }

[dependencies]
heapless = { version = "^0.7", features = ["serde"] }
heapless = { version = "^0.7" }
defmt = { version = "^0.2", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }

[features]
default = []

defmt-impl = ["defmt", "heapless/defmt-impl"]
defmt-impl = ["defmt", "heapless/defmt-impl"]

derive = ["serde", "heapless/serde"]
26 changes: 11 additions & 15 deletions mqttrust/src/encoding/v4/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use super::{decoder::*, encoder::*, *};
/// [`Connect`]: struct.Connect.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Protocol {
/// [MQTT 3.1.1] is the most commonly implemented version. [MQTT 5] isn't yet supported my by
/// `mqttrs`.
/// [MQTT 3.1.1] is the most commonly implemented version.
///
/// [MQTT 3.1.1]: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
/// [MQTT 5]: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
MQTT311,
/// MQIsdp, aka SCADA are pre-standardisation names of MQTT. It should mostly conform to MQTT
/// 3.1.1, but you should watch out for implementation discrepancies. `Mqttrs` handles it like
/// standard MQTT 3.1.1.
/// 3.1.1, but you should watch out for implementation discrepancies.
MQIsdp,
}
impl Protocol {
Expand All @@ -26,7 +24,7 @@ impl Protocol {
_ => Err(Error::InvalidProtocol(name.into(), level)),
}
}
pub(crate) fn from_buffer<'a>(buf: &'a [u8], offset: &mut usize) -> Result<Self, Error> {
pub(crate) fn from_buffer(buf: &[u8], offset: &mut usize) -> Result<Self, Error> {
let protocol_name = read_str(buf, offset)?;
let protocol_level = buf[*offset];
*offset += 1;
Expand All @@ -36,16 +34,14 @@ impl Protocol {
pub(crate) fn to_buffer(&self, buf: &mut [u8], offset: &mut usize) -> Result<usize, Error> {
match self {
Protocol::MQTT311 => {
let slice = &[0u8, 4, 'M' as u8, 'Q' as u8, 'T' as u8, 'T' as u8, 4];
let slice = &[0u8, 4, b'M', b'Q', b'T', b'T', 4];
for &byte in slice {
write_u8(buf, offset, byte)?;
}
Ok(slice.len())
}
Protocol::MQIsdp => {
let slice = &[
0u8, 4, 'M' as u8, 'Q' as u8, 'i' as u8, 's' as u8, 'd' as u8, 'p' as u8, 4,
];
let slice = &[0u8, 4, b'M', b'Q', b'i', b's', b'd', b'p', 4];
for &byte in slice {
write_u8(buf, offset, byte)?;
}
Expand Down Expand Up @@ -86,7 +82,7 @@ pub enum ConnectReturnCode {
NotAuthorized,
}
impl ConnectReturnCode {
fn to_u8(&self) -> u8 {
fn as_u8(&self) -> u8 {
match *self {
ConnectReturnCode::Accepted => 0,
ConnectReturnCode::RefusedProtocolVersion => 1,
Expand Down Expand Up @@ -174,10 +170,10 @@ impl<'a> Connect<'a> {
protocol,
keep_alive,
client_id,
clean_session,
last_will,
username,
password,
last_will,
clean_session,
})
}

Expand Down Expand Up @@ -215,7 +211,7 @@ impl<'a> Connect<'a> {
};
if let Some(last_will) = &self.last_will {
connect_flags |= 0b00000100;
connect_flags |= last_will.qos.to_u8() << 3;
connect_flags |= last_will.qos.as_u8() << 3;
if last_will.retain {
connect_flags |= 0b00100000;
};
Expand Down Expand Up @@ -251,7 +247,7 @@ impl<'a> Connect<'a> {
}

impl Connack {
pub(crate) fn from_buffer<'a>(buf: &'a [u8], offset: &mut usize) -> Result<Self, Error> {
pub(crate) fn from_buffer(buf: &[u8], offset: &mut usize) -> Result<Self, Error> {
let flags = buf[*offset];
let return_code = buf[*offset + 1];
*offset += 2;
Expand All @@ -268,7 +264,7 @@ impl Connack {
if self.session_present {
flags |= 0b1;
};
let rc = self.code.to_u8();
let rc = self.code.as_u8();
write_u8(buf, offset, header)?;
write_u8(buf, offset, length)?;
write_u8(buf, offset, flags)?;
Expand Down
7 changes: 2 additions & 5 deletions mqttrust/src/encoding/v4/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

pub fn decode_slice<'a>(buf: &'a [u8]) -> Result<Option<Packet<'a>>, Error> {
pub fn decode_slice(buf: &[u8]) -> Result<Option<Packet<'_>>, Error> {
let mut offset = 0;
if let Some((header, remaining_len)) = read_header(buf, &mut offset)? {
let r = read_packet(header, remaining_len, buf, &mut offset)?;
Expand Down Expand Up @@ -37,10 +37,7 @@ fn read_packet<'a>(

/// Read the parsed header and remaining_len from the buffer. Only return Some() and advance the
/// buffer position if there is enough data in the buffer to read the full packet.
pub fn read_header<'a>(
buf: &'a [u8],
offset: &mut usize,
) -> Result<Option<(Header, usize)>, Error> {
pub fn read_header(buf: &[u8], offset: &mut usize) -> Result<Option<(Header, usize)>, Error> {
let mut len: usize = 0;
for pos in 0..=3 {
if buf.len() > *offset + pos + 1 {
Expand Down
25 changes: 9 additions & 16 deletions mqttrust/src/encoding/v4/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use super::{Error, Packet};

/// Encode a [Packet] enum into a [BufMut] buffer.
/// Encode a [Packet] enum into a u8 slice.
///
/// ```
/// # use mqttrs::*;
/// # use bytes::*;
/// # use mqttrust::encoding::v4::*;
/// // Instantiate a `Packet` to encode.
/// let packet = Publish {
/// dup: false,
/// qospid: QosPid::AtMostOnce,
/// qos: QoS::AtMostOnce,
/// retain: false,
/// topic_name: "test",
/// payload: b"hello",
/// pid: None,
/// }.into();
///
/// // Allocate buffer (should be appropriately-sized or able to grow as needed).
Expand All @@ -20,18 +20,11 @@ use super::{Error, Packet};
/// // Write bytes corresponding to `&Packet` into the `BytesMut`.
/// let len = encode_slice(&packet, &mut buf).expect("failed encoding");
/// assert_eq!(&buf[..len], &[0b00110000, 11,
/// 0, 4, 't' as u8, 'e' as u8, 's' as u8, 't' as u8,
/// 'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8]);
/// 0, 4, b't', b'e', b's', b't',
/// b'h', b'e', b'l', b'l', b'o']);
/// ```
///
/// [Packet]: ../enum.Packet.html
/// [BufMut]: https://docs.rs/bytes/1.0.0/bytes/trait.BufMut.html
// #[cfg(feature = "std")]
// pub fn encode_slice(packet: &Packet, buf: impl BufMut) -> Result<usize, Error> {
// let mut offset = 0;
// encode_slice(packet, buf.bytes_mut(), &mut offset)
// }

pub fn encode_slice(packet: &Packet, buf: &mut [u8]) -> Result<usize, Error> {
let mut offset = 0;

Expand Down Expand Up @@ -149,12 +142,12 @@ pub(crate) fn write_length(buf: &mut [u8], offset: &mut usize, len: usize) -> Re
let mut x = len;
while !done {
let mut byte = (x % 128) as u8;
x = x / 128;
x /= 128;
if x > 0 {
byte = byte | 128;
byte |= 128;
}
write_u8(buf, offset, byte)?;
done = x <= 0;
done = x == 0;
}
Ok(write_len)
}
Expand Down
5 changes: 3 additions & 2 deletions mqttrust/src/encoding/v4/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ const PID_LEN: usize = 2;
/// [`encode()`]. Most variants can be constructed directly without using methods.
///
/// ```
/// # use mqttrs::*;
/// # use mqttrust::encoding::v4::*;
/// # use core::convert::TryFrom;
/// // Simplest form
/// let pkt = Packet::Connack(Connack { session_present: false,
/// code: ConnectReturnCode::Accepted });
/// // Using `Into` trait
/// let publish = Publish { dup: false,
/// qospid: QosPid::AtMostOnce,
/// qos: QoS::AtMostOnce,
/// retain: false,
/// pid: None,
/// topic_name: "to/pic",
/// payload: b"payload" };
/// let pkt: Packet = publish.into();
Expand Down
4 changes: 2 additions & 2 deletions mqttrust/src/encoding/v4/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ impl<'a> Publish<'a> {
QoS::ExactlyOnce => 0b00110100,
};
if self.dup {
header |= 0b00001000 as u8;
header |= 0b00001000_u8;
};
if self.retain {
header |= 0b00000001 as u8;
header |= 0b00000001_u8;
};
check_remaining(buf, offset, 1)?;
write_u8(buf, offset, header)?;
Expand Down
17 changes: 7 additions & 10 deletions mqttrust/src/encoding/v4/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ impl<'a> FromBuffer<'a> for SubscribeReturnCodes {
}

impl SubscribeReturnCodes {
pub(crate) fn to_u8(&self) -> u8 {
pub(crate) fn as_u8(&self) -> u8 {
match *self {
SubscribeReturnCodes::Failure => 0x80,
SubscribeReturnCodes::Success(qos) => qos.to_u8(),
SubscribeReturnCodes::Success(qos) => qos.as_u8(),
}
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ where
match self.list {
List::Owned(data) => {
// FIXME: Can we get rid of this clone?
let item = data.get(self.index).map(|t| t.clone());
let item = data.get(self.index).cloned();
self.index += 1;
item
}
Expand Down Expand Up @@ -214,12 +214,12 @@ impl<'a> Subscribe<'a> {
let write_len = write_length(buf, offset, self.len())? + 1;

// Pid
self.pid.unwrap_or(Pid::default()).to_buffer(buf, offset)?;
self.pid.unwrap_or_default().to_buffer(buf, offset)?;

// Topics
for topic in self.topics() {
write_string(buf, offset, topic.topic_path)?;
write_u8(buf, offset, topic.qos.to_u8())?;
write_u8(buf, offset, topic.qos.as_u8())?;
}

Ok(write_len)
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<'a> Unsubscribe<'a> {
let write_len = write_length(buf, offset, self.len())? + 1;

// Pid
self.pid.unwrap_or(Pid::default()).to_buffer(buf, offset)?;
self.pid.unwrap_or_default().to_buffer(buf, offset)?;

for topic in self.topics() {
write_string(buf, offset, topic)?;
Expand All @@ -296,9 +296,6 @@ impl<'a> Suback<'a> {
// let mut return_codes = LimitedVec::new();
// while *offset < payload_end {
// let _res = return_codes.push(SubscribeReturnCodes::from_buffer(buf, offset)?);

// #[cfg(not(feature = "std"))]
// _res.map_err(|_| Error::InvalidLength)?;
// }

Ok(Suback {
Expand All @@ -316,7 +313,7 @@ impl<'a> Suback<'a> {
let write_len = write_length(buf, offset, length)? + 1;
self.pid.to_buffer(buf, offset)?;
for rc in self.return_codes {
write_u8(buf, offset, rc.to_u8())?;
write_u8(buf, offset, rc.as_u8())?;
}
Ok(write_len)
}
Expand Down
9 changes: 6 additions & 3 deletions mqttrust/src/encoding/v4/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl From<IoError> for Error {
/// For packets with [`QoS::AtLeastOne` or `QoS::ExactlyOnce`] delivery.
///
/// ```rust
/// # use mqttrs::{Packet, Pid, QosPid};
/// # use mqttrust::encoding::v4::{Packet, Pid, QosPid};
/// # use std::convert::TryFrom;
/// #[derive(Default)]
/// struct Session {
Expand All @@ -99,6 +99,7 @@ impl From<IoError> for Error {
/// [MQTT-2.3.1-1]: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718025
/// [MQTT-2.2.1-3]: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901026
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
pub struct Pid(NonZeroU16);
impl Pid {
Expand All @@ -112,7 +113,7 @@ impl Pid {
self.0.get()
}

pub(crate) fn from_buffer<'a>(buf: &'a [u8], offset: &mut usize) -> Result<Self, Error> {
pub(crate) fn from_buffer(buf: &[u8], offset: &mut usize) -> Result<Self, Error> {
let pid = ((buf[*offset] as u16) << 8) | buf[*offset + 1] as u16;
*offset += 2;
Self::try_from(pid)
Expand Down Expand Up @@ -180,6 +181,7 @@ impl TryFrom<u16> for Pid {
/// [Quality of Service]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718099
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
pub enum QoS {
/// `QoS 0`. No ack needed.
AtMostOnce,
Expand All @@ -190,7 +192,7 @@ pub enum QoS {
}

impl QoS {
pub(crate) fn to_u8(&self) -> u8 {
pub(crate) fn as_u8(&self) -> u8 {
match *self {
QoS::AtMostOnce => 0,
QoS::AtLeastOnce => 1,
Expand All @@ -217,6 +219,7 @@ impl QoS {
/// [`Pid`]: struct.Pid.html
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "derive", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
pub enum QosPid {
AtMostOnce,
AtLeastOnce(Pid),
Expand Down
Loading

0 comments on commit 2444b8d

Please sign in to comment.