diff --git a/edgedb-protocol/src/annotations.rs b/edgedb-protocol/src/annotations.rs index d272fbb5..981e1e6d 100644 --- a/edgedb-protocol/src/annotations.rs +++ b/edgedb-protocol/src/annotations.rs @@ -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, /// Additional user-friendly info + #[cfg_attr(feature = "with-serde", serde(default))] pub hint: Option, /// Developer-friendly explanation of why this problem occured + #[cfg_attr(feature = "with-serde", serde(default))] pub details: Option, /// 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, /// 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, /// 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, /// 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, } @@ -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::() - .map_err(serde::de::Error::custom) - .map(Some), - StringOrInt::Number(i) => Ok(Some(i)), - StringOrInt::None => Ok(None), - } + Option::::deserialize(deserializer)? + .map(|x| match x { + StringOrInt::String(s) => s.parse::().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 + } + ); } diff --git a/edgedb-tokio/src/query_executor.rs b/edgedb-tokio/src/query_executor.rs index 2f4b4933..0aa01901 100644 --- a/edgedb-tokio/src/query_executor.rs +++ b/edgedb-tokio/src/query_executor.rs @@ -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 { + /// Query results pub data: R, + + /// Query warnings pub warnings: Vec, } diff --git a/edgedb-tokio/src/server_params.rs b/edgedb-tokio/src/server_params.rs index 90cc39dc..2cfe042f 100644 --- a/edgedb-tokio/src/server_params.rs +++ b/edgedb-tokio/src/server_params.rs @@ -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 {