-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Extract the `require` macro and common uniffi logic to a new crate called `bitwarden-core` since it's required for extracting other bitwarden functionality.
- Loading branch information
Showing
40 changed files
with
190 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "bitwarden-core" | ||
description = """ | ||
Internal crate for the bitwarden crate. Do not use. | ||
""" | ||
|
||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license-file.workspace = true | ||
keywords.workspace = true | ||
|
||
[features] | ||
uniffi = ["dep:uniffi"] | ||
|
||
[dependencies] | ||
chrono = { version = ">=0.4.26, <0.5", default-features = false } | ||
uniffi = { version = "=0.27.2", optional = true } | ||
uuid = { version = ">=1.3.3, <2.0", features = ["serde"] } | ||
thiserror = ">=1.0.40, <2.0" | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
#[error("The response received was missing a required field: {0}")] | ||
pub struct MissingFieldError(pub &'static str); | ||
|
||
/// This macro is used to require that a value is present or return an error otherwise. | ||
/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and | ||
/// with a more descriptive error message. | ||
/// Note that this macro will return early from the function if the value is not present. | ||
#[macro_export] | ||
macro_rules! require { | ||
($val:expr) => { | ||
match $val { | ||
Some(val) => val, | ||
None => return Err($crate::MissingFieldError(stringify!($val)).into()), | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#[cfg(feature = "uniffi")] | ||
uniffi::setup_scaffolding!(); | ||
#[cfg(feature = "uniffi")] | ||
mod uniffi_support; | ||
|
||
mod error; | ||
pub use error::MissingFieldError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use uuid::Uuid; | ||
|
||
use crate::UniffiCustomTypeConverter; | ||
|
||
type DateTime = chrono::DateTime<chrono::Utc>; | ||
uniffi::custom_type!(DateTime, std::time::SystemTime); | ||
|
||
impl UniffiCustomTypeConverter for chrono::DateTime<chrono::Utc> { | ||
type Builtin = std::time::SystemTime; | ||
|
||
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> { | ||
Ok(Self::from(val)) | ||
} | ||
|
||
fn from_custom(obj: Self) -> Self::Builtin { | ||
obj.into() | ||
} | ||
} | ||
|
||
uniffi::custom_type!(Uuid, String); | ||
|
||
impl UniffiCustomTypeConverter for Uuid { | ||
type Builtin = String; | ||
|
||
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> { | ||
Uuid::parse_str(val.as_str()).map_err(|e| e.into()) | ||
} | ||
|
||
fn from_custom(obj: Self) -> Self::Builtin { | ||
obj.to_string() | ||
} | ||
} | ||
|
||
// Uniffi doesn't emit unused types, this is a dummy record to ensure that the custom type | ||
// converters are emitted | ||
#[allow(dead_code)] | ||
#[derive(uniffi::Record)] | ||
struct UniffiConverterDummyRecord { | ||
uuid: Uuid, | ||
date: DateTime, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[bindings.kotlin] | ||
package_name = "com.bitwarden.core" | ||
generate_immutable_records = true | ||
android = true | ||
|
||
[bindings.swift] | ||
ffi_module_name = "BitwardenCoreFFI" | ||
module_name = "BitwardenCore" | ||
generate_immutable_records = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.