Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
fix: switch to lossy file read
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrus1 committed Jan 31, 2023
1 parent ae0152e commit 7fa32e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Release Notes

## [0.3.2]

### Fixed

- Fixed invalid file reads when the file contains invalid UTF-8 characters.

## [0.3.1]

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use tree_sitter::Parser;
use walkdir::WalkDir;

use crate::{document::Document, environment::Environment};
use crate::{document::Document, environment::Environment, utils::read_to_string_lossy};

#[derive(Clone)]
pub struct Store {
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Store {
if self.documents.contains_key(&uri) {
continue;
}
let text = match fs::read_to_string(uri.to_file_path().unwrap()) {
let text = match read_to_string_lossy(uri.to_file_path().unwrap()) {
Ok(text) => text,
Err(_err) => {
eprintln!("Failed to read file {:?} ", uri.to_file_path().unwrap());
Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fs::File, io::Read, path::PathBuf};

use lsp_types::{Position, Range, TextDocumentContentChangeEvent, Url};

use crate::{line_index::LineIndex, line_index_ext::LineIndexExt};
Expand Down Expand Up @@ -148,3 +150,17 @@ pub fn uri_to_file_name(uri: &Url) -> Option<String> {
Err(_) => None,
}
}

/// Read a file from its path in a lossy way. If the file non UTF-8 characters, they will be replaced
/// by a �.
///
/// # Arguments
///
/// * `path` - [Path][PathBuf] of the file.
pub fn read_to_string_lossy(path: PathBuf) -> anyhow::Result<String> {
let mut file = File::open(path)?;
let mut buf = vec![];
file.read_to_end(&mut buf)?;

Ok(String::from_utf8_lossy(&buf).to_string())
}

0 comments on commit 7fa32e7

Please sign in to comment.