Skip to content
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
32 changes: 16 additions & 16 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
@@ -1,8 +1,8 @@
[workspace]
resolver = "2"
resolver = "3"
members = [
"dispatch-bundle",
"cookie-cutter",
"serac",
"embedded-command",
"macros",
"packit",
Expand Down
4 changes: 2 additions & 2 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euxo pipefail
rustup toolchain install nightly --component miri

TARGETS=("thumbv6m-none-eabi" "thumbv7em-none-eabi" "thumbv7em-none-eabihf")
CRATES=("macros" "cookie-cutter" "dispatch-bundle")
CRATES=("macros" "serac" "dispatch-bundle")

# build

Expand Down Expand Up @@ -34,4 +34,4 @@ cargo clippy
# cookie-cutter

# asm analysis
cargo build -p cookie-cutter --bin asm --target thumbv7em-none-eabihf --features binary --release
cargo build -p serac --bin asm --target thumbv7em-none-eabihf --features binary --release
12 changes: 0 additions & 12 deletions cookie-cutter/src/encoding.rs

This file was deleted.

36 changes: 0 additions & 36 deletions cookie-cutter/src/medium.rs

This file was deleted.

4 changes: 2 additions & 2 deletions dispatch-bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
packit = []

[dependencies]
macros = { path = "../macros" }
macros = { package = "embedded-command-macros", path = "../macros" }

[dev-dependencies]
cookie-cutter = { path = "../cookie-cutter" }
serac = { path = "../serac" }
2 changes: 1 addition & 1 deletion embedded-command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ embassy-sync = "0.5.0"
embedded-hal-async = "1.0.0"
embedded-io-async = "0.6.1"
heapless = "0.8.0"
cookie-cutter = { path = "../cookie-cutter" }
serac = { path = "../serac" }
defmt = { version = "0.3.10", optional = true }

[features]
Expand Down
7 changes: 5 additions & 2 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "macros"
name = "embedded-command-macros"
version = "0.1.0"
edition = "2021"
edition = "2024"
description = "Macros for the embedded command crate family."
license = "CC-BY-NC-SA-4.0"
repository = "https://github.com/adinack/embedded-command"

[lib]
proc-macro = true
Expand Down
14 changes: 7 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro::TokenStream;

mod cookie_cutter;
mod dispatch_bundle;
mod serac;

/// Transform attached enum into a "bundle".
///
Expand All @@ -20,10 +20,10 @@ pub fn bundle(attr: TokenStream, item: TokenStream) -> TokenStream {
///
/// # Note
///
/// Requires `cookie_cutter` to be in scope with that name.
/// Requires `serac` to be in scope with that name.
#[proc_macro_derive(SerializeIter)]
pub fn serialize_iter_vanilla(item: TokenStream) -> TokenStream {
cookie_cutter::vanilla::serialize_iter(item)
pub fn impl_serialize_iter_vanilla(item: TokenStream) -> TokenStream {
serac::vanilla::serialize_iter(item)
}

/// Generates the implementation block for conforming to `SerializeBuf` of the "vanilla" flavor.
Expand All @@ -32,8 +32,8 @@ pub fn serialize_iter_vanilla(item: TokenStream) -> TokenStream {
///
/// # Note
///
/// Requires `cookie_cutter` to be in scope with that name.
/// Requires `serac` to be in scope with that name.
#[proc_macro_derive(SerializeBuf)]
pub fn serialize_buf_vanilla(item: TokenStream) -> TokenStream {
cookie_cutter::vanilla::serialize_buf(item)
pub fn impl_serialize_buf_vanilla(item: TokenStream) -> TokenStream {
serac::vanilla::impl_serialize_buf(item)
}
File renamed without changes.
51 changes: 36 additions & 15 deletions macros/src/cookie_cutter/vanilla.rs → macros/src/serac/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn size_of_struct(s: DataStruct, info: &BodyInfo) -> TokenStream2 {
if types.is_empty() {
quote! { 0 }
} else {
quote! { #( <<#types as #path::SerializeBuf>::Serialized as #path::medium::Medium>::SIZE )+* }
quote! { #( <#types as #path::SerializeBuf>::SIZE )+* }
}
}

Expand Down Expand Up @@ -307,13 +307,9 @@ fn size_of_enum(e: DataEnum, info: &BodyInfo, repr: Type) -> TokenStream2 {
.iter()
.filter_map(|variant| {
if !variant.fields.is_empty() {
let types: Vec<_> = variant
.fields
.iter()
.map(|field| &field.ty)
.collect();
let types: Vec<_> = variant.fields.iter().map(|field| &field.ty).collect();

Some(quote! { #(<<#types as #path::SerializeBuf>::Serialized as #path::medium::Medium>::SIZE)+* })
Some(quote! { #(<#types as #path::SerializeBuf>::SIZE)+* })
} else {
None
}
Expand All @@ -329,7 +325,7 @@ fn size_of_enum(e: DataEnum, info: &BodyInfo, repr: Type) -> TokenStream2 {
}
)*

max + <<#repr as #path::SerializeBuf>::Serialized as #path::medium::Medium>::SIZE
max + <#repr as #path::SerializeBuf>::SIZE
}}
}

Expand All @@ -339,7 +335,7 @@ pub fn serialize_iter(item: TokenStream) -> TokenStream {
let info = BodyInfo {
ident: item.ident,
generics: item.generics,
path: syn::parse2(quote! { cookie_cutter }).unwrap(),
path: syn::parse2(quote! { serac }).unwrap(),
};

let implementation = match item.data {
Expand All @@ -351,7 +347,7 @@ pub fn serialize_iter(item: TokenStream) -> TokenStream {
implementation.into()
}

pub fn serialize_buf(item: TokenStream) -> TokenStream {
pub fn impl_serialize_buf(item: TokenStream) -> TokenStream {
let item: DeriveInput = syn::parse2(item.into()).unwrap();

if !item.generics.params.is_empty() {
Expand All @@ -361,7 +357,7 @@ pub fn serialize_buf(item: TokenStream) -> TokenStream {
let info = BodyInfo {
ident: item.ident,
generics: item.generics,
path: syn::parse2(quote! { cookie_cutter }).unwrap(),
path: syn::parse2(quote! { serac }).unwrap(),
};

let size = match item.data {
Expand All @@ -372,12 +368,37 @@ pub fn serialize_buf(item: TokenStream) -> TokenStream {

let path = info.path;
let ident = info.ident;
let (impl_generics, ty_generics, where_clause) = info.generics.split_for_impl();
let ty = quote! { #ident #ty_generics };

quote! {
unsafe impl #impl_generics #path::SerializeBuf for #ty #ty_generics #where_clause {
type Serialized = [u8; #size];
unsafe impl #path::SerializeBuf for #ident {
const SIZE: usize = #size;
}

impl #ident {
/// Serialize into the serialization medium.
pub fn serialize_buf<'a>(
&self,
buf: &'a mut <#path::encoding::Vanilla as #path::Encoding>::Serialized<{ <Self as #path::SerializeBuf>::SIZE }>,
) where
&'a mut <#path::encoding::Vanilla as #path::Encoding>::Serialized<{ <Self as #path::SerializeBuf>::SIZE }>:
IntoIterator<Item = &'a mut <#path::encoding::Vanilla as #path::Encoding>::Word> + 'a,
{
unsafe { #path::SerializeIter::serialize_iter(self, buf).unwrap_unchecked() }
}

/// Deserialize from the serialization medium.
pub fn deserialize_buf<'a>(src: &'a <#path::encoding::Vanilla as #path::Encoding>::Serialized<{ <Self as #path::SerializeBuf>::SIZE }>) -> Result<Self, #path::error::Invalid>
where
&'a <#path::encoding::Vanilla as #path::Encoding>::Serialized<{ <Self as #path::SerializeBuf>::SIZE }>:
IntoIterator<Item = &'a <#path::encoding::Vanilla as #path::Encoding>::Word> + 'a,
{
#path::SerializeIter::deserialize_iter(src).or_else(|err| match err {
#path::error::Error::Invalid => Err(#path::error::Invalid),
// SAFETY: dependent on safety of trait implementation.
// `Serialized` must be of sufficient length.
#path::error::Error::EndOfInput => unsafe { ::core::hint::unreachable_unchecked() },
})
}
}
}
.into()
Expand Down
2 changes: 1 addition & 1 deletion packit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
cookie-cutter = { path = "../cookie-cutter" }
serac = { path = "../serac" }
crc = "3.0.1"
12 changes: 7 additions & 5 deletions cookie-cutter/Cargo.toml → serac/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[package]
name = "cookie-cutter"
name = "serac"
version = "0.1.0"
edition = "2021"
edition = "2024"
description = "A static, modular, and light-weight serialization framework."
license = "CC-BY-NC-SA-4.0"
repository = "https://github.com/adinack/embedded-command"

[dependencies]
macros = { path = "../macros", version = "0.1.0" }
macros = { package = "embedded-command-macros", version = "0.1.0" }
fill-array = "0.2.1"

# for binary
Expand All @@ -14,8 +17,7 @@ cortex-m-rt = { version = "0.7.3", optional = true }
defmt = { version = "0.3.10", optional = true }

[features]
binary = ["dep:panic-halt", "dep:cortex-m", "cortex-m-rt"]
cortex-m-rt = ["dep:cortex-m-rt"]
binary = ["dep:panic-halt", "dep:cortex-m", "dep:cortex-m-rt"]
defmt = ["dep:defmt"]

[[bin]]
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions serac/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub mod vanilla;
pub use vanilla::Vanilla;

/// Types implement this trait to be used as indication of a specific encoding
/// scheme.
pub trait Encoding {
/// The fundamental word of the encoding scheme.
///
/// i.e. `u8` for `[u8; ...]` mediums.
type Word;

/// The serialized form converted to and from by this encoding scheme.
type Serialized<const SIZE: usize>;
}
Loading
Loading