forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

steps.rs129 lines, 4.1 KiBHistoryRawDownload
1//! Job steps from the log: `::group::Name` opens a step and `::endgroup::` closes it, the way the
2//! browser's log viewer reads them. Only top-level groups are steps; nested groups add no marks.
3
4/// The most steps kept per job. Later top-level groups are not stored.
5pub const MAX_STEPS: usize = 32;
6/// The longest step name, in characters. Longer names are cut.
7pub const MAX_STEP_NAME_CHARS: usize = 80;
8
9/// A change of step state found in a log line.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum StepMark {
12 /// A top-level group opened. `offset` is the byte position of its line in the log.
13 Start { name: String, offset: u64 },
14 /// The open top-level group closed.
15 End,
16}
17
18/// The marks found in the complete lines of a chunk, and how many bytes of it they used.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct Scan {
21 pub marks: Vec<StepMark>,
22 /// Bytes through the last newline. A partial last line is left for the next scan.
23 pub consumed: usize,
24}
25
26/// Reads the complete lines of `bytes`, which start at log byte `base_offset`. `depth` is the
27/// number of open groups and carries over between scans.
28pub fn scan(bytes: &[u8], base_offset: u64, depth: &mut u32) -> Scan {
29 let mut marks = Vec::new();
30 let mut start = 0;
31 for (i, &byte) in bytes.iter().enumerate() {
32 if byte != b'\n' {
33 continue;
34 }
35 let raw = String::from_utf8_lossy(&bytes[start..i]);
36 let stripped = strip_ansi(&raw);
37 let line = stripped.strip_suffix('\r').unwrap_or(&stripped);
38 if let Some(rest) = line.strip_prefix("::group::") {
39 if *depth == 0 {
40 let trimmed = rest.trim();
41 let name = if trimmed.is_empty() { "group" } else { trimmed };
42 marks.push(StepMark::Start {
43 name: name.chars().take(MAX_STEP_NAME_CHARS).collect(),
44 offset: base_offset + start as u64,
45 });
46 }
47 *depth = depth.saturating_add(1);
48 } else if line.trim_end() == "::endgroup::" {
49 if *depth == 1 {
50 marks.push(StepMark::End);
51 }
52 *depth = depth.saturating_sub(1);
53 }
54 start = i + 1;
55 }
56 Scan {
57 marks,
58 consumed: start,
59 }
60}
61
62/// Removes ANSI CSI sequences: `ESC [`, any parameter bytes, then one final byte.
63fn strip_ansi(line: &str) -> String {
64 let mut out = String::with_capacity(line.len());
65 let mut chars = line.chars().peekable();
66 while let Some(c) = chars.next() {
67 if c == '\u{1b}' && chars.peek() == Some(&'[') {
68 chars.next();
69 for f in chars.by_ref() {
70 if ('\u{40}'..='\u{7e}').contains(&f) {
71 break;
72 }
73 }
74 } else {
75 out.push(c);
76 }
77 }
78 out
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn top_level_groups_become_marks_with_offsets() {
87 let log = b"hi\n::group::Build\nx\n::endgroup::\npartial";
88 let mut depth = 0;
89 let found = scan(log, 100, &mut depth);
90 assert_eq!(found.consumed, log.len() - "partial".len());
91 assert_eq!(
92 found.marks,
93 vec![
94 StepMark::Start {
95 name: "Build".into(),
96 offset: 103
97 },
98 StepMark::End,
99 ]
100 );
101 assert_eq!(depth, 0);
102 }
103
104 #[test]
105 fn nested_groups_add_no_marks() {
106 let mut depth = 0;
107 let found = scan(
108 b"::group::A\n::group::B\n::endgroup::\n::endgroup::\n",
109 0,
110 &mut depth,
111 );
112 assert_eq!(found.marks.len(), 2);
113 assert_eq!(found.marks[1], StepMark::End);
114 assert_eq!(depth, 0);
115 }
116
117 #[test]
118 fn ansi_codes_are_stripped_and_empty_names_default() {
119 let mut depth = 0;
120 let found = scan(b"\x1b[1;32m::group::\x1b[0m \r\n", 0, &mut depth);
121 assert_eq!(
122 found.marks,
123 vec![StepMark::Start {
124 name: "group".into(),
125 offset: 0
126 }]
127 );
128 }
129}