Skip to main content

read_file_to_string

Function read_file_to_string 

Source
pub fn read_file_to_string(path: &Path, max_size: Option<u64>) -> Result<String>
Expand description

Reads a file’s entire contents into a String with ADR 0004 security checks.

Performs the following validations before reading:

  1. File existence: checks fs::metadata() before opening
  2. File size: rejects files exceeding max_size (default 100 MB)
  3. UTF-8 encoding: on UTF-8 failure, falls back to lossy conversion with a warning

§Arguments

  • path - Path to the file to read
  • max_size - Maximum allowed file size in bytes (defaults to MAX_MANIFEST_SIZE)

§Returns

  • Ok(String) - File contents as UTF-8 string (lossy if non-UTF-8 bytes found)
  • Err - File doesn’t exist, is too large, or cannot be read

§Examples

use std::path::Path;
use provenant::parsers::utils::read_file_to_string;

let content = read_file_to_string(Path::new("path/to/file.txt"), None)?;