Skip to content

Commit

Permalink
added byte buffer impl
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed May 9, 2024
1 parent 5d3e8fb commit c83443d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/cst/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ fn parse_constant(node: &AstNode) -> Result<CstConstantStatement, CstParserError
name: name.ok_or(CstParserError::MissingContent { node_type: AstNodeType::StatementConstName })?.to_string(),
atoms: atom_buff,
}

)
}

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mod resolver;
mod compiler;
mod ast;
mod cst;
mod rst;
mod util;

fn main() {
let compiler = HexoCompiler::new(HexoCompilerContext::new());
Expand Down
1 change: 1 addition & 0 deletions src/rst/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod node;
1 change: 1 addition & 0 deletions src/rst/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) struct HexoFile {}
72 changes: 72 additions & 0 deletions src/util/byte_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
pub(crate) struct ByteBuffer {
inner: Vec<u8>,
}

impl ByteBuffer {
pub(crate) fn new() -> Self {
ByteBuffer {
inner: Vec::new()
}
}

pub(crate) fn push_byte(&mut self, byte: u8) {
self.inner.push(byte);
}

pub(crate) fn push_string(&mut self, string: String) {
self.inner.extend_from_slice(string.as_bytes());
}

pub(crate) fn push_u32(&mut self, num: u32) {
self.inner.extend_from_slice(num.to_be_bytes().as_slice());
}

pub(crate) fn len(&self) -> usize {
self.inner.len()
}

pub(crate) fn as_vec(&self) -> Vec<u8> {
self.inner.clone()
}
}

mod test {
use crate::util::ByteBuffer;

#[test]
fn byte_push() {
let mut buffer = ByteBuffer::new();
buffer.push_byte(0x01);
buffer.push_byte(0x02);

assert_eq!(buffer.len(), 2);

assert_eq!(buffer.as_vec(), vec![0x01, 0x02]);
}

#[test]
fn string_push() {
let mut buffer = ByteBuffer::new();
buffer.push_string("hello world".to_string());

assert_eq!(buffer.len(), 11);

assert_eq!(
buffer.as_vec(),
vec![104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
);
}

#[test]
fn u32_push() {
let mut buffer = ByteBuffer::new();
buffer.push_u32(13);

assert_eq!(buffer.len(), 4);

assert_eq!(
buffer.as_vec(),
vec![0, 0, 0, 13]
);
}
}
3 changes: 3 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod byte_buffer;

pub(crate) use byte_buffer::ByteBuffer;

0 comments on commit c83443d

Please sign in to comment.