use anyhow::{Context, Result};
use nbformat::v4::Notebook;
use std::fs;
use std::path::Path;
pub fn read_notebook(path: impl AsRef<Path>) -> Result<Notebook> {
let content = fs::read_to_string(&path).context("Failed to read notebook file")?;
let notebook = nbformat::parse_notebook(&content).context("Failed to parse notebook")?;
match notebook {
nbformat::Notebook::V4(nb) => Ok(nb),
_ => anyhow::bail!("Only nbformat v4 notebooks are supported"),
}
}
pub fn write_notebook(path: impl AsRef<Path>, notebook: &Notebook) -> Result<()> {
let notebook_enum = nbformat::Notebook::V4(notebook.clone());
let content = serialize_notebook(¬ebook_enum)?;
fs::write(path, content)?;
Ok(())
}
pub fn write_notebook_atomic(path: impl AsRef<Path>, notebook: &Notebook) -> Result<()> {
let path = path.as_ref();
let temp_path = path.with_extension("ipynb.tmp");
write_notebook(&temp_path, notebook).context("Failed to write to temporary file")?;
fs::rename(&temp_path, path).context("Failed to rename temporary file to target")?;
Ok(())
}
fn serialize_notebook(notebook: &nbformat::Notebook) -> Result<String> {
nbformat::serialize_notebook(notebook).context("Failed to serialize notebook")
}