Skip to content

Commit

Permalink
Factor out Self::read_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Sep 5, 2024
1 parent eda7196 commit b0e0e5e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,8 @@ impl Runner {

fn edit_contract_cargo_file(&self, contract_path: &Path) -> Result<(), Error> {
let cargo_path = contract_path.join("Cargo.toml");
let cargo_toml_str = read_to_string(&cargo_path).map_err(|e| {
Error::Io(
format!("Error reading Cargo.toml file in: {contract_path:?}"),
e,
)
})?;

let cargo_toml_str = Self::read_to_string(&cargo_path)?;
let cargo_toml_str = regex::Regex::new(r#"soroban-sdk = "[^\"]+""#)
.unwrap()
.replace_all(
Expand Down Expand Up @@ -390,7 +385,7 @@ impl Runner {
file_name: &str,
) -> Result<(), Error> {
let file_path = project_path.join(file_name);
let file_contents = read_to_string(&file_path)?;
let file_contents = Self::read_to_string(&file_path)?;

let mut doc: JsonValue = from_str(&file_contents).map_err(|e| {
Error::Json(
Expand Down Expand Up @@ -456,6 +451,10 @@ impl Runner {
fn write(path: &Path, contents: &str) -> Result<(), Error> {
write(path, contents).map_err(|e| Error::Io(format!("Error writing file: {path:?}"), e))
}

fn read_to_string(path: &Path) -> Result<String, Error> {
read_to_string(path).map_err(|e| Error::Io(format!("Error reading file: {path:?}"), e))
}
}

#[cfg(test)]
Expand Down

0 comments on commit b0e0e5e

Please sign in to comment.