wellfare_core/
companion_sync.rs1use serde::{Deserialize, Serialize};
4
5pub const COMPANION_HEALTH_BUNDLE_SCHEMA: u32 = 1;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8pub struct CompanionCsvFile {
9 pub filename: String,
10 pub csv_content: String,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
15pub struct CompanionHealthBundle {
16 pub schema_version: u32,
17 pub operation_id: String,
18 pub device_id: String,
19 pub captured_at_unix: u32,
20 pub files: Vec<CompanionCsvFile>,
21}
22
23impl CompanionHealthBundle {
24 pub fn new(
26 device_id: impl Into<String>,
27 captured_at_unix: u32,
28 files: Vec<CompanionCsvFile>,
29 ) -> Self {
30 Self {
31 schema_version: COMPANION_HEALTH_BUNDLE_SCHEMA,
32 operation_id: uuid::Uuid::new_v4().to_string(),
33 device_id: device_id.into(),
34 captured_at_unix,
35 files,
36 }
37 }
38
39 pub fn validate(&self) -> Result<(), String> {
41 if self.schema_version != COMPANION_HEALTH_BUNDLE_SCHEMA {
42 return Err(format!(
43 "unsupported schema_version {} (expected {COMPANION_HEALTH_BUNDLE_SCHEMA})",
44 self.schema_version
45 ));
46 }
47 if self.device_id.trim().is_empty() {
48 return Err("device_id is required".into());
49 }
50 if self.files.is_empty() {
51 return Err("bundle must contain at least one CSV file".into());
52 }
53 for file in &self.files {
54 if file.filename.trim().is_empty() {
55 return Err("each file must have a filename".into());
56 }
57 if file.csv_content.trim().is_empty() {
58 return Err(format!("{} has empty CSV content", file.filename));
59 }
60 }
61 Ok(())
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn validates_nonempty_bundle() {
71 let bundle = CompanionHealthBundle::new(
72 "phone-abc",
73 1_700_000_000,
74 vec![CompanionCsvFile {
75 filename: "weight.csv".into(),
76 csv_content: "uuid,start_time,weight\nx,1,70".into(),
77 }],
78 );
79 assert!(bundle.validate().is_ok());
80 }
81
82 #[test]
83 fn rejects_empty_files() {
84 let bundle = CompanionHealthBundle::new("phone", 0, vec![]);
85 assert!(bundle.validate().is_err());
86 }
87}