Read files lossily, in case they are not utf8

This commit is contained in:
MinusGix 2022-07-25 14:05:31 -05:00
parent 2f5dd9f937
commit e09b438f6d
1 changed files with 20 additions and 3 deletions

View File

@ -4,7 +4,7 @@
use std::ffi::OsString;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::{borrow::Cow, path::Path, time::SystemTime};
use xi_rope::{interval::IntervalBounds, rope::Rope, RopeDelta};
@ -127,11 +127,28 @@ pub fn is_empty(&self) -> bool {
}
pub fn load_file(path: &Path) -> Result<String> {
Ok(fs::read_to_string(path)?)
Ok(read_path_to_string_lossy(path)?)
}
fn load_rope(path: &Path) -> Result<Rope> {
Ok(Rope::from(fs::read_to_string(path)?))
Ok(Rope::from(read_path_to_string_lossy(path)?))
}
pub fn read_path_to_string_lossy<P: AsRef<Path>>(
path: P,
) -> Result<String, std::io::Error> {
let path = path.as_ref();
let mut file = File::open(path)?;
// Read the file in as bytes
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
// Parse the file contents as utf8, replacing non-utf8 data with the
// replacement character
let contents = String::from_utf8_lossy(&buffer);
Ok(contents.to_string())
}
fn language_id_from_path(path: &Path) -> Option<&str> {