Module formats

Module formats 

Source
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.