Skip to main content

arora_types/
lib.rs

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