Skip to main content

read_source_file

Function read_source_file 

Source
pub fn read_source_file(path: &Path) -> Result<FileReadResult, TldrError>
Expand description

Read a source file with encoding detection.

This function:

  1. Reads the file as bytes
  2. Checks for and strips UTF-8 BOM
  3. Detects UTF-16 BOM and reports unsupported
  4. Attempts UTF-8 decoding
  5. Falls back to lossy decoding if needed
  6. Detects binary files (contains null bytes)

§Arguments

  • path - Path to the file to read

§Returns

  • Ok(FileReadResult::Ok(content)) - Valid UTF-8 content
  • Ok(FileReadResult::Lossy { content, warning }) - Lossy decoded content with warning
  • Ok(FileReadResult::Binary) - File is binary
  • Err(TldrError) - IO error