Skip to main content

haematite_cli/install/
types.rs

1use std::collections::BTreeMap;
2use std::fmt;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug)]
7pub struct Error {
8    pub code: &'static str,
9    pub message: String,
10    pub partial: Option<Partial>,
11    pub stale_stages: Vec<String>,
12}
13
14impl Error {
15    pub(super) fn new(code: &'static str, message: &(impl ToString + ?Sized)) -> Self {
16        Self {
17            code,
18            message: message.to_string(),
19            partial: None,
20            stale_stages: Vec::new(),
21        }
22    }
23
24    pub(super) fn with_partial(mut self, completed: &[String], published: bool) -> Self {
25        if !completed.is_empty() {
26            self.partial = Some(Partial {
27                name: "bundle_partial",
28                completed: completed.to_vec(),
29                published,
30            });
31        }
32        self
33    }
34
35    pub(super) fn with_stale_stages(mut self, stages: Vec<String>) -> Self {
36        self.stale_stages = stages;
37        self
38    }
39
40    pub(super) fn with_cleanup(mut self, error: &Self) -> Self {
41        self.message.push_str("; ");
42        self.message.push_str(&error.to_string());
43        self
44    }
45}
46
47#[derive(Debug)]
48pub struct Partial {
49    pub name: &'static str,
50    pub completed: Vec<String>,
51    pub published: bool,
52}
53
54impl fmt::Display for Error {
55    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(formatter, "{}: {}", self.code, self.message)?;
57        if let Some(partial) = &self.partial {
58            write!(
59                formatter,
60                "; {}: completed={:?}; published={}",
61                partial.name, partial.completed, partial.published
62            )?;
63        }
64        if !self.stale_stages.is_empty() {
65            write!(formatter, "; stale_stages={:?}", self.stale_stages)?;
66        }
67        Ok(())
68    }
69}
70
71impl std::error::Error for Error {}
72
73#[derive(Debug, Serialize)]
74pub struct Report {
75    pub directory: String,
76    pub files: usize,
77    pub already_installed: bool,
78    pub stale_stages: Vec<String>,
79}
80
81#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
82#[serde(deny_unknown_fields)]
83pub(super) struct Manifest {
84    pub format: u32,
85    pub files: BTreeMap<String, Entry>,
86}
87
88#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
89#[serde(deny_unknown_fields)]
90pub(super) struct Entry {
91    pub bytes: u64,
92    pub sha256: String,
93    pub mode: u32,
94}