Skip to content

Commit

Permalink
Fix warnings with some fields missing (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen authored Oct 21, 2024
1 parent a12e2ae commit 479e570
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
108 changes: 95 additions & 13 deletions edgedb-protocol/src/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,42 @@ pub struct Warning {
pub code: u64,

/// Name of the source file that caused the warning.
#[cfg_attr(feature = "with-serde", serde(default))]
pub filename: Option<String>,

/// Additional user-friendly info
#[cfg_attr(feature = "with-serde", serde(default))]
pub hint: Option<String>,

/// Developer-friendly explanation of why this problem occured
#[cfg_attr(feature = "with-serde", serde(default))]
pub details: Option<String>,

/// Inclusive 0-based position within the source
#[cfg_attr(
feature = "with-serde",
serde(deserialize_with = "deserialize_usize_from_str")
serde(deserialize_with = "deserialize_usize_from_str", default)
)]
pub start: Option<usize>,

/// Exclusive 0-based position within the source
#[cfg_attr(
feature = "with-serde",
serde(deserialize_with = "deserialize_usize_from_str")
serde(deserialize_with = "deserialize_usize_from_str", default)
)]
pub end: Option<usize>,

/// 1-based index of the line of the start
#[cfg_attr(
feature = "with-serde",
serde(deserialize_with = "deserialize_usize_from_str")
serde(deserialize_with = "deserialize_usize_from_str", default)
)]
pub line: Option<usize>,

/// 1-based index of the column of the start
#[cfg_attr(
feature = "with-serde",
serde(deserialize_with = "deserialize_usize_from_str")
serde(deserialize_with = "deserialize_usize_from_str", default)
)]
pub col: Option<usize>,
}
Expand Down Expand Up @@ -101,15 +104,94 @@ fn deserialize_usize_from_str<'de, D: serde::Deserializer<'de>>(
enum StringOrInt {
String(String),
Number(usize),
None,
}

match StringOrInt::deserialize(deserializer)? {
StringOrInt::String(s) => s
.parse::<usize>()
.map_err(serde::de::Error::custom)
.map(Some),
StringOrInt::Number(i) => Ok(Some(i)),
StringOrInt::None => Ok(None),
}
Option::<StringOrInt>::deserialize(deserializer)?
.map(|x| match x {
StringOrInt::String(s) => s.parse::<usize>().map_err(serde::de::Error::custom),
StringOrInt::Number(i) => Ok(i),
})
.transpose()
}

#[test]
#[cfg(feature = "with-serde")]
fn deserialize_warning() {
let a: Warning =
serde_json::from_str(r#"{"message": "a", "type": "WarningException", "code": 1}"#).unwrap();
assert_eq!(
a,
Warning {
message: "a".to_string(),
r#type: "WarningException".to_string(),
code: 1,
filename: None,
hint: None,
details: None,
start: None,
end: None,
line: None,
col: None
}
);

let a: Warning = serde_json::from_str(
r#"{"message": "a", "type": "WarningException", "code": 1, "start": null}"#,
)
.unwrap();
assert_eq!(
a,
Warning {
message: "a".to_string(),
r#type: "WarningException".to_string(),
code: 1,
filename: None,
hint: None,
details: None,
start: None,
end: None,
line: None,
col: None
}
);

let a: Warning = serde_json::from_str(
r#"{"message": "a", "type": "WarningException", "code": 1, "start": 23}"#,
)
.unwrap();
assert_eq!(
a,
Warning {
message: "a".to_string(),
r#type: "WarningException".to_string(),
code: 1,
filename: None,
hint: None,
details: None,
start: Some(23),
end: None,
line: None,
col: None
}
);

let a: Warning = serde_json::from_str(
r#"{"message": "a", "type": "WarningException", "code": 1, "start": "23"}"#,
)
.unwrap();
assert_eq!(
a,
Warning {
message: "a".to_string(),
r#type: "WarningException".to_string(),
code: 1,
filename: None,
hint: None,
details: None,
start: Some(23),
end: None,
line: None,
col: None
}
);
}
5 changes: 5 additions & 0 deletions edgedb-tokio/src/query_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ use std::future::Future;

use crate::{Client, Error, Transaction};

/// Query result with additional metadata.
#[non_exhaustive]
#[derive(Debug)]
pub struct ResultVerbose<R> {
/// Query results
pub data: R,

/// Query warnings
pub warnings: Vec<Warning>,
}

Expand Down
1 change: 1 addition & 0 deletions edgedb-tokio/src/server_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl ServerParam for PostgresAddress {
impl SealedParam for PostgresAddress {}

#[derive(Debug)]
#[allow(dead_code)]
pub struct PostgresDsn(pub String);

impl ServerParam for PostgresDsn {
Expand Down

0 comments on commit 479e570

Please sign in to comment.