From d66b06962645d927b4fd884f55c7535e1ef5e495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Thu, 14 Nov 2019 16:32:32 +0100 Subject: [PATCH 1/2] Make FlashAlgorithm saveable --- probe-rs/src/probe/flash/flasher.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/probe-rs/src/probe/flash/flasher.rs b/probe-rs/src/probe/flash/flasher.rs index eee6092c83..a840e731a3 100644 --- a/probe-rs/src/probe/flash/flasher.rs +++ b/probe-rs/src/probe/flash/flasher.rs @@ -101,6 +101,13 @@ impl FlashAlgorithm { pub fn new(definition: &str) -> Result { serde_yaml::from_str(definition) } + + pub fn write_to_file(&self, file_name: impl AsRef) { + use std::io::Write; + let s = serde_yaml::to_string(self).unwrap(); + let mut file = std::fs::File::create(file_name.as_ref()).unwrap(); + file.write_all(s.as_bytes()).unwrap(); + } } #[derive(Debug)] From 432002eaf643e73908e9e1fd6a5e6ead9edebe93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Thu, 14 Nov 2019 16:50:51 +0100 Subject: [PATCH 2/2] Implement @jonas-schievinks improvement hints --- .gitignore | 3 +++ probe-rs/src/probe/flash/flasher.rs | 25 ++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 693699042b..bf433b6e56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /target **/*.rs.bk Cargo.lock + +# Needed for rls +probe-rs/target diff --git a/probe-rs/src/probe/flash/flasher.rs b/probe-rs/src/probe/flash/flasher.rs index a840e731a3..7f9bf51cd2 100644 --- a/probe-rs/src/probe/flash/flasher.rs +++ b/probe-rs/src/probe/flash/flasher.rs @@ -97,16 +97,31 @@ pub struct FlashAlgorithm { pub type AlgorithmParseError = serde_yaml::Error; +pub enum FlashAlgorithmSaveError { + Io(std::io::Error), + Serde(serde_yaml::Error), +} + +impl From for FlashAlgorithmSaveError { + fn from(error: std::io::Error) -> FlashAlgorithmSaveError { + FlashAlgorithmSaveError::Io(error) + } +} + +impl From for FlashAlgorithmSaveError { + fn from(error: serde_yaml::Error) -> FlashAlgorithmSaveError { + FlashAlgorithmSaveError::Serde(error) + } +} + impl FlashAlgorithm { pub fn new(definition: &str) -> Result { serde_yaml::from_str(definition) } - pub fn write_to_file(&self, file_name: impl AsRef) { - use std::io::Write; - let s = serde_yaml::to_string(self).unwrap(); - let mut file = std::fs::File::create(file_name.as_ref()).unwrap(); - file.write_all(s.as_bytes()).unwrap(); + pub fn write_to_file(&self, file_name: impl AsRef) -> Result<(), FlashAlgorithmSaveError> { + let file = std::fs::File::create(file_name.as_ref())?; + serde_yaml::to_writer(file, self).map_err(From::from) } }