rrefactor

This commit is contained in:
2025-03-16 08:02:29 +01:00
parent 3b1068a3a8
commit 4796e4fe82
37 changed files with 896 additions and 115 deletions

View File

@@ -13,6 +13,8 @@ pub fn encode[T](obj T) ![]u8 {
$if field.typ is string {
// $(string_expr) produces an identifier
d.add_string(obj.$(field.name).str())
} $else $if field.typ is bool {
d.add_bool(bool(obj.$(field.name)))
} $else $if field.typ is int {
d.add_int(int(obj.$(field.name)))
} $else $if field.typ is u8 {
@@ -70,6 +72,8 @@ pub fn decode[T](data []u8) !T {
$if field.typ is string {
// $(string_expr) produces an identifier
result.$(field.name) = d.get_string()!
} $else $if field.typ is bool {
result.$(field.name) = d.get_bool()!
} $else $if field.typ is int {
result.$(field.name) = d.get_int()!
} $else $if field.typ is u8 {

View File

@@ -54,6 +54,11 @@ pub fn (mut d Decoder) get_bytes() ![]u8 {
return bytes
}
pub fn (mut d Decoder) get_bool() !bool {
val := d.get_u8()!
return val == 1
}
// adds u16 length of string in bytes + the bytes
pub fn (mut d Decoder) get_u8() !u8 {
if d.data.len < 1 {

View File

@@ -57,6 +57,14 @@ pub fn (mut b Encoder) add_bytes(data []u8) {
b.data << data
}
pub fn (mut b Encoder) add_bool(data bool) {
if data {
b.add_u8(1)
} else {
b.add_u8(0)
}
}
pub fn (mut b Encoder) add_u8(data u8) {
b.data << data
}

View File

@@ -37,6 +37,17 @@ fn test_bytes() {
assert d.get_list_u8()! == sb
}
fn test_bool() {
mut e := new()
e.add_bool(true)
e.add_bool(false)
assert e.data == [u8(1), 0]
mut d := decoder_new(e.data)
assert d.get_bool()! == true
assert d.get_bool()! == false
}
fn test_u8() {
mut e := new()
e.add_u8(min_u8)
@@ -88,7 +99,8 @@ fn test_time() {
e.add_time(t)
mut d := decoder_new(e.data)
assert d.get_time()! == t
// Compare unix timestamps instead of full time objects
assert d.get_time()!.unix() == t.unix()
}
fn test_list_string() {
@@ -198,7 +210,13 @@ fn encode_decode_struct[T](input StructType[T]) bool {
console.print_debug('Failed to decode, error: ${err}')
return false
}
return input == output
$if T is time.Time {
// Special handling for time.Time comparison
return input.val.unix() == output.val.unix()
} $else {
return input == output
}
}
fn test_struct() {
@@ -230,6 +248,11 @@ fn test_struct() {
// time.Time
// assert encode_decode_struct[time.Time](get_empty_struct_input[time.Time]()) // get error here
assert encode_decode_struct[time.Time](get_struct_input[time.Time](time.now()))
// bool
assert encode_decode_struct(get_empty_struct_input[bool]())
assert encode_decode_struct(get_struct_input(true))
assert encode_decode_struct(get_struct_input(false))
// string array
assert encode_decode_struct(get_empty_struct_input[[]string]())

View File

@@ -27,6 +27,7 @@ The binary format starts with a version byte (currently v1), followed by the enc
### Primitive Types
- `string`
- `int` (32-bit)
- `bool`
- `u8`
- `u16`
- `u32`
@@ -61,6 +62,7 @@ mut e := encoder.new()
// Add primitive values
e.add_string('hello')
e.add_int(42)
e.add_bool(true)
e.add_u8(255)
e.add_u16(65535)
e.add_u32(4294967295)
@@ -89,6 +91,7 @@ mut d := encoder.decoder_new(encoded)
// Read values in same order as encoded
str := d.get_string()
num := d.get_int()
bool_val := d.get_bool()
byte := d.get_u8()
u16_val := d.get_u16()
u32_val := d.get_u32()