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