use relay_protocol::{Annotated, Empty, FromValue, IntoValue, Object, Value};
use uuid::Uuid;
use crate::processor::ProcessValue;
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
pub struct DeviceContext {
#[metastructure(pii = "maybe")]
pub name: Annotated<String>,
pub family: Annotated<String>,
pub model: Annotated<String>,
pub model_id: Annotated<String>,
pub arch: Annotated<String>,
pub battery_level: Annotated<f64>,
pub orientation: Annotated<String>,
pub manufacturer: Annotated<String>,
pub brand: Annotated<String>,
#[metastructure(pii = "maybe")]
pub screen_resolution: Annotated<String>,
#[metastructure(pii = "maybe")]
pub screen_width_pixels: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub screen_height_pixels: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub screen_density: Annotated<f64>,
#[metastructure(pii = "maybe")]
pub screen_dpi: Annotated<u64>,
pub online: Annotated<bool>,
pub charging: Annotated<bool>,
pub low_memory: Annotated<bool>,
pub simulator: Annotated<bool>,
#[metastructure(pii = "maybe")]
pub memory_size: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub free_memory: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub usable_memory: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub storage_size: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub free_storage: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub external_storage_size: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub external_free_storage: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub boot_time: Annotated<String>,
#[metastructure(pii = "maybe")]
pub timezone: Annotated<String>,
#[metastructure(pii = "maybe")]
pub locale: Annotated<String>,
pub processor_count: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub cpu_description: Annotated<String>,
pub processor_frequency: Annotated<u64>,
#[metastructure(pii = "maybe")]
pub device_type: Annotated<String>,
#[metastructure(pii = "maybe")]
pub battery_status: Annotated<String>,
#[metastructure(pii = "true")]
pub device_unique_identifier: Annotated<String>,
pub supports_vibration: Annotated<bool>,
pub supports_accelerometer: Annotated<bool>,
pub supports_gyroscope: Annotated<bool>,
pub supports_audio: Annotated<bool>,
pub supports_location_service: Annotated<bool>,
#[metastructure(pii = "maybe")]
pub uuid: Annotated<Uuid>,
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
}
impl super::DefaultContext for DeviceContext {
fn default_key() -> &'static str {
"device"
}
fn from_context(context: super::Context) -> Option<Self> {
match context {
super::Context::Device(c) => Some(*c),
_ => None,
}
}
fn cast(context: &super::Context) -> Option<&Self> {
match context {
super::Context::Device(c) => Some(c),
_ => None,
}
}
fn cast_mut(context: &mut super::Context) -> Option<&mut Self> {
match context {
super::Context::Device(c) => Some(c),
_ => None,
}
}
fn into_context(self) -> super::Context {
super::Context::Device(Box::new(self))
}
}
#[cfg(test)]
mod tests {
use uuid::uuid;
use super::*;
use crate::protocol::Context;
#[test]
fn test_device_context_roundtrip() {
let json = r#"{
"name": "iphone",
"family": "iphone",
"model": "iphone7,3",
"model_id": "AH223",
"arch": "arm64",
"battery_level": 58.5,
"orientation": "landscape",
"manufacturer": "Apple",
"brand": "iphone",
"screen_resolution": "800x600",
"screen_width_pixels": 1920,
"screen_height_pixels": 1080,
"screen_density": 1.1,
"screen_dpi": 1,
"online": true,
"charging": false,
"low_memory": false,
"simulator": true,
"memory_size": 3137978368,
"free_memory": 322781184,
"usable_memory": 2843525120,
"storage_size": 63989469184,
"free_storage": 31994734592,
"external_storage_size": 2097152,
"external_free_storage": 2097152,
"boot_time": "2018-02-08T12:52:12Z",
"timezone": "Europe/Vienna",
"locale": "de-AT",
"processor_count": 8,
"cpu_description": "Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz",
"processor_frequency": 2400,
"device_type": "Handheld",
"battery_status": "Charging",
"device_unique_identifier": "1234567",
"supports_vibration": true,
"supports_accelerometer": true,
"supports_gyroscope": true,
"supports_audio": true,
"supports_location_service": true,
"uuid": "abadcade-feed-dead-beef-baddadfeeded",
"other": "value",
"type": "device"
}"#;
let context = Annotated::new(Context::Device(Box::new(DeviceContext {
name: Annotated::new("iphone".to_string()),
family: Annotated::new("iphone".to_string()),
model: Annotated::new("iphone7,3".to_string()),
model_id: Annotated::new("AH223".to_string()),
arch: Annotated::new("arm64".to_string()),
battery_level: Annotated::new(58.5),
orientation: Annotated::new("landscape".to_string()),
simulator: Annotated::new(true),
manufacturer: Annotated::new("Apple".to_string()),
brand: Annotated::new("iphone".to_string()),
screen_resolution: Annotated::new("800x600".to_string()),
screen_width_pixels: Annotated::new(1920),
screen_height_pixels: Annotated::new(1080),
screen_density: Annotated::new(1.1),
screen_dpi: Annotated::new(1),
online: Annotated::new(true),
charging: Annotated::new(false),
low_memory: Annotated::new(false),
memory_size: Annotated::new(3_137_978_368),
free_memory: Annotated::new(322_781_184),
usable_memory: Annotated::new(2_843_525_120),
storage_size: Annotated::new(63_989_469_184),
free_storage: Annotated::new(31_994_734_592),
external_storage_size: Annotated::new(2_097_152),
external_free_storage: Annotated::new(2_097_152),
boot_time: Annotated::new("2018-02-08T12:52:12Z".to_string()),
timezone: Annotated::new("Europe/Vienna".to_string()),
locale: Annotated::new("de-AT".to_string()),
processor_count: Annotated::new(8),
cpu_description: Annotated::new(
"Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz".to_string(),
),
processor_frequency: Annotated::new(2400),
device_type: Annotated::new("Handheld".to_string()),
battery_status: Annotated::new("Charging".to_string()),
device_unique_identifier: Annotated::new("1234567".to_string()),
supports_vibration: Annotated::new(true),
supports_accelerometer: Annotated::new(true),
supports_gyroscope: Annotated::new(true),
supports_audio: Annotated::new(true),
supports_location_service: Annotated::new(true),
uuid: Annotated::new(uuid!("abadcade-feed-dead-beef-baddadfeeded")),
other: {
let mut map = Object::new();
map.insert(
"other".to_string(),
Annotated::new(Value::String("value".to_string())),
);
map
},
})));
assert_eq!(context, Annotated::from_json(json).unwrap());
assert_eq!(json, context.to_json_pretty().unwrap());
}
}