use ciborium::value::Value;
use serde::{Deserialize, Serialize};
use crate::bundle::{self, BundleParams};
use crate::cbor;
use crate::container::BundleKind;
use crate::error::{FcbError, Result};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Student {
pub id: String,
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Submission {
pub case_id: String,
pub bundle_hash: String,
pub student: Student,
pub notes: Vec<Value>,
pub report: Value,
pub activity: Vec<Value>,
pub exported_at: String,
}
pub fn pack_submission(work: &Submission, passphrase: &str) -> Result<Vec<u8>> {
let payload = cbor::encode(work)?;
let params = BundleParams::new(
BundleKind::Work,
work.case_id.clone(),
work.bundle_hash.clone(),
Value::Map(vec![]),
);
bundle::pack_bytes(¶ms, &payload, passphrase)
}
pub fn open_submission(bytes: &[u8], passphrase: &str) -> Result<Submission> {
let (kind, _header, payload) = bundle::open_bytes(bytes, passphrase)?;
if kind != BundleKind::Work {
return Err(FcbError::Malformed("not a .casework (KIND != work)".into()));
}
cbor::decode(&payload)
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> Submission {
Submission {
case_id: "acme-ir-2026-03".into(),
bundle_hash: "sha256:9f2c".into(),
student: Student {
id: "s1234567".into(),
name: "Lin".into(),
},
notes: vec![Value::Text("pinned auth.log line 42".into())],
report: Value::Text("freeform report body".into()),
activity: vec![Value::Text("search: failed login".into())],
exported_at: "2026-06-20T10:00:00Z".into(),
}
}
#[test]
fn submission_round_trips() {
let work = sample();
let bytes = pack_submission(&work, "passw0rd").unwrap();
let back = open_submission(&bytes, "passw0rd").unwrap();
assert_eq!(back, work);
}
#[test]
fn opening_a_case_as_submission_is_rejected() {
let params = BundleParams::new(BundleKind::Case, "c", "h", Value::Map(vec![]));
let mut params = params;
params.m_cost = 32;
params.t_cost = 1;
params.p_cost = 1;
let case_bytes = bundle::pack_bytes(¶ms, b"evidence", "passw0rd").unwrap();
assert!(matches!(
open_submission(&case_bytes, "passw0rd"),
Err(FcbError::Malformed(_))
));
}
}