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