Skip to content

Commit

Permalink
modbus: project layout and base traits
Browse files Browse the repository at this point in the history
Adds basic modules that we will be using to write common traits. Also
creates a new workspace for modbus to show how we can layout protocols.
  • Loading branch information
cccs-sadugas committed Sep 3, 2020
1 parent b4fbbba commit 021bab4
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
[package]
name = "sawp"
version = "0.1.0"
authors = ["cccs-rtmorti"]
authors = ["cccs-rtmorti", "cccs-sadugas"]
description = "Security Aware Wire Protocol parsing library"
readme = "README.md"
edition = "2018"
license-file = "LICENSE"

[workspace]
members = [
"sawp-modbus"
]

[lib]
crate-type = ["staticlib", "rlib", "cdylib"]

[dependencies]
[dev-dependencies]
criterion = "0.3"

[[bench]]
name = "modbus"
harness = false
13 changes: 13 additions & 0 deletions benches/modbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

// TODO: benchmark modbus protocol parsing
fn modbus(n: u64) -> bool {
n == 20
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("modbus", |b| b.iter(|| modbus(black_box(20))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
14 changes: 14 additions & 0 deletions sawp-modbus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sawp-modbus"
version = "0.1.0"
authors = ["cccs-sadugas"]
readme = "../README.md"
edition = "2018"
license-file = "../LICENSE"

[dependencies]
sawp = { path = ".." }
nom = "5.1.2"

[dev-dependencies]
rstest = "0.6.4"
41 changes: 41 additions & 0 deletions sawp-modbus/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use sawp::error::{Error, ErrorKind, Result};
use sawp::parser::Parse;
use sawp::protocol::Protocol;

#[derive(Debug)]
pub struct Modbus {}

#[derive(Debug, PartialEq)]
pub struct Message {}

impl Protocol for Modbus {
type Message = Message;

fn name() -> &'static str {
"modbus"
}
}

impl Parse for Modbus {
fn parse(_input: &[u8]) -> Result<(usize, Self::Message)> {
Err(Error::new(ErrorKind::Unimplemented))
}
}

#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
use sawp::error::{Error, ErrorKind, Result};

#[rstest(
input,
expected,
case(b"", Err(Error::new(ErrorKind::Unimplemented))),
case(b"hello world", Err(Error::new(ErrorKind::Unimplemented)))
)]
#[test]
fn test_modbus(input: &[u8], expected: Result<(usize, <Modbus as Protocol>::Message)>) {
assert_eq!(Modbus::parse(input), expected);
}
}
25 changes: 25 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, PartialEq)]
pub struct Error {
kind: ErrorKind,
}

impl Error {
pub fn new(kind: ErrorKind) -> Self {
Self { kind }
}
}

#[derive(Debug, PartialEq)]
pub enum ErrorKind {
Unimplemented,
}

impl std::fmt::Display for Error {
fn fmt(&self, _: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
todo!()
}
}

impl std::error::Error for Error {}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub mod error;
pub mod parser;
pub mod protocol;

#[cfg(test)]
mod tests {
#[test]
Expand Down
10 changes: 10 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::error::Result;
use crate::protocol::Protocol;

pub trait Parse: Protocol {
/// Parse a chunk of bytes into the protocol's Message type.
///
/// Returns a tuple with the number of bytes parsed from the input and the
/// parsed message.
fn parse(input: &[u8]) -> Result<(usize, Self::Message)>;
}
8 changes: 8 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Represents the basic elements of a protocol
pub trait Protocol {
/// Type of message returned when parsing
type Message;

/// Protocol name string
fn name() -> &'static str;
}

0 comments on commit 021bab4

Please sign in to comment.