1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use super::{FileProvider, FileType};
9 use pos::{RelativePath, RelativePathCtx};
13 pub struct PlainFileProvider {
14 relative_path_ctx: Arc<RelativePathCtx>,
15 store: Arc<dyn Store<RelativePath, FileType>>,
18 impl PlainFileProvider {
20 relative_path_ctx: Arc<RelativePathCtx>,
21 store: Arc<dyn Store<RelativePath, FileType>>,
29 pub fn with_no_cache(relative_path_ctx: Arc<RelativePathCtx>) -> Self {
32 store: Arc::new(datastore::EmptyStore),
36 fn read_file_contents_from_disk(&self, file: RelativePath) -> Result<bstr::BString> {
37 let absolute_path = file.to_absolute(&self.relative_path_ctx);
38 Ok((std::fs::read(&absolute_path))?.into())
42 impl FileProvider for PlainFileProvider {
43 fn get(&self, file: RelativePath) -> Result<Option<FileType>> {
47 fn get_contents(&self, file: RelativePath) -> Result<bstr::BString> {
48 match self.get(file)? {
49 Some(FileType::Ide(bytes)) => Ok(bytes),
50 Some(FileType::Disk(bytes)) => Ok(bytes),
51 None => self.read_file_contents_from_disk(file),
55 fn provide_file_for_tests(&self, file: RelativePath, contents: bstr::BString) -> Result<()> {
56 self.store.insert(file, FileType::Disk(contents))
59 fn provide_file_for_ide(&self, file: RelativePath, contents: bstr::BString) -> Result<()> {
60 self.store.insert(file, FileType::Ide(contents))
63 fn provide_file_hint(&self, file: RelativePath, file_type: FileType) -> Result<()> {
64 if let FileType::Ide(_) = file_type {
65 self.store.insert(file, file_type)?;
70 fn remove_batch(&self, files: &std::collections::BTreeSet<RelativePath>) -> Result<()> {
71 self.store.remove_batch(&mut files.iter().copied())