Lines
100 %
Functions
Branches
use super::*;
use scripting_format::OutputHeader;
/// An output buffer holding one entity per `(entity_type, value_type)`.
fn output(entities: &[(EntityType, ValueType)]) -> Vec<u8> {
let mut header = OutputHeader::new(0);
header.output_entity_count = u32::try_from(entities.len()).unwrap_or(u32::MAX);
let mut bytes = header.to_bytes().to_vec();
for (entity_type, value_type) in entities {
let data_offset = u32::try_from(bytes.len() + ENTITY_HEADER_SIZE).unwrap_or(u32::MAX);
let entity = EntityHeader {
entity_type: *entity_type as u8,
operation: 0,
flags: 0,
reserved: [0],
id: [0; 16],
parent_idx: -1,
data_offset,
data_size: u32::try_from(DEBUG_VALUE_DATA_SIZE).unwrap_or(u32::MAX),
};
bytes.extend_from_slice(&entity.to_bytes());
let value = DebugValueData {
value_type: *value_type as u8,
reserved: [0; 7],
data1: 1,
data2: 1,
bytes.extend_from_slice(&value.to_bytes());
}
bytes
fn debug(value_type: ValueType) -> (EntityType, ValueType) {
(EntityType::DebugValue, value_type)
/// One debug value as the guest writes it: its type, `data1` / `data2` and
/// the `inline` bytes after its fixed data.
type Item<'a> = (ValueType, i64, i64, &'a [u8]);
/// An output buffer holding one debug value of `value_type` with `data1` /
/// `data2` and `inline` bytes after its fixed data, as the guest writes it.
fn output_one(value_type: ValueType, data1: i64, data2: i64, inline: &[u8]) -> Vec<u8> {
output_run(&[(value_type, data1, data2, inline)])
/// An output buffer holding each of `items` as one debug value entity.
fn output_run(items: &[Item]) -> Vec<u8> {
header.output_entity_count = u32::try_from(items.len()).unwrap_or(u32::MAX);
for (value_type, data1, data2, inline) in items {
let data_size = u32::try_from(DEBUG_VALUE_DATA_SIZE + inline.len()).unwrap_or(u32::MAX);
entity_type: EntityType::DebugValue as u8,
data_offset: u32::try_from(bytes.len() + ENTITY_HEADER_SIZE).unwrap_or(u32::MAX),
data_size,
data1: *data1,
data2: *data2,
bytes.extend_from_slice(inline);
fn symbol(name: &str) -> Item<'_> {
(
ValueType::Symbol,
0,
i64::try_from(name.len()).unwrap_or(i64::MAX),
name.as_bytes(),
)
fn number(n: i64) -> Item<'static> {
(ValueType::Number, n, 1, &[])
fn scalar(numer: i64, denom: i64) -> Item<'static> {
(ValueType::Scalar, numer, denom, &[])
fn tag(value_type: ValueType) -> Item<'static> {
(value_type, 0, 0, &[])
fn printed(items: &[Item]) -> Result<String> {
decode_result(&output_run(items)).map(|value| nomiscript::format_value(&value))
/// The money tag (plan 0014 Stage 1, ISS-018): numerator and denominator in
/// the data fields, the commodity id's high and low i64 halves inline, each
/// little-endian as the guest's `i64.store` writes them.
#[test]
fn atomic_money_decodes_with_its_commodity_id() {
let id = uuid::Uuid::from_u128(0x0123_4567_89ab_cdef_0f1e_2d3c_4b5a_6978);
let (hi, lo) = id.as_u64_pair();
let mut inline = hi.to_le_bytes().to_vec();
inline.extend_from_slice(&lo.to_le_bytes());
let data = output_one(ValueType::Commodity, 3, 2, &inline);
assert_eq!(
decode_result(&data),
Ok(Value::Commodity {
amount: Fraction::new(3, 2),
commodity_id: id,
})
);
let truncated = output_one(ValueType::Commodity, 3, 2, &inline[..8]);
assert!(matches!(
decode_result(&truncated),
Err(DecodeError::Malformed(_))
));
fn a_non_positive_denominator_is_malformed_not_a_panic() {
for value_type in [ValueType::Number, ValueType::Scalar, ValueType::Commodity] {
for (numer, denom) in [(1, 0), (1, i64::MIN), (i64::MIN, -1)] {
let data = output_one(value_type, numer, denom, &[0; 16]);
Err(DecodeError::Malformed(
"non-positive denominator".to_string()
))
/// The entity tag (plan 0014 Stage 1, ISS-058): the kind's wire code alone,
/// decoded as the struct the eval-path plist names.
fn an_entity_tag_decodes_as_its_kind() {
for (kind, name) in [
(EntityKind::Account, "account"),
(EntityKind::Transaction, "transaction"),
(EntityKind::Commodity, "commodity"),
(EntityKind::Tag, "tag"),
] {
let data = output_one(ValueType::Entity, kind.wire_code(), 0, &[]);
Ok(Value::Struct {
name: name.to_string(),
fields: Vec::new(),
}),
"{kind}"
let condition = output_one(ValueType::Entity, EntityKind::Condition.wire_code(), 0, &[]);
assert_eq!(decode_result(&condition), Ok(Value::Condition));
let unknown = output_one(ValueType::Entity, 99, 0, &[]);
decode_result(&unknown),
fn a_nested_list_decodes_in_pre_order() {
let data = output(&[
debug(ValueType::List),
debug(ValueType::Number),
debug(ValueType::T),
debug(ValueType::ListEnd),
]);
let value = decode_result(&data).map(|value| nomiscript::format_value(&value));
assert_eq!(value.ok().as_deref(), Some("(1 (t))"));
fn other_entities_between_list_items_are_skipped() {
(EntityType::Tag, ValueType::Nil),
assert_eq!(value.ok().as_deref(), Some("(t)"));
fn the_last_complete_value_is_the_result() {
let data = output(&[debug(ValueType::T), debug(ValueType::Nil)]);
assert!(matches!(decode_result(&data), Ok(Value::Nil)));
fn a_malformed_list_run_is_an_error() {
for entities in [
vec![debug(ValueType::List), debug(ValueType::T)],
vec![debug(ValueType::ListEnd)],
decode_result(&output(&entities)),
fn an_output_without_a_debug_value_is_absent_not_malformed() {
decode_result(&output(&[(EntityType::Tag, ValueType::Nil)])),
Err(DecodeError::Absent("no debug value in output"))
decode_result(&output(&[])),
Err(DecodeError::Absent("no output entities"))
fn the_output_region_stops_at_the_buffer_capacity() {
let memory: Vec<u8> = (0..=255).collect();
assert_eq!(output_region(&memory, 16, 8).ok(), Some(&memory[16..24]));
fn an_output_region_outside_memory_is_an_error() {
let memory = [0u8; 32];
assert!(output_region(&memory, 16, 17).is_err());
assert!(output_region(&memory, usize::MAX, 1).is_err());
/// A decodable value just past the capacity is not part of the result.
fn a_value_written_past_the_capacity_is_not_decoded() {
let bytes = output(&[(EntityType::DebugValue, ValueType::Nil)]);
let region = output_region(&bytes, 0, OUTPUT_HEADER_SIZE + ENTITY_HEADER_SIZE);
assert!(region.and_then(decode_result).is_err());
assert!(decode_result(&bytes).is_ok());
fn a_struct_run_decodes_to_a_named_struct_printed_as_cl() {
let run = [
tag(ValueType::Struct),
symbol("PT"),
symbol(":X"),
number(8),
tag(ValueType::ListEnd),
];
assert_eq!(printed(&run).as_deref(), Ok("#S(PT :X 8)"));
let decoded = decode_result(&output_run(&run));
decoded,
name: "PT".to_string(),
fields: vec![("X".to_string(), Value::Number(Number::Index(8)))],
fn nested_structs_and_nil_fields_print_as_cl() {
tag(ValueType::List),
symbol("BOX2"),
symbol(":INNER"),
symbol(":TAIL"),
tag(ValueType::Nil),
printed(&run).as_deref(),
Ok("(#S(BOX2 :INNER #S(PT :X 8) :TAIL nil))")
fn a_struct_without_a_name_or_a_field_value_is_malformed() {
for run in [
vec![tag(ValueType::Struct), tag(ValueType::ListEnd)],
vec![tag(ValueType::Struct), number(1), tag(ValueType::ListEnd)],
vec![
number(1),
number(2),
],
assert!(matches!(printed(&run), Err(DecodeError::Malformed(_))));
fn an_index_and_a_scalar_print_with_their_kind() {
assert_eq!(printed(&[number(2)]), Ok("2".to_string()));
assert_eq!(printed(&[scalar(2, 1)]), Ok("2/1".to_string()));
assert_eq!(printed(&[scalar(1, 4)]), Ok("1/4".to_string()));
fn an_index_with_a_denominator_is_malformed() {
let run = [(ValueType::Number, 1, 2, &[][..])];
printed(&run),
"index with a denominator".to_string()