Skip to content

Commit

Permalink
refactor: use Strings for error variants' data
Browse files Browse the repository at this point in the history
  • Loading branch information
cyril-marpaud committed Feb 16, 2024
1 parent 44b8faa commit 23f8ff7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub enum Error {
CargoAdd(String),
ChangeDir,
CreateCargo,
CreateFile(&'static str),
CreateFolder(&'static str),
CreateFile(String),
CreateFolder(String),
ErroneousSoftdevice,
InvalidChip(InvalidChip),
}
Expand Down
14 changes: 7 additions & 7 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Init {
}

fn init_config(&self, target: &Target, chip: &str) -> Result<(), Error> {
fs::create_dir_all(".cargo").map_err(|_| Error::CreateFolder(".cargo"))?;
fs::create_dir_all(".cargo").map_err(|_| Error::CreateFolder(".cargo".into()))?;

self.create_file(
".cargo/config.toml",
Expand Down Expand Up @@ -214,15 +214,15 @@ impl Init {
.read(true)
.append(true)
.open("Cargo.toml")
.map_err(|_| Error::CreateFile("Cargo.toml"))?;
.map_err(|_| Error::CreateFile("Cargo.toml".into()))?;

// really gross patch for cargo version discontinuity
// somewhere between cargo 1.72 and 1.76 the behavior of "cargo add" changed
let mut buf = String::new();
file.read_to_string(&mut buf).unwrap();
if !buf.contains("[features]") {
file.write_all(include_str!("templates/Cargo.toml.feature-patch.template").as_bytes())
.map_err(|_| Error::CreateFile("Cargo.toml"))?;
.map_err(|_| Error::CreateFile("Cargo.toml".into()))?;
}

file.write_all(
Expand All @@ -236,7 +236,7 @@ impl Init {
}
.as_bytes(),
)
.map_err(|_| Error::CreateFile("Cargo.toml"))?;
.map_err(|_| Error::CreateFile("Cargo.toml".into()))?;

Ok(())
}
Expand Down Expand Up @@ -289,18 +289,18 @@ impl Init {
)
}

fn create_file(&self, name: &'static str, content: &str) -> Result<(), Error> {
fn create_file(&self, name: &str, content: &str) -> Result<(), Error> {
self.pb.set_message(format!("Create file: {name}"));

let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(name)
.map_err(|_| Error::CreateFile(name))?;
.map_err(|_| Error::CreateFile(name.into()))?;

file.write_all(content.as_bytes())
.map_err(|_| Error::CreateFile(name))?;
.map_err(|_| Error::CreateFile(name.into()))?;

Ok(())
}
Expand Down

0 comments on commit 23f8ff7

Please sign in to comment.