Skip to main content

osc7501/
context.rs

1use crate::{AppId, RecordId, State, Status};
2
3/// Borrowed fields to repeat in each report for a record.
4///
5/// [`status`](Self::status) starts a fresh complete replacement with this app,
6/// id, and title. Each report starts without a message, progress, or blocking kind.
7/// Creating a context or a report does not allocate or write output.
8#[derive(Clone, Copy, Debug, Default)]
9pub struct Context<'a> {
10    id: Option<RecordId<'a>>,
11    app: Option<AppId<'a>>,
12    title: Option<&'a str>,
13}
14
15impl<'a> Context<'a> {
16    /// Creates a root context with no app or title.
17    #[must_use]
18    pub const fn new() -> Self {
19        Self {
20            id: None,
21            app: None,
22            title: None,
23        }
24    }
25
26    /// Addresses this validated child record in each report.
27    #[must_use]
28    pub const fn id(mut self, id: RecordId<'a>) -> Self {
29        self.id = Some(id);
30        self
31    }
32
33    /// Repeats this validated application identifier in each report.
34    #[must_use]
35    pub const fn app(mut self, app: AppId<'a>) -> Self {
36        self.app = Some(app);
37        self
38    }
39
40    /// Repeats this single-line label, validated when a report is sent.
41    #[must_use]
42    pub const fn title(mut self, title: &'a str) -> Self {
43        self.title = Some(title);
44        self
45    }
46
47    /// Begins a fresh report with the context's app, id, and title.
48    ///
49    /// The report borrows the original fields independently of this context.
50    /// Its setters can override those fields without changing subsequent reports.
51    /// Validation and output happen in [`Status::write_to`].
52    #[must_use]
53    pub const fn status(&self, state: State) -> Status<'a> {
54        Status {
55            id: self.id,
56            app: self.app,
57            title: self.title,
58            ..Status::new(state)
59        }
60    }
61}