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

Added LengthCodec, BincodeCodec and demo module #7

Merged
merged 27 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
76e171d
feat(demo): added demo module
JadKHaddad Nov 1, 2024
b7a2065
refactor(demo): made Header packet_length u16 big_endian
JadKHaddad Nov 1, 2024
ef7b235
feat(LengthCodec): added LengthCodec
JadKHaddad Nov 1, 2024
4b96715
test(LengthCodec): added LengthCodec tests
JadKHaddad Nov 1, 2024
d9c10c6
feat(BincodeCodec): added BincodeCodec
JadKHaddad Nov 2, 2024
da6eaeb
test(BincodeCodec): added BincodeCodec tests
JadKHaddad Nov 2, 2024
1161354
refactor(LengthCodec): removed logging
JadKHaddad Nov 2, 2024
e1d5ec5
feat(BincodeCodec): added tokio code impl for BincodeCodec
JadKHaddad Nov 2, 2024
63a58d6
refactor(LengthCodec, BincodeCodec): removed ZeroPayloadLength error
JadKHaddad Nov 2, 2024
04c0614
test(LengthCodec): added tests
JadKHaddad Nov 2, 2024
c90b417
refactor(BincodeCodec): moved BincodeMessage to test module
JadKHaddad Nov 2, 2024
4fb3294
fix(defmt): fix wrong defmt::Fromat impls
JadKHaddad Nov 2, 2024
467e1ac
refactor(test): moved tests to their modules
JadKHaddad Nov 2, 2024
b34aea6
chore(devcontainer): added devcontainer file
JadKHaddad Nov 2, 2024
f612363
fix(clippy): fix clippy nightly
JadKHaddad Nov 2, 2024
7c0f7a9
refactor(BincodeCodec): made BincodeCodec impl DecoderOwned and delet…
JadKHaddad Nov 4, 2024
d80d6c6
refactor(BincodeCodec): made tokio_codec module public
JadKHaddad Nov 4, 2024
9f2e841
docs(BufferTooSmall): updated docs for BufferTooSmall error
JadKHaddad Nov 4, 2024
8a8a9fc
fix(BincodeCodec): fixed impl tokio Encoder for BincodeCodec: payload…
JadKHaddad Nov 4, 2024
e742379
feat(demo): added Packet, checksum, encode and decode functionalities
JadKHaddad Nov 9, 2024
ec4cc63
feat(demo): added PacketCodec
JadKHaddad Nov 9, 2024
1579e4c
build(ci): added demo module to ci
JadKHaddad Nov 9, 2024
1ec7a77
chore(description): updated description in Cargo.toml
JadKHaddad Nov 9, 2024
a9edf33
docs(demo): added docs for demo module
JadKHaddad Nov 10, 2024
dcfda77
chore(cargo): reset cargo features
JadKHaddad Nov 10, 2024
2333c9b
docs(missing): allow missing docs for internal modules
JadKHaddad Nov 10, 2024
bfe9bcb
build(bincode): bincode feature test needs tokio and std features
JadKHaddad Nov 10, 2024
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
31 changes: 31 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/rust
{
"name": "Rust",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye",

// Use 'mounts' to make the cargo cache persistent in a Docker Volume.
"mounts": [
{
"source": "devcontainer-cargo-cache-${devcontainerId}",
"target": "/usr/local/cargo",
"type": "volume"
}
]

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "rustc --version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
12 changes: 12 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,22 @@ jobs:
- name: Clippy -- tracing
run: cargo clippy --no-default-features --features="tracing" -- -D warnings

- name: Clippy -- bincode
run: cargo clippy --no-default-features --features="bincode" -- -D warnings

- name: Clippy -- demo
run: cargo clippy --no-default-features --features="demo" -- -D warnings

# Test

- name: Test
run: cargo nextest run

- name: Test -- buffer-early-shift
run: cargo nextest run --features buffer-early-shift

- name: Test -- bincode
run: cargo nextest run --features "bincode,tokio,std"

- name: Test -- demo
run: cargo nextest run --features demo
41 changes: 36 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,69 @@ edition = "2021"
authors = ["Jad K. Haddad <jadkhaddad@gmail.com>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
description = "A simple and fast codec for encoding and decoding data in no_std environments"
description = "A simple and zerocopy codec for encoding and decoding data in no_std environments."
repository = "https://github.com/JadKHaddad/Cody-C"
keywords = ["codec", "no_std", "embedded", "encode", "decode"]

[features]
default = ["char-fmt"]
std = []
tokio = ["dep:tokio"]
std = ["bincode/std", "futures/std"]
tokio = ["dep:tokio", "dep:tokio-util"]
futures-io = ["futures/std"]
embedded-io-async = ["dep:embedded-io-async"]
bincode = ["dep:bincode"]
log = ["dep:log"]
tracing = ["dep:tracing"]
defmt = ["dep:defmt"]
pretty-hex-fmt = []
char-fmt = []
buffer-early-shift = []
demo = [
"dep:zerocopy",
"dep:serde",
"dep:serde-json-core",
"dep:derive_more",
"dep:crc32fast",
]

[dependencies]
tokio = { version = "1.37.0", optional = true, default-features = false, features = [
tokio = { version = "1.41.0", default-features = false, features = [
"io-util",
] }
], optional = true }
tokio-util = { version = "0.7.12", default-features = false, features = [
"codec",
], optional = true }
embedded-io-async = { version = "0.6.1", optional = true }
log = { version = "0.4.22", default-features = false, optional = true }
tracing = { version = "0.1.40", default-features = false, optional = true }
defmt = { version = "0.3.6", default-features = false, optional = true }
futures = { version = "0.3.31", default-features = false }
heapless = { version = "0.8.0", default-features = false }
bincode = { version = "2.0.0-rc.3", default-features = false, optional = true }

zerocopy = { version = "0.8.1", features = ["derive"], optional = true }
serde = { version = "1.0.214", default-features = false, features = [
"derive",
], optional = true }
serde-json-core = { version = "0.6.0", optional = true }
derive_more = { version = "1", default-features = false, features = [
"from",
], optional = true }
crc32fast = { version = "1.4.2", default-features = false, optional = true }


[dev-dependencies]
tokio = { version = "1", features = ["full"] }
tracing = { version = "0.1.40", default-features = false }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
heapless = { version = "0.8.0", default-features = false, features = ["serde"] }
serde = { version = "1.0.214", default-features = false, features = ["derive"] }
bincode = { version = "2.0.0-rc.3", default-features = false, features = [
"derive",
"std",
"serde",
] }


[package.metadata.docs.rs]
all-features = true
Expand Down
90 changes: 90 additions & 0 deletions src/codec/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,93 @@ impl<const N: usize> Encoder<Vec<u8, N>> for AnyDelimiterCodecOwned<'_, N> {
Encoder::encode(&mut self.inner, &item, dst)
}
}

#[cfg(test)]
mod test {
extern crate std;

use std::vec::Vec;

use futures::{pin_mut, SinkExt, StreamExt};
use tokio::io::AsyncWriteExt;

use crate::{
error, framed_read, sink_stream, test::init_tracing, tokio::Compat, FramedRead,
FramedReadError, FramedWrite,
};

use super::*;

#[tokio::test]
async fn framed_read() {
init_tracing();

let items: &[&[u8]] = &[
b"jh asjd##ppppppppppppppp##",
b"k hb##jsjuwjal kadj##jsadhjiu##w",
b"##jal kadjjsadhjiuwqens ##",
b"nd ",
b"yxxcjajsdi##askdn as",
b"jdasd##iouqw es",
b"sd##k",
];

let decoder = AnyDelimiterCodec::new(b"##");

let expected: &[&[u8]] = &[];
framed_read!(items, expected, decoder, 1, BufferTooSmall);
framed_read!(items, expected, decoder, 1, 1, BufferTooSmall);
framed_read!(items, expected, decoder, 1, 2, BufferTooSmall);
framed_read!(items, expected, decoder, 1, 4, BufferTooSmall);

framed_read!(items, expected, decoder, 2, BufferTooSmall);
framed_read!(items, expected, decoder, 2, 1, BufferTooSmall);
framed_read!(items, expected, decoder, 2, 2, BufferTooSmall);
framed_read!(items, expected, decoder, 2, 4, BufferTooSmall);

framed_read!(items, expected, decoder, 4, BufferTooSmall);
framed_read!(items, expected, decoder, 4, 1, BufferTooSmall);
framed_read!(items, expected, decoder, 4, 2, BufferTooSmall);
framed_read!(items, expected, decoder, 4, 4, BufferTooSmall);

let expected: &[&[u8]] = &[b"jh asjd"];
framed_read!(items, expected, decoder, 16, BufferTooSmall);

let expected: &[&[u8]] = &[
b"jh asjd",
b"ppppppppppppppp",
b"k hb",
b"jsjuwjal kadj",
b"jsadhjiu",
b"w",
b"jal kadjjsadhjiuwqens ",
b"nd yxxcjajsdi",
b"askdn asjdasd",
b"iouqw essd",
];
framed_read!(items, expected, decoder, 32, BytesRemainingOnStream);
framed_read!(items, expected, decoder, 32, 1, BytesRemainingOnStream);
framed_read!(items, expected, decoder, 32, 2, BytesRemainingOnStream);
framed_read!(items, expected, decoder, 32, 4, BytesRemainingOnStream);

framed_read!(items, expected, decoder);
}

#[tokio::test]
async fn sink_stream() {
init_tracing();

let items: Vec<heapless::Vec<u8, 32>> = std::vec![
heapless::Vec::from_slice(b"Hello").unwrap(),
heapless::Vec::from_slice(b"Hello, world!").unwrap(),
heapless::Vec::from_slice(b"Hei").unwrap(),
heapless::Vec::from_slice(b"sup").unwrap(),
heapless::Vec::from_slice(b"Hey").unwrap(),
];

let decoder = AnyDelimiterCodecOwned::<32>::new(b"###");
let encoder = AnyDelimiterCodecOwned::<32>::new(b"###");

sink_stream!(encoder, decoder, items);
}
}
Loading
Loading