1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Parsing error messages from the Yocto and other workflows
use crate::{commands::WorkflowKind, err_msg_parse::yocto_err::util::YoctoFailureKind};
use std::error::Error;

use self::yocto_err::YoctoError;

/// Maximum size of a logfile we'll add to the issue body
///
/// The maximum size of a GitHub issue body is 65536
pub const LOGFILE_MAX_LEN: usize = 5000;

pub mod yocto_err;

#[derive(Debug)]
pub enum ErrorMessageSummary {
    Yocto(YoctoError),
    Other(String),
}

impl ErrorMessageSummary {
    pub fn summary(&self) -> &str {
        match self {
            ErrorMessageSummary::Yocto(err) => err.summary(),
            ErrorMessageSummary::Other(o) => o.as_str(),
        }
    }
    pub fn log(&self) -> Option<&str> {
        match self {
            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.contents.as_str()),
            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
        }
    }
    pub fn logfile_name(&self) -> Option<&str> {
        match self {
            ErrorMessageSummary::Yocto(err) => err.logfile().map(|log| log.name.as_str()),
            ErrorMessageSummary::Other(_) => None, // Does not come with a log file
        }
    }

    pub fn failure_label(&self) -> Option<String> {
        match self {
            ErrorMessageSummary::Yocto(err) => Some(err.kind().to_string()),
            ErrorMessageSummary::Other(_) => None,
        }
    }
}

pub fn parse_error_message(
    err_msg: &str,
    workflow: WorkflowKind,
) -> Result<ErrorMessageSummary, Box<dyn Error>> {
    let err_msg = match workflow {
        WorkflowKind::Yocto => {
            ErrorMessageSummary::Yocto(yocto_err::parse_yocto_error(err_msg).unwrap_or_else(|e| {
                log::warn!("Failed to parse Yocto error: {e}");
                YoctoError::new(err_msg.to_string(), YoctoFailureKind::default(), None)
            }))
        },
        WorkflowKind::Other => ErrorMessageSummary::Other(err_msg.to_string()),
    };
    Ok(err_msg)
}