Skip to main content

arora_types/
lib.rs

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