Skip to content

Commit 459bde4

Browse files
committed
Add uuid macro
1 parent 40fbb30 commit 459bde4

File tree

9 files changed

+147
-15
lines changed

9 files changed

+147
-15
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,29 @@ bitwarden-api-identity = { path = "crates/bitwarden-api-identity", version = "=1
2424
bitwarden-cli = { path = "crates/bitwarden-cli", version = "=1.0.0" }
2525
bitwarden-core = { path = "crates/bitwarden-core", version = "=1.0.0" }
2626
bitwarden-crypto = { path = "crates/bitwarden-crypto", version = "=1.0.0" }
27+
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
28+
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
2729
bitwarden-exporters = { path = "crates/bitwarden-exporters", version = "=1.0.0" }
2830
bitwarden-fido = { path = "crates/bitwarden-fido", version = "=1.0.0" }
2931
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0" }
3032
bitwarden-ipc = { path = "crates/bitwarden-ipc", version = "=1.0.0" }
3133
bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
3234
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
3335
bitwarden-ssh = { path = "crates/bitwarden-ssh", version = "=1.0.0" }
36+
bitwarden-uuid = { path = "crates/bitwarden-uuid", version = "=1.0.0" }
37+
bitwarden-uuid-macro = { path = "crates/bitwarden-uuid-macro", version = "=1.0.0" }
3438
bitwarden-vault = { path = "crates/bitwarden-vault", version = "=1.0.0" }
35-
bitwarden-error = { path = "crates/bitwarden-error", version = "=1.0.0" }
36-
bitwarden-error-macro = { path = "crates/bitwarden-error-macro", version = "=1.0.0" }
3739

3840
# External crates that are expected to maintain a consistent version across all crates
3941
chrono = { version = ">=0.4.26, <0.5", features = [
4042
"clock",
4143
"serde",
4244
"std",
4345
], default-features = false }
46+
js-sys = { version = ">=0.3.72, <0.4" }
4447
log = ">=0.4.18, <0.5"
48+
proc-macro2 = ">=1.0.89, <2"
49+
quote = ">=1.0.37, <2"
4550
reqwest = { version = ">=0.12.5, <0.13", features = [
4651
"json",
4752
"multipart",
@@ -52,6 +57,7 @@ serde = { version = ">=1.0, <2.0", features = ["derive"] }
5257
serde_json = ">=1.0.96, <2.0"
5358
serde_qs = ">=0.12.0, <0.16"
5459
serde_repr = ">=0.1.12, <0.2"
60+
syn = ">=2.0.87, <3"
5561
thiserror = ">=1.0.40, <3"
5662
tokio = { version = "1.36.0", features = ["macros"] }
5763
tsify-next = { version = ">=0.5.4, <0.6", features = [
@@ -61,7 +67,6 @@ uniffi = "=0.28.3"
6167
uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4", "js"] }
6268
validator = { version = ">=0.18.1, <0.20", features = ["derive"] }
6369
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
64-
js-sys = { version = ">=0.3.72, <0.4" }
6570
wasm-bindgen-futures = "0.4.41"
6671

6772
# There is an incompatibility when using pkcs5 and chacha20 on wasm builds. This can be removed once a new

crates/bitwarden-error-macro/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ wasm = []
1919

2020
[dependencies]
2121
darling = "0.20.10"
22-
proc-macro2 = "1.0.89"
23-
quote = "1.0.37"
24-
syn = "2.0.87"
22+
proc-macro2 = { workspace = true }
23+
quote = { workspace = true }
24+
syn = { workspace = true }
2525

2626
[lints]
2727
workspace = true
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[package]
2+
name = "bitwarden-uuid-macro"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[features]
18+
wasm = []
19+
20+
[dependencies]
21+
proc-macro2 = { workspace = true }
22+
syn = { workspace = true }
23+
quote = { workspace = true }
24+
25+
[lints]
26+
workspace = true
27+
28+
[lib]
29+
proc-macro = true
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
use syn::{
4+
parse::{Parse, ParseStream},
5+
parse_macro_input, Ident, Visibility,
6+
};
7+
8+
#[proc_macro]
9+
pub fn uuid(input: TokenStream) -> TokenStream {
10+
// Parse input as: vis ident
11+
let input = parse_macro_input!(input as IdTypeInput);
12+
let ident = input.ident;
13+
let vis = input.vis;
14+
let name_str = ident.to_string();
15+
16+
let tsify_type = format!("Tagged<Uuid, \"{}\">", name_str);
17+
18+
let expanded = quote! {
19+
#[cfg(feature = "wasm")]
20+
#[derive(serde::Serialize, serde::Deserialize, tsify_next::Tsify)]
21+
#[tsify(into_wasm_abi, from_wasm_abi)]
22+
#[repr(transparent)]
23+
#vis struct #ident(
24+
#[tsify(type = #tsify_type)]
25+
pub uuid::Uuid
26+
);
27+
28+
#[cfg(not(feature = "wasm"))]
29+
#[derive(serde::Serialize, serde::Deserialize)]
30+
#[repr(transparent)]
31+
#vis struct #ident(
32+
pub uuid::Uuid
33+
);
34+
35+
#[cfg(feature = "uniffi")]
36+
uniffi::custom_newtype!(#ident, uuid::Uuid);
37+
};
38+
39+
TokenStream::from(expanded)
40+
}
41+
42+
// Helper struct to parse "vis ident"
43+
struct IdTypeInput {
44+
vis: Visibility,
45+
ident: Ident,
46+
}
47+
48+
impl Parse for IdTypeInput {
49+
fn parse(input: ParseStream) -> syn::Result<Self> {
50+
let vis: Visibility = input.parse()?;
51+
let ident: Ident = input.parse()?;
52+
Ok(IdTypeInput { vis, ident })
53+
}
54+
}

crates/bitwarden-uuid/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "bitwarden-uuid"
3+
description = """
4+
Internal crate for the bitwarden crate. Do not use.
5+
"""
6+
7+
version.workspace = true
8+
authors.workspace = true
9+
edition.workspace = true
10+
rust-version.workspace = true
11+
readme.workspace = true
12+
homepage.workspace = true
13+
repository.workspace = true
14+
license-file.workspace = true
15+
keywords.workspace = true
16+
17+
[features]
18+
wasm = [
19+
"bitwarden-uuid-macro/wasm",
20+
#"dep:js-sys",
21+
#"dep:tsify-next",
22+
#"dep:wasm-bindgen",
23+
]
24+
25+
[dependencies]
26+
bitwarden-uuid-macro = { workspace = true }
27+
28+
[lints]
29+
workspace = true
30+
31+
[dev-dependencies]

crates/bitwarden-uuid/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub use bitwarden_uuid_macro::uuid;

crates/bitwarden-vault/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ keywords.workspace = true
1818
uniffi = [
1919
"bitwarden-core/uniffi",
2020
"bitwarden-crypto/uniffi",
21-
"dep:uniffi"
21+
"dep:uniffi",
2222
] # Uniffi bindings
2323
wasm = ["dep:tsify-next", "dep:wasm-bindgen"] # WASM support
2424

@@ -28,6 +28,7 @@ bitwarden-api-api = { workspace = true }
2828
bitwarden-core = { workspace = true, features = ["internal"] }
2929
bitwarden-crypto = { workspace = true }
3030
bitwarden-error = { workspace = true }
31+
bitwarden-uuid = { workspace = true }
3132
chrono = { workspace = true }
3233
data-encoding = ">=2.0, <3"
3334
hmac = ">=0.12.1, <0.13"

crates/bitwarden-vault/src/cipher/cipher_client.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
use bitwarden_core::Client;
22
use bitwarden_crypto::IdentifyKey;
3+
use bitwarden_uuid::uuid;
34
#[cfg(feature = "wasm")]
45
use wasm_bindgen::prelude::*;
56

67
use crate::{
78
Cipher, CipherError, CipherListView, CipherView, DecryptError, EncryptError, VaultClient,
89
};
910

10-
/// NewType representing an Organization ID. This ensures type safety.
11-
#[cfg_attr(
12-
feature = "wasm",
13-
derive(serde::Serialize, serde::Deserialize, tsify_next::Tsify),
14-
tsify(into_wasm_abi, from_wasm_abi)
15-
)]
16-
#[repr(transparent)]
17-
pub struct OrganizationId(#[tsify(type = r#"Tagged<Uuid, "OrganizationId">"#)] pub uuid::Uuid);
11+
uuid!(pub OrganizationId);
1812

1913
#[cfg_attr(feature = "wasm", wasm_bindgen)]
2014
pub struct CiphersClient {

0 commit comments

Comments
 (0)