Remove push/pop_local_changes from FileProvider trait
[hiphop-php.git] / hphp / hack / src / rupro / file_provider / provider.rs
blob9589cef5b779d491d9d1ae7242db71e294b6ba70
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
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};
7 use anyhow::Result;
8 use datastore::Store;
9 use pos::{RelativePath, RelativePathCtx};
10 use std::sync::Arc;
12 #[derive(Debug)]
13 pub struct PlainFileProvider {
14     relative_path_ctx: Arc<RelativePathCtx>,
15     store: Arc<dyn Store<RelativePath, FileType>>,
18 impl PlainFileProvider {
19     pub fn new(
20         relative_path_ctx: Arc<RelativePathCtx>,
21         store: Arc<dyn Store<RelativePath, FileType>>,
22     ) -> Self {
23         Self {
24             relative_path_ctx,
25             store,
26         }
27     }
29     pub fn with_no_cache(relative_path_ctx: Arc<RelativePathCtx>) -> Self {
30         Self {
31             relative_path_ctx,
32             store: Arc::new(datastore::EmptyStore),
33         }
34     }
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())
39     }
42 impl FileProvider for PlainFileProvider {
43     fn get(&self, file: RelativePath) -> Result<Option<FileType>> {
44         self.store.get(file)
45     }
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),
52         }
53     }
55     fn provide_file_for_tests(&self, file: RelativePath, contents: bstr::BString) -> Result<()> {
56         self.store.insert(file, FileType::Disk(contents))
57     }
59     fn provide_file_for_ide(&self, file: RelativePath, contents: bstr::BString) -> Result<()> {
60         self.store.insert(file, FileType::Ide(contents))
61     }
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)?;
66         }
67         Ok(())
68     }
70     fn remove_batch(&self, files: &std::collections::BTreeSet<RelativePath>) -> Result<()> {
71         self.store.remove_batch(&mut files.iter().copied())
72     }