use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotebookCell {
pub cell_type: String,
pub source: Vec<String>,
pub metadata: serde_json::Value,
pub outputs: Option<Vec<serde_json::Value>>,
pub execution_count: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notebook {
pub nbformat: u32,
pub nbformat_minor: u32,
pub metadata: NotebookMetadata,
pub cells: Vec<NotebookCell>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotebookMetadata {
pub kernelspec: Option<KernelSpec>,
pub language_info: Option<LanguageInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelSpec {
pub name: String,
pub display_name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageInfo {
pub name: String,
pub version: Option<String>,
}
pub fn is_notebook_file(path: &str) -> bool {
path.ends_with(".ipynb")
}
pub fn parse_notebook(json: &str) -> Result<Notebook, serde_json::Error> {
serde_json::from_str(json)
}
pub fn extract_code_cells(notebook: &Notebook) -> Vec<String> {
notebook
.cells
.iter()
.filter(|c| c.cell_type == "code")
.map(|c| c.source.join(""))
.collect()
}