Skip to content

Commit

Permalink
Merge pull request #960 from quartiq/miniconf-0.17
Browse files Browse the repository at this point in the history
Miniconf-0.17
  • Loading branch information
jordens authored Oct 25, 2024
2 parents 858ec06 + 26c7e1c commit 8aff7d2
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 158 deletions.
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ minimq = "0.9.0"
usb-device = "0.3.2"
usbd-serial = "0.2"
# Keep this synced with the miniconf version in py/setup.py
miniconf = { version = "0.16", features = ["json-core", "derive", "postcard"] }
miniconf_mqtt = { version = "0.16" }
miniconf = { version = "0.17", features = ["json-core", "derive", "postcard"] }
miniconf_mqtt = { version = "0.17" }
tca9539 = "0.2"
smoltcp-nal = { version = "0.5", features = ["shared-stack"] }
postcard = "1"
Expand Down
2 changes: 1 addition & 1 deletion py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"numpy",
"scipy",
"matplotlib",
"miniconf-mqtt@git+https://github.com/quartiq/miniconf@v0.16.0#subdirectory=py/miniconf-mqtt",
"miniconf-mqtt@git+https://github.com/quartiq/miniconf@v0.17.0#subdirectory=py/miniconf-mqtt",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion serial-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repository = "https://github.com/quartiq/stabilizer"
maintenance = { status = "actively-developed" }

[dependencies]
miniconf = { version = "0.16", features = ["json-core", "postcard"] }
miniconf = { version = "0.17", features = ["json-core", "postcard"] }
menu = { version = "0.6", features = ["echo"], git = "https://github.com/rust-embedded-community/menu.git" }
heapless = "0.8"
embedded-io = "0.6"
Expand Down
44 changes: 21 additions & 23 deletions serial-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub use interface::BestEffortInterface;

/// Specifies the API required for objects that are used as settings with the serial terminal
/// interface.
pub trait Settings<const Y: usize>:
TreeSerialize<Y> + TreeDeserializeOwned<Y> + Clone
pub trait Settings:
TreeKey + TreeSerialize + TreeDeserializeOwned + Clone
{
/// Reset the settings to their default values.
fn reset(&mut self) {}
Expand All @@ -27,15 +27,15 @@ pub trait Settings<const Y: usize>:
///
/// Assuming there are no unit fields in the `Settings`, the empty value can be
/// used to mark the "cleared" state.
pub trait Platform<const Y: usize> {
pub trait Platform {
/// This type specifies the interface to the user, for example, a USB CDC-ACM serial port.
type Interface: embedded_io::Read
+ embedded_io::ReadReady
+ embedded_io::Write;

type Error: core::fmt::Debug;

type Settings: Settings<Y>;
type Settings: Settings;

/// Fetch a value from persisten storage
fn fetch<'a>(
Expand Down Expand Up @@ -68,7 +68,7 @@ struct Interface<'a, P, const Y: usize> {
updated: bool,
}

impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
impl<'a, P: Platform, const Y: usize> Interface<'a, P, Y> {
fn handle_platform(
_menu: &menu::Menu<Self, P::Settings>,
item: &menu::Item<Self, P::Settings>,
Expand All @@ -93,9 +93,9 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
&mut P::Settings,
),
{
let mut iter = P::Settings::nodes::<Path<String<128>, '/'>>();
let mut iter = P::Settings::nodes::<Path<String<128>, '/'>, Y>();
if let Some(key) = key {
match iter.root(&Path::<_, '/'>::from(key)) {
match iter.root(Path::<_, '/'>::from(key)) {
Ok(it) => iter = it,
Err(e) => {
writeln!(interface, "Failed to locate `{key}`: {e}")
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
|key, interface, settings, defaults| {
// Get current
let check =
match json::get_by_key(settings, &key, interface.buffer) {
match json::get_by_key(settings, key, interface.buffer) {
Err(miniconf::Error::Traversal(Traversal::Absent(
_,
))) => {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
};

// Get default and compare
match json::get_by_key(defaults, &key, interface.buffer) {
match json::get_by_key(defaults, key, interface.buffer) {
Err(miniconf::Error::Traversal(Traversal::Absent(_))) => {
write!(interface, " [default: absent]")
}
Expand Down Expand Up @@ -209,13 +209,13 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
Ok(Some(stored)) => {
let slic = ::postcard::de_flavors::Slice::new(stored);
// Use defaults as scratch space for postcard->json conversion
match postcard::set_by_key(defaults, &key, slic) {
match postcard::set_by_key(defaults, key, slic) {
Err(e) => write!(
interface,
" [stored deserialize error: {e}]"
),
Ok(_rest) =>
match json::get_by_key(defaults, &key, interface.buffer) {
match json::get_by_key(defaults, key, interface.buffer) {
Err(e) => write!(
interface,
" [stored serialization error: {e}]"
Expand Down Expand Up @@ -258,7 +258,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
// Get current value checksum
let slic =
::postcard::ser_flavors::Slice::new(interface.buffer);
let check = match postcard::get_by_key(settings, &key, slic) {
let check = match postcard::get_by_key(settings, key, slic) {
Err(miniconf::Error::Traversal(Traversal::Absent(_))) => {
return;
}
Expand All @@ -273,7 +273,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
// Get default if different
let slic =
::postcard::ser_flavors::Slice::new(interface.buffer);
let slic = match postcard::get_by_key(defaults, &key, slic) {
let slic = match postcard::get_by_key(defaults, key, slic) {
Err(miniconf::Error::Traversal(Traversal::Absent(_))) => {
log::warn!(
"Can't clear. Default is absent: `{}`",
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
// Set default
if let Some(slic) = slic {
let slic = ::postcard::de_flavors::Slice::new(slic);
match postcard::set_by_key(settings, &key, slic) {
match postcard::set_by_key(settings, key, slic) {
Err(miniconf::Error::Traversal(Traversal::Absent(
_,
))) => {
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
// Get default value checksum
let slic =
::postcard::ser_flavors::Slice::new(interface.buffer);
let mut check = match postcard::get_by_key(defaults, &key, slic)
let mut check = match postcard::get_by_key(defaults, key, slic)
{
// Could also serialize directly into the hasher for all these checksum calcs
Ok(slic) => yafnv::fnv1a::<u32>(slic),
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
// Get value
let slic =
::postcard::ser_flavors::Slice::new(interface.buffer);
let value = match postcard::get_by_key(settings, &key, slic) {
let value = match postcard::get_by_key(settings, key, slic) {
Ok(value) => value,
Err(miniconf::Error::Traversal(Traversal::Absent(
_depth,
Expand Down Expand Up @@ -578,9 +578,7 @@ impl<'a, P: Platform<Y>, const Y: usize> Interface<'a, P, Y> {
}
}

impl<'a, P: Platform<Y>, const Y: usize> core::fmt::Write
for Interface<'a, P, Y>
{
impl<'a, P: Platform, const Y: usize> core::fmt::Write for Interface<'a, P, Y> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.platform
.interface_mut()
Expand All @@ -589,11 +587,11 @@ impl<'a, P: Platform<Y>, const Y: usize> core::fmt::Write
}
}

impl<'a, P: Platform<Y>, const Y: usize> ErrorType for Interface<'a, P, Y> {
impl<'a, P: Platform, const Y: usize> ErrorType for Interface<'a, P, Y> {
type Error = <P::Interface as ErrorType>::Error;
}

impl<'a, P: Platform<Y>, const Y: usize> Write for Interface<'a, P, Y> {
impl<'a, P: Platform, const Y: usize> Write for Interface<'a, P, Y> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.platform.interface_mut().write(buf)
}
Expand All @@ -604,11 +602,11 @@ impl<'a, P: Platform<Y>, const Y: usize> Write for Interface<'a, P, Y> {
}

// The Menu runner
pub struct Runner<'a, P: Platform<Y>, const Y: usize>(
pub struct Runner<'a, P: Platform, const Y: usize>(
menu::Runner<'a, Interface<'a, P, Y>, P::Settings, [u8]>,
);

impl<'a, P: Platform<Y>, const Y: usize> Runner<'a, P, Y> {
impl<'a, P: Platform, const Y: usize> Runner<'a, P, Y> {
/// Constructor
///
/// # Args
Expand Down
Loading

0 comments on commit 8aff7d2

Please sign in to comment.