Skip to content

Commit

Permalink
update async
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 14, 2023
1 parent 54e73dc commit d04df59
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
20 changes: 16 additions & 4 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,29 @@ impl SolFilesCache {
#[cfg(feature = "async")]
impl SolFilesCache {
pub async fn async_read(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let content =
tokio::fs::read_to_string(path).await.map_err(|err| SolcError::io(err, path))?;
Ok(serde_json::from_str(&content)?)
let path = path.as_ref().to_owned();
Self::asyncify(move || Self::read(path)).await
}

pub async fn async_write(&self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
let content = serde_json::to_vec(self)?;
tokio::fs::write(path, content).await.map_err(|err| SolcError::io(err, path))
}

async fn asyncify<F, T>(f: F) -> Result<T>
where
F: FnOnce() -> Result<T> + Send + 'static,
T: Send + 'static,
{
match tokio::task::spawn_blocking(f).await {
Ok(res) => res,
Err(_) => Err(SolcError::io(
std::io::Error::new(std::io::ErrorKind::Other, "background task failed"),
"",
)),
}
}
}

impl Default for SolFilesCache {
Expand Down
20 changes: 10 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pub type Result<T> = std::result::Result<T, SolcError>;
#[derive(Debug, Error)]
pub enum SolcError {
/// Errors related to the Solc executable itself.
#[error("Solc exited with {0}\n{1}")]
#[error("solc exited with {0}\n{1}")]
SolcError(std::process::ExitStatus, String),
#[error("Missing pragma from solidity file")]
#[error("missing pragma from Solidity file")]
PragmaNotFound,
#[error("Could not find solc version locally or upstream")]
#[error("could not find Solc version locally or upstream")]
VersionNotFound,
#[error("Checksum mismatch for {file}: expected {expected} found {detected} for {version}")]
#[error("checksum mismatch for {file}: expected {expected} found {detected} for {version}")]
ChecksumMismatch { version: Version, expected: String, detected: String, file: PathBuf },
#[error("Checksum not found for {version}")]
#[error("checksum not found for {version}")]
ChecksumNotFound { version: Version },
#[error(transparent)]
SemverError(#[from] semver::Error),
Expand All @@ -29,12 +29,12 @@ pub enum SolcError {
/// Filesystem IO error
#[error(transparent)]
Io(#[from] SolcIoError),
#[error("File could not be resolved due to broken symlink: {0}.")]
#[error("file could not be resolved due to broken symlink: {0}")]
ResolveBadSymlink(SolcIoError),
/// Failed to resolve a file
#[error("Failed to resolve file: {0}.\n Check configured remappings.")]
#[error("failed to resolve file: {0}; check configured remappings")]
Resolve(SolcIoError),
#[error("File cannot be resolved due to mismatch of file name case: {error}.\n Found existing file: {existing_file:?}\n Please check the case of the import.")]
#[error("file cannot be resolved due to mismatch of file name case: {error}.\nFound existing file: {existing_file:?}\nPlease check the case of the import.")]
ResolveCaseSensitiveFileName { error: SolcIoError, existing_file: PathBuf },
#[error(
r#"{0}.
Expand All @@ -45,15 +45,15 @@ pub enum SolcError {
#[cfg(all(feature = "svm-solc", not(target_arch = "wasm32")))]
#[error(transparent)]
SvmError(#[from] svm::SolcVmError),
#[error("No contracts found at \"{0}\"")]
#[error("no contracts found at \"{0}\"")]
NoContracts(String),
#[error(transparent)]
PatternError(#[from] glob::PatternError),
/// General purpose message.
#[error("{0}")]
Message(String),

#[error("No artifact found for `{}:{}`", .0.display(), .1)]
#[error("no artifact found for `{}:{}`", .0.display(), .1)]
ArtifactNotFound(PathBuf, String),

#[cfg(feature = "project-util")]
Expand Down

0 comments on commit d04df59

Please sign in to comment.