jtool-core 0.3.0

core library for jtool
Documentation
use crate::error::CoreError;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;

/// Get the path to the current process's session file
pub fn get_session_file_path() -> Result<PathBuf, CoreError> {
    let pid = process::id();
    let temp_dir = env::temp_dir();
    Ok(temp_dir.join(format!("jtool-session-{pid}.txt")))
}

/// Save the most recent notebook for this terminal session
pub fn save_recent_notebook(notebook_path: &str, server: Option<&str>) -> Result<(), CoreError> {
    let session_file = get_session_file_path()?;

    // Format: notebook_path|server (or just notebook_path if no server)
    let content = if let Some(srv) = server {
        format!("{notebook_path}|{srv}")
    } else {
        notebook_path.to_string()
    };

    fs::write(session_file, content)?;
    Ok(())
}

/// Get the most recent notebook for this terminal session
/// Returns (notebook_path, optional_server)
pub fn get_recent_notebook() -> Result<(String, Option<String>), CoreError> {
    let session_file = get_session_file_path()?;

    if !session_file.exists() {
        return Err(CoreError::Session(
            "No recent notebook in this terminal session".to_string(),
        ));
    }

    let content = fs::read_to_string(session_file)?;

    // Parse: notebook_path|server or just notebook_path
    if let Some((notebook, server)) = content.split_once('|') {
        Ok((notebook.to_string(), Some(server.to_string())))
    } else {
        Ok((content, None))
    }
}