-
Notifications
You must be signed in to change notification settings - Fork 302
witx crate: improve public API #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
77eede4
witx tool: improve public api so you don't have to know about interna…
37a6ec3
witx: add render trait, make sure it roundtrips by implementing Eq
a1abdc9
witx crate: IntRepr variants are U8, U16... to reflect syntax as "u8"...
bd75cdf
witx crate: doc comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| use crate::WitxError; | ||
| use std::collections::HashMap; | ||
| use std::fs::{read_to_string, File}; | ||
| use std::io::{BufRead, BufReader, Error, ErrorKind}; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| pub trait WitxIo { | ||
| /// Read the entire file into a String. Used to resolve `use` declarations. | ||
| fn fgets(&self, path: &Path) -> Result<String, WitxError>; | ||
pchickey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Read a line of a file into a String. Used for error reporting. | ||
| fn fget_line(&self, path: &Path, line_num: usize) -> Result<String, WitxError>; | ||
pchickey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Return the canonical (non-symlinked) path of a file. Used to resolve `use` declarations. | ||
| fn canonicalize(&self, path: &Path) -> Result<PathBuf, WitxError>; | ||
| } | ||
|
|
||
| pub struct Filesystem; | ||
|
|
||
| impl WitxIo for Filesystem { | ||
| fn fgets(&self, path: &Path) -> Result<String, WitxError> { | ||
| read_to_string(path).map_err(|e| WitxError::Io(path.to_path_buf(), e)) | ||
| } | ||
| fn fget_line(&self, path: &Path, line_num: usize) -> Result<String, WitxError> { | ||
| let f = File::open(path).map_err(|e| WitxError::Io(path.into(), e))?; | ||
| let buf = BufReader::new(f); | ||
| let l = buf | ||
| .lines() | ||
| .skip(line_num - 1) | ||
| .next() | ||
| .ok_or_else(|| { | ||
| WitxError::Io(path.into(), Error::new(ErrorKind::Other, "Line not found")) | ||
| })? | ||
| .map_err(|e| WitxError::Io(path.into(), e))?; | ||
|
|
||
| Ok(l) | ||
| } | ||
| fn canonicalize(&self, path: &Path) -> Result<PathBuf, WitxError> { | ||
| path.canonicalize() | ||
| .map_err(|e| WitxError::Io(path.to_path_buf(), e)) | ||
| } | ||
| } | ||
|
|
||
| pub struct MockFs { | ||
| map: HashMap<String, String>, | ||
| } | ||
|
|
||
| impl MockFs { | ||
| pub fn new(strings: &[(&str, &str)]) -> Self { | ||
| MockFs { | ||
| map: strings | ||
| .iter() | ||
| .map(|(k, v)| (k.to_string(), v.to_string())) | ||
| .collect(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl WitxIo for MockFs { | ||
| fn fgets(&self, path: &Path) -> Result<String, WitxError> { | ||
| if let Some(entry) = self.map.get(path.to_str().unwrap()) { | ||
| Ok(entry.to_string()) | ||
| } else { | ||
| Err(WitxError::Io( | ||
| path.to_path_buf(), | ||
| Error::new(ErrorKind::Other, "mock fs: file not found"), | ||
| )) | ||
| } | ||
| } | ||
| fn fget_line(&self, path: &Path, line: usize) -> Result<String, WitxError> { | ||
| if let Some(entry) = self.map.get(path.to_str().unwrap()) { | ||
| entry | ||
| .lines() | ||
| .skip(line - 1) | ||
| .next() | ||
| .map(|s| s.to_string()) | ||
| .ok_or_else(|| { | ||
| WitxError::Io( | ||
| path.to_path_buf(), | ||
| Error::new(ErrorKind::Other, "mock fs: file not found"), | ||
| ) | ||
| }) | ||
| } else { | ||
| Err(WitxError::Io( | ||
| path.to_path_buf(), | ||
| Error::new(ErrorKind::Other, "mock fs: file not found"), | ||
| )) | ||
| } | ||
| } | ||
| fn canonicalize(&self, path: &Path) -> Result<PathBuf, WitxError> { | ||
| Ok(PathBuf::from(path)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.