Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DAP: Deserialize number IDs #10943

Merged
merged 5 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions helix-dap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod client;
mod test;
mod transport;
mod types;

Expand Down
18 changes: 18 additions & 0 deletions helix-dap/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[cfg(test)]
mod tests {
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved
use crate::Module;

#[test]
fn test_deserialize_module_id_from_number() {
let raw = r#"{"id": 0, "name": "Name"}"#;
let module: Module = serde_json::from_str(raw).expect("Error!");
assert_eq!(module.id, "0");
}

#[test]
fn test_deserialize_module_id_from_string() {
let raw = r#"{"id": "0", "name": "Name"}"#;
let module: Module = serde_json::from_str(raw).expect("Error!");
assert_eq!(module.id, "0");
}
}
22 changes: 20 additions & 2 deletions helix-dap/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down Expand Up @@ -311,7 +311,8 @@ pub struct Variable {
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Module {
pub id: String, // TODO: || number
#[serde(deserialize_with = "from_number")]
pub id: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<PathBuf>,
Expand All @@ -331,6 +332,23 @@ pub struct Module {
pub address_range: Option<String>,
}

fn from_number<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum NumberOrString {
Number(i64),
String(String),
}

match NumberOrString::deserialize(deserializer)? {
NumberOrString::Number(n) => Ok(n.to_string()),
NumberOrString::String(s) => Ok(s),
}
}

pub mod requests {
use super::*;
#[derive(Debug, Default, PartialEq, Eq, Clone, Deserialize, Serialize)]
Expand Down
Loading