MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / read_source_file

Function read_source_file

src/sync.rs:12–43  ·  view source on GitHub ↗

Read a source file to a UTF-8 string, transparently handling UTF-16 LE/BE (detected via BOM). Returns an IO error only when the file genuinely cannot be read or decoded.

(path: &Path)

Source from the content-addressed store, hash-verified

10/// (detected via BOM). Returns an IO error only when the file genuinely cannot
11/// be read or decoded.
12pub fn read_source_file(path: &Path) -> std::io::Result<String> {
13 let bytes = read_file_bytes(path)?;
14
15 // UTF-16 LE BOM: FF FE
16 if bytes.starts_with(&[0xFF, 0xFE]) {
17 let u16s: Vec<u16> = bytes[2..]
18 .chunks_exact(2)
19 .map(|pair| u16::from_le_bytes([pair[0], pair[1]]))
20 .collect();
21 return String::from_utf16(&u16s)
22 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e));
23 }
24
25 // UTF-16 BE BOM: FE FF
26 if bytes.starts_with(&[0xFE, 0xFF]) {
27 let u16s: Vec<u16> = bytes[2..]
28 .chunks_exact(2)
29 .map(|pair| u16::from_be_bytes([pair[0], pair[1]]))
30 .collect();
31 return String::from_utf16(&u16s)
32 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e));
33 }
34
35 // Strip UTF-8 BOM if present, then validate
36 let start = if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
37 3
38 } else {
39 0
40 };
41 String::from_utf8(bytes[start..].to_vec())
42 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
43}
44
45/// Reads a file's bytes, absorbing transient Windows file locks.
46///

Callers 15

process_requestFunction · 0.85
extract_files_in_processFunction · 0.85
sync_single_filesMethod · 0.85
handle_implementationsFunction · 0.85
collect_method_bodiesFunction · 0.85
source_body_for_nodeFunction · 0.85
handle_todosFunction · 0.85
handle_readFunction · 0.85
handle_unsafe_patternsFunction · 0.85
handle_constructorsFunction · 0.85
handle_field_sitesFunction · 0.85

Calls 2

read_file_bytesFunction · 0.85
collectMethod · 0.80