Skip to main content

arora_types/
lib.rs

1pub mod call;
2pub mod data;
3pub mod keyvalue;
4pub mod module;
5pub mod record;
6pub mod ty;
7pub mod value;
8
9#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
10pub mod wasm_value;
11
12use derive_more::Display;
13use semver::Version;
14use serde::{Deserialize, Serialize};
15use std::collections::hash_map::DefaultHasher;
16use std::hash::{Hash, Hasher};
17use uuid::Uuid;
18
19pub fn gen_uuid_from_str(key: &str) -> uuid::Uuid {
20  match Uuid::parse_str(key) {
21    Ok(uuid) => uuid,
22    Err(_) => {
23      // Generate a UUID based on the string key
24      // Generate a deterministic UUID based on the string content
25      let mut hasher = DefaultHasher::new();
26      key.hash(&mut hasher);
27      let hash = hasher.finish();
28
29      // Create a byte array with the hash value
30      let mut bytes = [0u8; 16];
31      bytes[0..8].copy_from_slice(&hash.to_le_bytes());
32
33      // Use part of the hash again for the second half
34      let hash2 = hash.wrapping_mul(31);
35      bytes[8..16].copy_from_slice(&hash2.to_le_bytes());
36
37      // Set version to 4 and variant to RFC4122
38      bytes[6] = (bytes[6] & 0x0F) | 0x40; // version 4
39      bytes[8] = (bytes[8] & 0x3F) | 0x80; // variant
40
41      Uuid::from_bytes(bytes)
42    }
43  }
44}
45
46pub fn gen_bb_uuid() -> Uuid {
47  Uuid::new_v4()
48}
49
50#[derive(Serialize, Deserialize, Debug, Display, Clone)]
51#[display("{}.{}.{}", major, minor, patch)]
52pub struct SemanticVersion {
53  pub major: u32,
54  pub minor: u32,
55  pub patch: u32,
56}
57
58impl From<SemanticVersion> for Version {
59  fn from(val: SemanticVersion) -> Self {
60    Version::new(val.major.into(), val.minor.into(), val.patch.into())
61  }
62}
63
64#[cfg(test)]
65mod tests {
66  use crate::module::high::{ExportSymbol, ModuleDefinition};
67  use std::str::FromStr;
68  use uuid::Uuid;
69
70  #[test]
71  fn parse_uuid() {
72    let uuid_string = "b41899c3-66dc-40d4-ab61-d1ccf5231c88";
73    let expected = Uuid::from_str(uuid_string).unwrap();
74    let actual: Uuid = serde_yaml::from_str(uuid_string).unwrap();
75    assert!(actual == expected);
76  }
77
78  #[test]
79  fn parse_simple_function() {
80    let function_string = "\
81type: function
82id: 07f5740c-ba4a-45af-8ec5-bedde5737e99
83name: test
84ret:
85  kind: scalar
86  id: i32";
87    let symbol: ExportSymbol = serde_yaml::from_str(function_string).unwrap();
88    match symbol {
89      ExportSymbol::Function(function) => assert!(function.name == "test"),
90    }
91  }
92
93  #[test]
94  fn parse_function() {
95    let function_string = "\
96type: function
97id: 07f5740c-ba4a-45af-8ec5-bedde5737e99
98name: test
99parameters:
100  - id: b41899c3-66dc-40d4-ab61-d1ccf5231c88
101    name: a
102    type:
103      kind: scalar
104      id: Status
105  - id: 63086e48-804f-403a-8862-3358ddedc08d
106    name: b
107    type:
108      kind: scalar
109      id: i32
110ret:
111  kind: scalar
112  id: i32";
113    let symbol: ExportSymbol = serde_yaml::from_str(function_string).unwrap();
114    match symbol {
115      ExportSymbol::Function(function) => assert!(function.name == "test"),
116    }
117  }
118
119  #[test]
120  fn parse_simple_module() {
121    let module_string = "\
122id: 325c5e47-32db-4e23-a38f-7a2849647e0c
123author: Semio
124description: Test C++ module
125license: Proprietary
126name: test-cpp-2
127version:
128  major: 0
129  minor: 1
130  patch: 0
131executor:
132  name: wasm
133exports:
134  - type: function
135    id: 07f5740c-ba4a-45af-8ec5-bedde5737e99
136    name: test
137    parameters:
138      - id: b41899c3-66dc-40d4-ab61-d1ccf5231c88
139        name: a
140        type:
141          kind: scalar
142          id: Status
143      - id: 63086e48-804f-403a-8862-3358ddedc08d
144        name: b
145        type:
146          kind: scalar
147          id: i32
148    ret:
149      kind: scalar
150      id: i32
151imports: []
152dependencies: []
153executable_mime: application/wasm";
154
155    let header: ModuleDefinition = serde_yaml::from_str(module_string).unwrap();
156    assert!(header.name == "test-cpp-2");
157  }
158}