Feat: WIP terse code documentation
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
//! JSON-based project file persistence with versioned format.
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -7,9 +9,9 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::project::{Bank, Project};
|
||||
|
||||
const VERSION: u8 = 1;
|
||||
pub const EXTENSION: &str = "cagire";
|
||||
const EXTENSION: &str = "cagire";
|
||||
|
||||
pub fn ensure_extension(path: &Path) -> PathBuf {
|
||||
fn ensure_extension(path: &Path) -> PathBuf {
|
||||
if path.extension().map(|e| e == EXTENSION).unwrap_or(false) {
|
||||
path.to_path_buf()
|
||||
} else {
|
||||
@@ -62,6 +64,7 @@ impl From<ProjectFile> for Project {
|
||||
}
|
||||
}
|
||||
|
||||
/// Error returned by project save/load operations.
|
||||
#[derive(Debug)]
|
||||
pub enum FileError {
|
||||
Io(io::Error),
|
||||
@@ -91,6 +94,7 @@ impl From<serde_json::Error> for FileError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a project to disk as pretty-printed JSON, returning the final path.
|
||||
pub fn save(project: &Project, path: &Path) -> Result<PathBuf, FileError> {
|
||||
let path = ensure_extension(path);
|
||||
let file = ProjectFile::from(project);
|
||||
@@ -99,11 +103,13 @@ pub fn save(project: &Project, path: &Path) -> Result<PathBuf, FileError> {
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Read a project from a `.cagire` file on disk.
|
||||
pub fn load(path: &Path) -> Result<Project, FileError> {
|
||||
let json = fs::read_to_string(path)?;
|
||||
load_str(&json)
|
||||
}
|
||||
|
||||
/// Parse a project from a JSON string.
|
||||
pub fn load_str(json: &str) -> Result<Project, FileError> {
|
||||
let file: ProjectFile = serde_json::from_str(json)?;
|
||||
if file.version > VERSION {
|
||||
|
||||
Reference in New Issue
Block a user