dap-server: Add support for non `Elf` binary formats. (#1656)

diff --git a/probe-rs/src/bin/probe-rs/cmd/dap_server/server/configuration.rs b/probe-rs/src/bin/probe-rs/cmd/dap_server/server/configuration.rs
index a6a7277..1da38a4 100644
--- a/probe-rs/src/bin/probe-rs/cmd/dap_server/server/configuration.rs
+++ b/probe-rs/src/bin/probe-rs/cmd/dap_server/server/configuration.rs
@@ -1,5 +1,5 @@
-use crate::cmd::dap_server::DebuggerError;
 use crate::util::rtt;
+use crate::{cmd::dap_server::DebuggerError, FormatOptions};
 use anyhow::{anyhow, Result};
 use probe_rs::{DebugProbeSelector, WireProtocol};
 use serde::Deserialize;
@@ -167,6 +167,10 @@
     /// Restore erased bytes that will not be rewritten from ELF
     #[serde(default)]
     pub(crate) restore_unwritten_bytes: bool,
+
+    /// [`FormatOptions`] to control the flashing operation, depending on the type of binary ( [`probe_rs::flashing::Format`] ) to be flashed.
+    #[serde(default)]
+    pub(crate) format_options: FormatOptions,
 }
 
 /// Configuration options for all core level configuration.
diff --git a/probe-rs/src/bin/probe-rs/cmd/dap_server/server/debugger.rs b/probe-rs/src/bin/probe-rs/cmd/dap_server/server/debugger.rs
index 0129a4a..3cbd9c8 100644
--- a/probe-rs/src/bin/probe-rs/cmd/dap_server/server/debugger.rs
+++ b/probe-rs/src/bin/probe-rs/cmd/dap_server/server/debugger.rs
@@ -20,7 +20,7 @@
 };
 use anyhow::{anyhow, Context};
 use probe_rs::{
-    flashing::{download_file_with_options, DownloadOptions, FlashProgress, Format},
+    flashing::{download_file_with_options, DownloadOptions, FlashProgress},
     Architecture, CoreStatus,
 };
 use std::{
@@ -733,7 +733,11 @@
         let flash_result = download_file_with_options(
             &mut session_data.session,
             path_to_elf,
-            Format::Elf,
+            self.config
+                .flashing_config
+                .format_options
+                .clone()
+                .into_format()?,
             download_options,
         );
 
diff --git a/probe-rs/src/bin/probe-rs/main.rs b/probe-rs/src/bin/probe-rs/main.rs
index 0f80f41..cbdfdb0 100644
--- a/probe-rs/src/bin/probe-rs/main.rs
+++ b/probe-rs/src/bin/probe-rs/main.rs
@@ -4,11 +4,14 @@
 include!(concat!(env!("OUT_DIR"), "/meta.rs"));
 
 use std::path::Path;
+use std::str::FromStr;
 use std::{ffi::OsString, fs::File, path::PathBuf};
 
 use anyhow::{Context, Result};
 use clap::Parser;
 use probe_rs::flashing::{BinOptions, Format, IdfOptions};
+use serde::{de::Error, Deserialize, Deserializer};
+use serde_json::Value;
 use time::{OffsetDateTime, UtcOffset};
 use tracing::metadata::LevelFilter;
 use tracing_subscriber::{
@@ -73,9 +76,22 @@
     core: usize,
 }
 
-#[derive(clap::Parser)]
+/// A helper function to deserialize a default [`Format`] from a string.
+fn format_from_str<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Format, D::Error> {
+    match Value::deserialize(deserializer)? {
+        Value::String(s) => match Format::from_str(s.as_str()) {
+            Ok(format) => Ok(format),
+            Err(e) => Err(D::Error::custom(e)),
+        },
+        _ => Err(D::Error::custom("invalid format")),
+    }
+}
+
+#[derive(clap::Parser, Clone, Deserialize, Debug, Default)]
+#[serde(default)]
 pub(crate) struct FormatOptions {
     #[clap(value_enum, ignore_case = true, default_value = "elf", long)]
+    #[serde(deserialize_with = "format_from_str")]
     format: Format,
     /// The address in memory where the binary will be put at.
     #[clap(long)]
diff --git a/probe-rs/src/flashing/download.rs b/probe-rs/src/flashing/download.rs
index 0d1520b..2eaa8d1 100644
--- a/probe-rs/src/flashing/download.rs
+++ b/probe-rs/src/flashing/download.rs
@@ -28,7 +28,7 @@
 }
 
 /// A finite list of all the available binary formats probe-rs understands.
-#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
+#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)]
 pub enum Format {
     /// Marks a file in binary format. This means that the file contains the contents of the flash 1:1.
     /// [BinOptions] can be used to define the location in flash where the file contents should be put at.
@@ -37,6 +37,7 @@
     /// Marks a file in [Intel HEX](https://en.wikipedia.org/wiki/Intel_HEX) format.
     Hex,
     /// Marks a file in the [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) format.
+    #[default]
     Elf,
     /// Marks a file in the [ESP-IDF bootloader](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/app_image_format.html#app-image-structures) format.
     /// Use [IdfOptions] to configure flashing.