-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(device-loader): Impl for wasm32 using local storage
- Implement device-loader for wasm32 using local storage web API. - Update integration tests to be platform agnostic. - Group all tests as single module (simplify configuring tests for wasm32)
- Loading branch information
1 parent
70bb6d4
commit 6186c21
Showing
19 changed files
with
889 additions
and
104 deletions.
There are no files selected for viewing
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
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
205 changes: 205 additions & 0 deletions
205
libparsec/crates/platform_device_loader/src/web/error.rs
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,205 @@ | ||
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS | ||
|
||
use libparsec_types::{anyhow, thiserror}; | ||
use web_sys::wasm_bindgen::JsValue; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum NewStorageError { | ||
#[error("No window context available, are we in a browser?")] | ||
NoWindow, | ||
#[error("No local storage available")] | ||
NoLocalStorage, | ||
#[error("Failed to access local storage ({:?})", .0)] | ||
WindowError(JsValue), | ||
} | ||
|
||
macro_rules! impl_from_new_storage_error { | ||
($for:ty) => { | ||
impl From<NewStorageError> for $for { | ||
fn from(value: NewStorageError) -> Self { | ||
Self::Internal(anyhow::anyhow!("{value}")) | ||
} | ||
} | ||
}; | ||
($($for:ty),*) => { | ||
$(impl_from_new_storage_error!($for);)* | ||
}; | ||
} | ||
|
||
impl_from_new_storage_error!( | ||
crate::LoadDeviceError, | ||
crate::SaveDeviceError, | ||
crate::ChangeAuthentificationError, | ||
crate::ArchiveDeviceError, | ||
crate::RemoveDeviceError | ||
); | ||
|
||
error_set::error_set! { | ||
GetItemStorageError = { | ||
#[display("Failed to get item {key} from storage ({error:?})")] | ||
GetItemStorage { | ||
key: String, | ||
error: JsValue | ||
} | ||
}; | ||
SetItemStorageError = { | ||
#[display("Failed to set item {key} to storage ({error:?})")] | ||
SetItemStorage { | ||
key: String, | ||
error: JsValue | ||
} | ||
}; | ||
RemoveItemStorageError = { | ||
#[display("Failed to remove item {key} from storage ({error:?})")] | ||
RemoveItemStorage { | ||
key: String, | ||
error: JsValue | ||
} | ||
}; | ||
JsonDeserializationError = { | ||
JsonDecode(serde_json::Error) | ||
}; | ||
JsonSerError = { | ||
JsonEncode(serde_json::Error) | ||
}; | ||
RmpDecodeError = { | ||
RmpDecode(libparsec_types::RmpDecodeError) | ||
}; | ||
Base64DecodeError = { | ||
B64Decode(base64::DecodeError) | ||
}; | ||
ListAvailableDevicesError = GetItemStorageError | ||
|| JsonDeserializationError | ||
|| InvalidPathError | ||
|| LoadAvailableDeviceError; | ||
GetRawDeviceError = GetItemStorageError | ||
|| Base64DecodeError | ||
|| { | ||
#[display("No device for '{key}'")] | ||
Missing { | ||
key: String | ||
}, | ||
}; | ||
LoadAvailableDeviceError = GetRawDeviceError || RmpDecodeError; | ||
InvalidPathError = { | ||
#[display("Invalid path {}", path.display())] | ||
InvalidPath { | ||
path: std::path::PathBuf | ||
} | ||
}; | ||
LoadDeviceError = InvalidPathError | ||
|| GetRawDeviceError | ||
|| RmpDecodeError | ||
|| { | ||
InvalidFileType, | ||
GetSecretKey(libparsec_crypto::CryptoError), | ||
DecryptAndLoad(crate::DecryptDeviceFileError), | ||
}; | ||
SaveDeviceError = SaveDeviceFileError || InvalidPathError; | ||
SaveDeviceFileError = SetItemStorageError || AddItemToListError; | ||
AddItemToListError = GetRawDeviceError | ||
|| SetItemStorageError | ||
|| JsonDeserializationError | ||
|| JsonSerError; | ||
RemoveItemFromListError = GetRawDeviceError | ||
|| SetItemStorageError | ||
|| JsonDeserializationError | ||
|| JsonSerError; | ||
ArchiveDeviceError = GetItemStorageError | ||
|| AddItemToListError | ||
|| RemoveItemFromListError | ||
|| JsonDeserializationError | ||
|| InvalidPathError; | ||
RemoveDeviceError = GetItemStorageError | ||
|| RemoveItemFromListError | ||
|| RemoveItemStorageError | ||
|| JsonDeserializationError | ||
|| InvalidPathError; | ||
} | ||
|
||
impl From<LoadDeviceError> for crate::LoadDeviceError { | ||
fn from(value: LoadDeviceError) -> Self { | ||
match value { | ||
LoadDeviceError::InvalidFileType => Self::InvalidData, | ||
LoadDeviceError::GetSecretKey(_) => Self::DecryptionFailed, | ||
LoadDeviceError::DecryptAndLoad(e) => e.into(), | ||
LoadDeviceError::InvalidPath { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::Missing { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::GetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::RmpDecode(_) => Self::InvalidData, | ||
LoadDeviceError::B64Decode(_) => Self::InvalidData, | ||
} | ||
} | ||
} | ||
|
||
impl From<SaveDeviceError> for crate::SaveDeviceError { | ||
fn from(value: SaveDeviceError) -> Self { | ||
match value { | ||
SaveDeviceError::SetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::Missing { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::InvalidPath { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::GetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::JsonDecode(_) => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::JsonEncode(_) => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::B64Decode(_) => Self::Internal(anyhow::anyhow!("{value}")), | ||
} | ||
} | ||
} | ||
|
||
impl From<LoadDeviceError> for crate::ChangeAuthentificationError { | ||
fn from(value: LoadDeviceError) -> Self { | ||
match value { | ||
LoadDeviceError::InvalidFileType => Self::InvalidData, | ||
LoadDeviceError::GetSecretKey(_) => Self::DecryptionFailed, | ||
LoadDeviceError::DecryptAndLoad(e) => e.into(), | ||
LoadDeviceError::InvalidPath { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::Missing { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::GetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
LoadDeviceError::RmpDecode(_) => Self::InvalidData, | ||
LoadDeviceError::B64Decode(_) => Self::InvalidData, | ||
} | ||
} | ||
} | ||
|
||
impl From<SaveDeviceError> for crate::ChangeAuthentificationError { | ||
fn from(value: SaveDeviceError) -> Self { | ||
match value { | ||
SaveDeviceError::SetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::Missing { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::InvalidPath { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::GetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::JsonDecode(_) => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::JsonEncode(_) => Self::Internal(anyhow::anyhow!("{value}")), | ||
SaveDeviceError::B64Decode(_) => Self::InvalidData, | ||
} | ||
} | ||
} | ||
|
||
impl From<RemoveDeviceError> for crate::ChangeAuthentificationError { | ||
fn from(value: RemoveDeviceError) -> Self { | ||
match value { | ||
RemoveDeviceError::GetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
RemoveDeviceError::Missing { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
RemoveDeviceError::SetItemStorage { .. } => Self::Internal(anyhow::anyhow!("{value}")), | ||
RemoveDeviceError::JsonDecode(_) => Self::InvalidData, | ||
RemoveDeviceError::JsonEncode(_) => Self::InvalidData, | ||
RemoveDeviceError::RemoveItemStorage { .. } => { | ||
Self::Internal(anyhow::anyhow!("{value}")) | ||
} | ||
RemoveDeviceError::InvalidPath { .. } => Self::InvalidPath(anyhow::anyhow!("{value}")), | ||
RemoveDeviceError::B64Decode(_) => Self::InvalidData, | ||
} | ||
} | ||
} | ||
|
||
impl From<RemoveDeviceError> for crate::RemoveDeviceError { | ||
fn from(value: RemoveDeviceError) -> Self { | ||
Self::Internal(anyhow::anyhow!("{value}")) | ||
} | ||
} | ||
|
||
impl From<ArchiveDeviceError> for crate::ArchiveDeviceError { | ||
fn from(value: ArchiveDeviceError) -> Self { | ||
Self::Internal(anyhow::anyhow!("{value}")) | ||
} | ||
} |
Oops, something went wrong.