Skip to main content

munin_msbuild/
context.rs

1// Copyright (c) Michael Grier
2
3//! `BuildEventContext` — seven fields identifying the execution context of a
4//! build event (node, project, target, task, submission, project instance,
5//! and evaluation).
6
7use std::io::{Read, Write};
8
9use serde::{Deserialize, Serialize};
10
11use crate::{error::MuninError, primitives::read_7bit_int, writers::write_7bit_int};
12
13/// Sentinel value used when an evaluation ID is not available (format version ≤ 1).
14const INVALID_EVALUATION_ID: i32 = -1;
15
16/// Execution context attached to a build event.
17///
18/// All seven fields are 7-bit variable-length encoded `i32` values. The
19/// `evaluation_id` field was introduced in format version 2; for earlier
20/// versions it defaults to [`INVALID_EVALUATION_ID`] (-1).
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
22pub struct BuildEventContext {
23    pub node_id: i32,
24    pub project_context_id: i32,
25    pub target_id: i32,
26    pub task_id: i32,
27    pub submission_id: i32,
28    pub project_instance_id: i32,
29    pub evaluation_id: i32,
30}
31
32/// Read a `BuildEventContext` from the stream.
33///
34/// The `file_format_version` controls whether the `evaluation_id` field is
35/// present (version > 1) or defaults to -1.
36pub fn read_build_event_context(
37    reader: &mut impl Read,
38    file_format_version: i32,
39) -> Result<BuildEventContext, MuninError> {
40    let node_id = read_7bit_int(reader)?;
41    let project_context_id = read_7bit_int(reader)?;
42    let target_id = read_7bit_int(reader)?;
43    let task_id = read_7bit_int(reader)?;
44    let submission_id = read_7bit_int(reader)?;
45    let project_instance_id = read_7bit_int(reader)?;
46    let evaluation_id = if file_format_version > 1 {
47        read_7bit_int(reader)?
48    } else {
49        INVALID_EVALUATION_ID
50    };
51    Ok(BuildEventContext {
52        node_id,
53        project_context_id,
54        target_id,
55        task_id,
56        submission_id,
57        project_instance_id,
58        evaluation_id,
59    })
60}
61
62/// Write a `BuildEventContext` to the stream. Inverse of
63/// [`read_build_event_context`].
64pub fn write_build_event_context<W: Write>(
65    w: &mut W,
66    ctx: &BuildEventContext,
67    file_format_version: i32,
68) -> std::io::Result<()> {
69    write_7bit_int(w, ctx.node_id)?;
70    write_7bit_int(w, ctx.project_context_id)?;
71    write_7bit_int(w, ctx.target_id)?;
72    write_7bit_int(w, ctx.task_id)?;
73    write_7bit_int(w, ctx.submission_id)?;
74    write_7bit_int(w, ctx.project_instance_id)?;
75    if file_format_version > 1 {
76        write_7bit_int(w, ctx.evaluation_id)?;
77    }
78    Ok(())
79}
80
81#[cfg(test)]
82mod tests;