use crate::data::records::{EncryptedRecord, Record};
use crate::data::wasm::simple::{
WASMAttribute, WASMEncryptedAttribute, WASMEncryptedPseudonym, WASMPseudonym,
};
use wasm_bindgen::prelude::*;
#[cfg(feature = "long")]
use crate::data::records::{LongEncryptedRecord, LongRecord, LongRecordStructure};
#[cfg(feature = "long")]
use crate::data::wasm::long::{
WASMLongAttribute, WASMLongEncryptedAttribute, WASMLongEncryptedPseudonym, WASMLongPseudonym,
};
#[wasm_bindgen(js_name = Record)]
pub struct WASMRecord {
pseudonyms: Vec<WASMPseudonym>,
attributes: Vec<WASMAttribute>,
}
#[wasm_bindgen(js_class = Record)]
impl WASMRecord {
#[wasm_bindgen(constructor)]
pub fn new(pseudonyms: Vec<WASMPseudonym>, attributes: Vec<WASMAttribute>) -> Self {
WASMRecord {
pseudonyms,
attributes,
}
}
#[wasm_bindgen(getter)]
pub fn pseudonyms(&self) -> Vec<WASMPseudonym> {
self.pseudonyms.clone()
}
#[wasm_bindgen(getter)]
pub fn attributes(&self) -> Vec<WASMAttribute> {
self.attributes.clone()
}
}
impl From<WASMRecord> for Record {
fn from(record: WASMRecord) -> Self {
Record::new(
record.pseudonyms.into_iter().map(|p| p.0).collect(),
record.attributes.into_iter().map(|a| a.0).collect(),
)
}
}
impl From<Record> for WASMRecord {
fn from(record: Record) -> Self {
WASMRecord {
pseudonyms: record.pseudonyms.into_iter().map(WASMPseudonym).collect(),
attributes: record.attributes.into_iter().map(WASMAttribute).collect(),
}
}
}
#[wasm_bindgen(js_name = RecordEncrypted)]
pub struct WASMRecordEncrypted {
pseudonyms: Vec<WASMEncryptedPseudonym>,
attributes: Vec<WASMEncryptedAttribute>,
}
#[wasm_bindgen(js_class = RecordEncrypted)]
impl WASMRecordEncrypted {
#[wasm_bindgen(constructor)]
pub fn new(
pseudonyms: Vec<WASMEncryptedPseudonym>,
attributes: Vec<WASMEncryptedAttribute>,
) -> Self {
WASMRecordEncrypted {
pseudonyms,
attributes,
}
}
#[wasm_bindgen(getter)]
pub fn pseudonyms(&self) -> Vec<WASMEncryptedPseudonym> {
self.pseudonyms.clone()
}
#[wasm_bindgen(getter)]
pub fn attributes(&self) -> Vec<WASMEncryptedAttribute> {
self.attributes.clone()
}
}
impl From<EncryptedRecord> for WASMRecordEncrypted {
fn from(record: EncryptedRecord) -> Self {
WASMRecordEncrypted {
pseudonyms: record
.pseudonyms
.into_iter()
.map(WASMEncryptedPseudonym)
.collect(),
attributes: record
.attributes
.into_iter()
.map(WASMEncryptedAttribute)
.collect(),
}
}
}
impl From<WASMRecordEncrypted> for EncryptedRecord {
fn from(record: WASMRecordEncrypted) -> Self {
EncryptedRecord::new(
record.pseudonyms.into_iter().map(|p| p.0).collect(),
record.attributes.into_iter().map(|a| a.0).collect(),
)
}
}
#[cfg(feature = "long")]
#[wasm_bindgen(js_name = LongRecord)]
pub struct WASMLongRecord {
pseudonyms: Vec<WASMLongPseudonym>,
attributes: Vec<WASMLongAttribute>,
}
#[cfg(feature = "long")]
#[wasm_bindgen(js_class = LongRecord)]
impl WASMLongRecord {
#[wasm_bindgen(constructor)]
pub fn new(pseudonyms: Vec<WASMLongPseudonym>, attributes: Vec<WASMLongAttribute>) -> Self {
WASMLongRecord {
pseudonyms,
attributes,
}
}
#[wasm_bindgen(getter)]
pub fn pseudonyms(&self) -> Vec<WASMLongPseudonym> {
self.pseudonyms.clone()
}
#[wasm_bindgen(getter)]
pub fn attributes(&self) -> Vec<WASMLongAttribute> {
self.attributes.clone()
}
#[wasm_bindgen]
pub fn structure(&self) -> WASMLongRecordStructure {
let rust_record: LongRecord = self.into();
WASMLongRecordStructure(rust_record.structure())
}
#[wasm_bindgen(js_name = padTo)]
pub fn pad_to(&self, structure: &WASMLongRecordStructure) -> Result<WASMLongRecord, JsValue> {
let rust_record: LongRecord = self.into();
rust_record
.pad_to(&structure.0)
.map(WASMLongRecord::from)
.map_err(|e| JsValue::from_str(&format!("Padding failed: {e}")))
}
}
#[cfg(feature = "long")]
impl From<WASMLongRecord> for LongRecord {
fn from(record: WASMLongRecord) -> Self {
LongRecord::new(
record.pseudonyms.into_iter().map(|p| p.0).collect(),
record.attributes.into_iter().map(|a| a.0).collect(),
)
}
}
#[cfg(feature = "long")]
impl From<&WASMLongRecord> for LongRecord {
fn from(record: &WASMLongRecord) -> Self {
LongRecord::new(
record.pseudonyms.iter().map(|p| p.0.clone()).collect(),
record.attributes.iter().map(|a| a.0.clone()).collect(),
)
}
}
#[cfg(feature = "long")]
impl From<LongRecord> for WASMLongRecord {
fn from(record: LongRecord) -> Self {
WASMLongRecord {
pseudonyms: record
.pseudonyms
.into_iter()
.map(WASMLongPseudonym)
.collect(),
attributes: record
.attributes
.into_iter()
.map(WASMLongAttribute)
.collect(),
}
}
}
#[cfg(feature = "long")]
#[wasm_bindgen(js_name = LongRecordEncrypted)]
pub struct WASMLongRecordEncrypted {
pseudonyms: Vec<WASMLongEncryptedPseudonym>,
attributes: Vec<WASMLongEncryptedAttribute>,
}
#[cfg(feature = "long")]
#[wasm_bindgen(js_class = LongRecordEncrypted)]
impl WASMLongRecordEncrypted {
#[wasm_bindgen(constructor)]
pub fn new(
pseudonyms: Vec<WASMLongEncryptedPseudonym>,
attributes: Vec<WASMLongEncryptedAttribute>,
) -> Self {
WASMLongRecordEncrypted {
pseudonyms,
attributes,
}
}
#[wasm_bindgen(getter)]
pub fn pseudonyms(&self) -> Vec<WASMLongEncryptedPseudonym> {
self.pseudonyms.clone()
}
#[wasm_bindgen(getter)]
pub fn attributes(&self) -> Vec<WASMLongEncryptedAttribute> {
self.attributes.clone()
}
}
#[cfg(feature = "long")]
impl From<LongEncryptedRecord> for WASMLongRecordEncrypted {
fn from(record: LongEncryptedRecord) -> Self {
WASMLongRecordEncrypted {
pseudonyms: record
.pseudonyms
.into_iter()
.map(WASMLongEncryptedPseudonym)
.collect(),
attributes: record
.attributes
.into_iter()
.map(WASMLongEncryptedAttribute)
.collect(),
}
}
}
#[cfg(feature = "long")]
impl From<WASMLongRecordEncrypted> for LongEncryptedRecord {
fn from(record: WASMLongRecordEncrypted) -> Self {
LongEncryptedRecord::new(
record.pseudonyms.into_iter().map(|p| p.0).collect(),
record.attributes.into_iter().map(|a| a.0).collect(),
)
}
}
#[cfg(feature = "long")]
#[wasm_bindgen(js_name = LongRecordStructure)]
pub struct WASMLongRecordStructure(pub(crate) LongRecordStructure);
#[cfg(feature = "long")]
#[wasm_bindgen(js_class = LongRecordStructure)]
impl WASMLongRecordStructure {
#[wasm_bindgen(constructor)]
pub fn new(pseudonym_blocks: Vec<usize>, attribute_blocks: Vec<usize>) -> Self {
WASMLongRecordStructure(LongRecordStructure {
pseudonym_blocks,
attribute_blocks,
})
}
#[wasm_bindgen(getter, js_name = pseudonymBlocks)]
pub fn pseudonym_blocks(&self) -> Vec<usize> {
self.0.pseudonym_blocks.clone()
}
#[wasm_bindgen(getter, js_name = attributeBlocks)]
pub fn attribute_blocks(&self) -> Vec<usize> {
self.0.attribute_blocks.clone()
}
}