Expand description
File format operations for reading and writing structured data files.
This module provides convenience functions for working with common file formats:
- Plain text files
- JSON (with pretty printing option)
- TOML (always pretty printed)
- YAML
All write operations use atomic writes via super::atomic::safe_write to ensure
data integrity.
§Examples
use agpm_cli::utils::fs::formats::{read_json_file, write_json_file};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Serialize, Deserialize)]
struct Config {
name: String,
version: String,
}
let config = Config {
name: "agpm".to_string(),
version: "1.0.0".to_string(),
};
// Write with pretty formatting
write_json_file(Path::new("config.json"), &config, true)?;
// Read back
let loaded: Config = read_json_file(Path::new("config.json"))?;Functions§
- create_
temp_ file - Creates a temporary file with content for testing.
- read_
json_ file - Reads and parses a JSON file.
- read_
text_ file - Reads a text file with proper error handling and context.
- read_
text_ file_ with_ retry - Reads a text file asynchronously with retry for filesystem coherency delays.
- read_
toml_ file - Reads and parses a TOML file.
- read_
yaml_ file - Reads and parses a YAML file.
- write_
json_ file - Writes data as JSON to a file atomically.
- write_
text_ file - Writes a text file atomically with proper error handling.
- write_
toml_ file - Writes data as TOML to a file atomically.
- write_
yaml_ file - Writes data as YAML to a file atomically.