Skip to main content

sol_log_parser/structured_log/
raw.rs

1use crate::{
2    raw_log::{RawDataLog, RawLog, RawProgramLog},
3    Result,
4};
5
6use super::{ComputeUnits, Log2};
7
8/// A Raw Structured Log
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct RawStructuredLog<'a> {
11    pub program_id: &'a str,
12    pub depth: u8,
13    pub result: RawProgramResult<'a>,
14    pub program_logs: Vec<RawProgramLog<'a>>,
15    pub data_logs: Vec<RawDataLog<'a>>,
16    pub return_data: Option<&'a str>,
17    pub compute_log: Option<ComputeUnits>,
18    pub cpi_logs: Vec<RawStructuredLog<'a>>,
19    pub raw_logs: Vec<&'a str>,
20}
21
22impl<'a> RawStructuredLog<'a> {
23    pub fn from_raw_logs(logs: Vec<RawLog<'a>>) -> Result<Vec<Self>> {
24        let log2: Vec<_> = logs.into_iter().map(Log2::from).collect();
25        let structured_log = helper_code::RawStructuredLogHelper::from_logs(log2)?;
26        Ok(structured_log.into_iter().map(Self::from).collect())
27    }
28}
29
30/// A Raw Program Result
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub enum RawProgramResult<'a> {
33    Success,
34    Err(&'a str),
35}
36
37/* *************************************************************************** *
38 *  HELPER CODE
39 * *************************************************************************** */
40
41mod helper_code {
42    use crate::{
43        raw_log::{RawDataLog, RawProgramLog},
44        structured_log::{ProgramResult, StructuredLog},
45    };
46
47    use super::{RawProgramResult, RawStructuredLog};
48
49    impl<'a> From<RawStructuredLogHelper<'a>> for RawStructuredLog<'a> {
50        fn from(value: RawStructuredLogHelper<'a>) -> Self {
51            Self {
52                program_id: value.program_id,
53                depth: value.depth,
54                result: match value.result {
55                    ProgramResult::Success => RawProgramResult::Success,
56                    ProgramResult::Err(err) => RawProgramResult::Err(err),
57                },
58                program_logs: value.program_logs,
59                data_logs: value.data_logs,
60                return_data: value.return_data,
61                compute_log: value.compute_log,
62                cpi_logs: value.cpi_logs.into_iter().map(Self::from).collect(),
63                raw_logs: value.raw_logs,
64            }
65        }
66    }
67
68    pub type RawStructuredLogHelper<'a> = StructuredLog<
69        &'a str,
70        ProgramResult<&'a str>,
71        RawProgramLog<'a>,
72        RawDataLog<'a>,
73        &'a str,
74        &'a str,
75    >;
76}