use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StackDecline {
Recursion,
IndirectCall,
ExternalCall,
CalleeUnbounded,
UnknownSpMove,
}
impl StackDecline {
pub fn as_str(&self) -> &'static str {
match self {
StackDecline::Recursion => "recursion",
StackDecline::IndirectCall => "call_indirect",
StackDecline::ExternalCall => "external-call",
StackDecline::CalleeUnbounded => "callee-unbounded",
StackDecline::UnknownSpMove => "unknown-sp-move",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StackFrame {
pub name: String,
pub own_bytes: u64,
pub calls: Vec<String>,
pub decline: Option<StackDecline>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StackResult {
Bounded { name: String, bytes: u64 },
Declined { name: String, reason: StackDecline },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StackReport {
pub schema: String,
pub module: String,
pub exports: Vec<StackEntry>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StackEntry {
pub name: String,
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub bytes: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
impl StackReport {
pub fn new(module: impl Into<String>) -> Self {
StackReport {
schema: "synth-stack-v1".to_string(),
module: module.into(),
exports: Vec::new(),
}
}
pub fn sidecar_path(output: &std::path::Path) -> std::path::PathBuf {
let mut s = output.as_os_str().to_os_string();
s.push(".stack.json");
std::path::PathBuf::from(s)
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(self)
}
}
impl From<StackResult> for StackEntry {
fn from(r: StackResult) -> Self {
match r {
StackResult::Bounded { name, bytes } => StackEntry {
name,
status: "bounded".to_string(),
bytes: Some(bytes),
reason: None,
},
StackResult::Declined { name, reason } => StackEntry {
name,
status: "declined".to_string(),
bytes: None,
reason: Some(reason.as_str().to_string()),
},
}
}
}