martianbook 0.1.0

Transform ordinary Rust codebases into explainable execution artifacts.
Documentation
use std::time::Instant;

use uuid::Uuid;

use crate::schema::{
    ExecutionNode,
    Status,
};

use crate::session::SESSION;

pub fn capture<F, T>(
    name: &str,
    f: F,
) -> T
where
    F: FnOnce() -> T,
{
    let start = Instant::now();

    let result = f();

    let duration_ms =
        start.elapsed().as_secs_f64() * 1000.0;

    let mut session =
        SESSION.lock().unwrap();

    session.counter += 1;

    let call_order =
        session.counter;

    session.nodes.push(
        ExecutionNode {
            id: format!(
                "fn_{}",
                Uuid::new_v4().simple()
            ),

            name: name.to_string(),

            module: "main".to_string(),

            file: "src/main.rs".to_string(),

            line_start: 0,

            call_order,

            depth: 0,

            duration_ms,

            status: Status::Success,

            text: Some(
                "Captured from Rust adapter.".to_string()
            ),

            source_code: Some(
                format!(
                    "capture(\"{}\", || {{ ... }});",
                    name
                )
            ),

            stdout: vec![
                "loading data".to_string()
            ],

            stderr: vec![],

            parent: None,

            children: vec![],
        }
    );

    result
}