Skip to main content

codoseo_core/
audit.rs

1//! The file `codoseo crawl` saves: the report plus the snapshot to diff against later.
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::report::CrawlReport;
7use crate::snapshot::Snapshot;
8
9/// Bumped when the saved format changes in a way old readers can't handle.
10pub const AUDIT_FORMAT_VERSION: u32 = 1;
11
12#[derive(Debug, thiserror::Error)]
13pub enum AuditError {
14    #[error("not a valid audit file: {0}")]
15    Json(#[from] serde_json::Error),
16    #[error("audit file format version {0} is not supported (expected {AUDIT_FORMAT_VERSION})")]
17    Version(u32),
18}
19
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
21pub struct Audit {
22    pub format_version: u32,
23    pub tool_version: String,
24    /// Unix seconds.
25    pub created_at: u64,
26    pub duration_ms: u64,
27    pub start_url: Url,
28    pub report: CrawlReport,
29    pub snapshot: Snapshot,
30}
31
32impl Audit {
33    /// Parses a saved audit, checking the format version before reading the rest.
34    pub fn from_json(bytes: &[u8]) -> Result<Audit, AuditError> {
35        #[derive(Deserialize)]
36        struct Header {
37            format_version: u32,
38        }
39        let header: Header = serde_json::from_slice(bytes)?;
40        if header.format_version != AUDIT_FORMAT_VERSION {
41            return Err(AuditError::Version(header.format_version));
42        }
43        Ok(serde_json::from_slice(bytes)?)
44    }
45}