Skip to content

Commit

Permalink
Add storage feature, fix serde in mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani authored and ivmarkov committed Nov 14, 2023
1 parent b6fa324 commit be3ac85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
run: cargo build --no-default-features --features experimental,nightly,alloc,use_serde,use_strum,use_numenum,log --target riscv32imc-esp-espidf -Zbuild-std=core,alloc,panic_abort -Zbuild-std-features=panic_immediate_abort
- name: Build | Compile / no_std
run: cargo build --no-default-features --features experimental,nightly,use_serde,use_strum,use_numenum,log --target riscv32imc-esp-espidf -Zbuild-std=core,alloc,panic_abort -Zbuild-std-features=panic_immediate_abort
- name: Build | Compile / no_std, no serde
run: cargo build --no-default-features --features experimental,nightly,use_strum,use_numenum,log --target riscv32imc-esp-espidf -Zbuild-std=core,alloc,panic_abort -Zbuild-std-features=panic_immediate_abort
- name: Build | Compile / defmt
run: cargo build --no-default-features --features std,experimental,nightly,use_serde,use_strum,use_numenum,defmt --target riscv32imc-esp-espidf -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort
- name: Build | Compile / defmt, no_std
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod log;
pub mod mqtt;
pub mod ota;
pub mod ping;
#[cfg(feature = "storage")]
pub mod storage;
pub mod sys_time;
pub mod timer;
Expand Down
16 changes: 11 additions & 5 deletions src/mqtt/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::fmt::{self, Debug, Display, Formatter};
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "use_serde")]
use serde::{Deserialize, Serialize};

pub trait ErrorType {
Expand All @@ -25,8 +26,9 @@ where

/// Quality of service
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub enum QoS {
AtMostOnce = 0,
AtLeastOnce = 1,
Expand All @@ -35,8 +37,9 @@ pub enum QoS {

pub type MessageId = u32;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub enum Event<M> {
BeforeConnect,
Connected(bool),
Expand Down Expand Up @@ -159,22 +162,25 @@ impl Message for MessageImpl {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub enum Details {
Complete,
InitialChunk(InitialChunkData),
SubsequentChunk(SubsequentChunkData),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub struct InitialChunkData {
pub total_data_size: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
pub struct SubsequentChunkData {
pub current_data_offset: usize,
pub total_data_size: usize,
Expand Down
12 changes: 10 additions & 2 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::any::Any;
use core::fmt::{self, Debug};

use serde::de::DeserializeOwned;
use serde::Serialize;
#[cfg(feature = "use_serde")]
use serde::{de::DeserializeOwned, Serialize};

pub trait StorageBase {
type Error: Debug;
Expand All @@ -26,6 +26,7 @@ where
}
}

#[cfg(feature = "use_serde")]
pub trait Storage: StorageBase {
fn get<T>(&self, name: &str) -> Result<Option<T>, Self::Error>
where
Expand All @@ -36,6 +37,7 @@ pub trait Storage: StorageBase {
T: serde::Serialize;
}

#[cfg(feature = "use_serde")]
impl<S> Storage for &mut S
where
S: Storage,
Expand Down Expand Up @@ -99,6 +101,7 @@ where
}
}

#[cfg(feature = "use_serde")]
pub trait SerDe {
type Error: Debug;

Expand All @@ -111,6 +114,7 @@ pub trait SerDe {
T: DeserializeOwned;
}

#[cfg(feature = "use_serde")]
impl<S> SerDe for &S
where
S: SerDe,
Expand Down Expand Up @@ -160,11 +164,13 @@ where
{
}

#[cfg(feature = "use_serde")]
pub struct StorageImpl<const N: usize, R, S> {
raw_storage: R,
serde: S,
}

#[cfg(feature = "use_serde")]
impl<const N: usize, R, S> StorageImpl<N, R, S>
where
R: RawStorage,
Expand Down Expand Up @@ -236,6 +242,7 @@ where
}
}

#[cfg(feature = "use_serde")]
impl<const N: usize, R, S> StorageBase for StorageImpl<N, R, S>
where
R: RawStorage,
Expand All @@ -252,6 +259,7 @@ where
}
}

#[cfg(feature = "use_serde")]
impl<const N: usize, R, S> Storage for StorageImpl<N, R, S>
where
R: RawStorage,
Expand Down

0 comments on commit be3ac85

Please sign in to comment.