Skip to content

Commit

Permalink
feat: get node type a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
sstelfox committed Feb 11, 2024
1 parent a89a687 commit b2f976b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/codec/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod node_type;

pub use node_type::NodeType;
65 changes: 65 additions & 0 deletions src/codec/filesystem/node_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use async_trait::async_trait;
use futures::{AsyncWrite, AsyncWriteExt};
use nom::bytes::streaming::take;
use nom::error::Error as NomError;
use nom::error::ErrorKind;

use crate::codec::AsyncEncodable;

#[derive(Debug, PartialEq)]
pub enum NodeType {
File,
}

impl NodeType {
pub fn parse(input: &[u8]) -> nom::IResult<&[u8], Self, NomError<&[u8]>> {
let (input, node_type) = take(1u8)(input)?;
let node_type = node_type[0];

let parsed_type = match node_type {
0x00 => Self::File,
_ => return Err(nom::Err::Error(NomError::new(input, ErrorKind::Tag))),
};

Ok((input, parsed_type))
}
}

#[async_trait]
impl AsyncEncodable for NodeType {
async fn encode<W: AsyncWrite + Unpin + Send>(
&self,
writer: &mut W,
pos: usize,
) -> std::io::Result<usize> {
let type_byte = match self {
Self::File => 0x00,
};

writer.write_all(&[type_byte]).await?;
Ok(pos + 1)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test(async))]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_round_trip_file() {
let node_type = NodeType::File;
let raw_bytes = [0x00];

let mut encoded = Vec::new();
node_type.encode(&mut encoded, 0).await.unwrap();
assert_eq!(raw_bytes, encoded.as_slice());

let (remaining, parsed) = NodeType::parse(&raw_bytes).unwrap();
assert!(remaining.is_empty());
assert_eq!(node_type, parsed);
}
}
1 change: 1 addition & 0 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod content_payload;
pub mod crypto;
pub mod filesystem;
mod filesystem_id;
pub mod header;

Expand Down

0 comments on commit b2f976b

Please sign in to comment.