diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/kvstore/src/owned_value.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/kvstore/src/owned_value.rs')
-rw-r--r-- | toolkit/components/kvstore/src/owned_value.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/toolkit/components/kvstore/src/owned_value.rs b/toolkit/components/kvstore/src/owned_value.rs new file mode 100644 index 0000000000..a22abcb8da --- /dev/null +++ b/toolkit/components/kvstore/src/owned_value.rs @@ -0,0 +1,72 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use error::KeyValueError; +use nsstring::{nsCString, nsString}; +use rkv::OwnedValue; +use std::convert::TryInto; +use storage_variant::{DataType, NsIVariantExt, VariantType}; +use xpcom::{interfaces::nsIVariant, RefPtr}; + +pub fn owned_to_variant(owned: OwnedValue) -> Result<RefPtr<nsIVariant>, KeyValueError> { + match owned { + OwnedValue::Bool(val) => Ok(val.into_variant()), + OwnedValue::I64(val) => Ok(val.into_variant()), + OwnedValue::F64(val) => Ok(val.into_variant()), + OwnedValue::Str(ref val) => Ok(nsString::from(val).into_variant()), + + // kvstore doesn't (yet?) support these types of OwnedValue, + // and we should never encounter them, but we need to exhaust + // all possible variants of the OwnedValue enum. + OwnedValue::Instant(_) => Err(KeyValueError::UnsupportedOwned), + OwnedValue::Json(_) => Err(KeyValueError::UnsupportedOwned), + OwnedValue::U64(_) => Err(KeyValueError::UnsupportedOwned), + OwnedValue::Uuid(_) => Err(KeyValueError::UnsupportedOwned), + OwnedValue::Blob(_) => Err(KeyValueError::UnsupportedOwned), + } +} + +pub fn variant_to_owned(variant: &nsIVariant) -> Result<Option<OwnedValue>, KeyValueError> { + let data_type = variant.get_data_type(); + + match data_type.try_into() { + Ok(DataType::Int32) => { + let mut val: i32 = 0; + unsafe { variant.GetAsInt32(&mut val) }.to_result()?; + Ok(Some(OwnedValue::I64(val.into()))) + } + Ok(DataType::Int64) => { + let mut val: i64 = 0; + unsafe { variant.GetAsInt64(&mut val) }.to_result()?; + Ok(Some(OwnedValue::I64(val))) + } + Ok(DataType::Double) => { + let mut val: f64 = 0.0; + unsafe { variant.GetAsDouble(&mut val) }.to_result()?; + Ok(Some(OwnedValue::F64(val))) + } + Ok(DataType::CString) + | Ok(DataType::CharStr) + | Ok(DataType::StringSizeIs) + | Ok(DataType::Utf8String) => { + let mut val: nsCString = nsCString::new(); + unsafe { variant.GetAsAUTF8String(&mut *val) }.to_result()?; + let s = std::str::from_utf8(&*val)?; + Ok(Some(OwnedValue::Str(s.into()))) + } + Ok(DataType::AString) | Ok(DataType::WCharStr) | Ok(DataType::WStringSizeIs) => { + let mut val: nsString = nsString::new(); + unsafe { variant.GetAsAString(&mut *val) }.to_result()?; + let str = String::from_utf16(&val)?; + Ok(Some(OwnedValue::Str(str))) + } + Ok(DataType::Bool) => { + let mut val: bool = false; + unsafe { variant.GetAsBool(&mut val) }.to_result()?; + Ok(Some(OwnedValue::Bool(val))) + } + Ok(DataType::Void) | Ok(DataType::EmptyArray) | Ok(DataType::Empty) => Ok(None), + Err(_) => Err(KeyValueError::UnsupportedVariant(data_type)), + } +} |