use sealmap_model::{Call, CallKind, Confidence, SymbolId};
pub type Segs = Vec<String>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RawStep {
Call(RawCall),
Branch(Vec<(String, Vec<RawStep>)>),
Loop(String, Vec<RawStep>),
Optional(String, Vec<RawStep>),
Parallel(Vec<(String, Vec<RawStep>)>),
Return(String, u32),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawCall {
pub callee: Callee,
pub label: String,
pub kind: CallKind,
pub awaited: bool,
pub fallible: bool,
pub line: u32,
}
impl RawCall {
pub fn new(callee: Callee, label: String, kind: CallKind, line: u32) -> Self {
Self { callee, label, kind, awaited: false, fallible: false, line }
}
pub fn to_call(&self, target: SymbolId, confidence: Confidence) -> Call {
Call {
target,
label: self.label.clone(),
kind: self.kind,
confidence,
awaited: self.awaited,
fallible: self.fallible,
line: self.line,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Callee {
Path(Segs),
Method {
recv: Recv,
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Recv {
SelfValue,
SelfField(String),
Typed(Vec<Segs>),
Untyped,
Returned(Segs),
Derived(Box<Recv>),
Computed(Box<Recv>),
Unknown,
}
pub fn push_arms(arms: Vec<(String, Vec<RawStep>)>, out: &mut Vec<RawStep>) {
if arms.iter().all(|(_, s)| s.is_empty()) {
return;
}
if arms.len() == 1 {
let (label, steps) = arms.into_iter().next().unwrap_or_default();
out.push(RawStep::Optional(label, steps));
} else {
out.push(RawStep::Branch(arms));
}
}
pub fn last_call_mut(out: &mut [RawStep]) -> Option<&mut RawStep> {
out.iter_mut().rev().find(|s| matches!(s, RawStep::Call(_)))
}
pub fn place_deferred(callee: &str, deferred: Vec<Vec<RawStep>>, looping: &[&str], out: &mut Vec<RawStep>) {
use crate::labels::{DeferredShape, deferred_label, deferred_shape};
for steps in deferred {
let shape = deferred_shape(callee, looping);
let label = deferred_label(callee, shape);
out.push(match shape {
DeferredShape::Parallel => RawStep::Parallel(vec![(label, steps)]),
DeferredShape::Loop => RawStep::Loop(label, steps),
DeferredShape::Optional => RawStep::Optional(label, steps),
});
}
}
#[cfg(test)]
mod tests {
use super::*;
fn call(n: &str) -> RawStep {
RawStep::Call(RawCall::new(Callee::Path(vec![n.into()]), n.into(), CallKind::Function, 1))
}
#[test]
fn push_arms_shapes() {
let mut out = Vec::new();
push_arms(vec![("a".into(), vec![]), (String::new(), vec![])], &mut out);
assert!(out.is_empty());
push_arms(vec![("a".into(), vec![call("f")])], &mut out);
assert!(matches!(&out[0], RawStep::Optional(l, s) if l == "a" && s.len() == 1));
push_arms(vec![("a".into(), vec![call("f")]), (String::new(), vec![])], &mut out);
assert!(matches!(&out[1], RawStep::Branch(arms) if arms.len() == 2));
}
#[test]
fn deferred_bodies_follow_the_callee_shape() {
let mut out = Vec::new();
place_deferred("spawn", vec![vec![call("a")]], &["map"], &mut out);
place_deferred("map", vec![vec![call("b")]], &["map"], &mut out);
place_deferred("with", vec![vec![call("c")]], &["map"], &mut out);
place_deferred("", vec![vec![call("d")]], &["map"], &mut out);
assert!(matches!(&out[0], RawStep::Parallel(arms) if arms[0].0 == "spawned task"));
assert!(matches!(&out[1], RawStep::Loop(l, _) if l == "each via map"));
assert!(matches!(&out[2], RawStep::Optional(l, _) if l == "via with"));
assert!(matches!(&out[3], RawStep::Optional(l, _) if l == "closure"));
}
}