Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support heapless primitives #96

Merged
merged 7 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
* python module: don't emite whitespace in JSON to match serde-json-core (#92)
* `heapless::String` now implements `Miniconf` directly.

### Fixed
* Python device discovery now only discovers unique device identifiers. See [#97](https://github.com/quartiq/miniconf/issues/97)


## [0.5.0] - 2022-05-12

### Changed
Expand Down
106 changes: 57 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,67 +330,75 @@ pub trait Miniconf {
macro_rules! impl_single {
($x:ty) => {
impl Miniconf for $x {
fn string_set(
&mut self,
mut topic_parts: core::iter::Peekable<core::str::Split<char>>,
value: &[u8],
) -> Result<(), Error> {
if topic_parts.peek().is_some() {
return Err(Error::PathTooLong);
}
*self = serde_json_core::from_slice(value)?.0;
Ok(())
impl_single!();
}
};

() => {
fn string_set(
&mut self,
mut topic_parts: core::iter::Peekable<core::str::Split<char>>,
value: &[u8],
) -> Result<(), Error> {
if topic_parts.peek().is_some() {
return Err(Error::PathTooLong);
}
*self = serde_json_core::from_slice(value)?.0;
Ok(())
}

fn string_get(
&self,
mut topic_parts: core::iter::Peekable<core::str::Split<char>>,
value: &mut [u8],
) -> Result<usize, Error> {
if topic_parts.peek().is_some() {
return Err(Error::PathTooLong);
}
fn string_get(
&self,
mut topic_parts: core::iter::Peekable<core::str::Split<char>>,
value: &mut [u8],
) -> Result<usize, Error> {
if topic_parts.peek().is_some() {
return Err(Error::PathTooLong);
}

serde_json_core::to_slice(self, value).map_err(|_| Error::SerializationFailed)
serde_json_core::to_slice(self, value).map_err(|_| Error::SerializationFailed)
}

fn get_metadata(&self) -> MiniconfMetadata {
MiniconfMetadata {
// No topic length is needed, as there are no sub-members.
max_topic_size: 0,
// One index is required for the current element.
max_depth: 1,
}
}

fn get_metadata(&self) -> MiniconfMetadata {
MiniconfMetadata {
// No topic length is needed, as there are no sub-members.
max_topic_size: 0,
// One index is required for the current element.
max_depth: 1,
}
// This implementation is the base case for primitives where it will
// yield once for self, then return None on subsequent calls.
fn recurse_paths<const TS: usize>(
&self,
index: &mut [usize],
_topic: &mut heapless::String<TS>,
) -> Option<()> {
if index.len() == 0 {
// Note: During expected execution paths using `iter()`, the size of the
// index stack is checked in advance to make sure this condition doesn't occur.
// However, it's possible to happen if the user manually calls `recurse_paths`.
unreachable!("Index stack too small");
}

// This implementation is the base case for primitives where it will
// yield once for self, then return None on subsequent calls.
fn recurse_paths<const TS: usize>(
&self,
index: &mut [usize],
_topic: &mut heapless::String<TS>,
) -> Option<()> {
if index.len() == 0 {
// Note: During expected execution paths using `iter()`, the size of the
// index stack is checked in advance to make sure this condition doesn't occur.
// However, it's possible to happen if the user manually calls `recurse_paths`.
unreachable!("Index stack too small");
}

let i = index[0];
index[0] += 1;
index[1..].iter_mut().for_each(|x| *x = 0);

if i == 0 {
Some(())
} else {
None
}
let i = index[0];
index[0] += 1;
index[1..].iter_mut().for_each(|x| *x = 0);

if i == 0 {
Some(())
} else {
None
}
}
};
}

impl<const N: usize> Miniconf for heapless::String<N> {
impl_single!();
}

// Implement trait for the primitive types
impl_single!(u8);
impl_single!(u16);
Expand Down
19 changes: 19 additions & 0 deletions tests/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ fn recursive_struct() {
assert_eq!(metadata.max_depth, 3);
assert_eq!(metadata.max_topic_size, "c/a".len());
}

#[test]
fn struct_with_string() {
#[derive(Miniconf, Default)]
struct Settings {
string: heapless::String<10>,
}

let mut s = Settings::default();

let field = "string".split('/').peekable();
let mut buf = [0u8; 256];
let len = s.string_get(field, &mut buf).unwrap();
assert_eq!(&buf[..len], b"\"\"");

let field = "string".split('/').peekable();
s.string_set(field, br#""test""#).unwrap();
assert_eq!(s.string, "test");
}