From 888f18cdf27d1ed4edcfee9d17bb13c694022276 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 20 Jun 2019 22:56:33 -0700 Subject: [PATCH] WIP: NoticeResponseBuilder --- mason-postgres-protocol/src/lib.rs | 2 +- .../src/notice_response.rs | 75 +++++++++++++++++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/mason-postgres-protocol/src/lib.rs b/mason-postgres-protocol/src/lib.rs index 159b7efc..06e82ef8 100644 --- a/mason-postgres-protocol/src/lib.rs +++ b/mason-postgres-protocol/src/lib.rs @@ -12,6 +12,6 @@ pub use self::{ decode::Decode, encode::Encode, message::Message, - notice_response::{NoticeResponse, Severity}, + notice_response::{NoticeResponse, NoticeResponseBuilder, NoticeResponseFields, Severity}, ready_for_query::{ReadyForQuery, TransactionStatus}, }; diff --git a/mason-postgres-protocol/src/notice_response.rs b/mason-postgres-protocol/src/notice_response.rs index 2299552a..590afa93 100644 --- a/mason-postgres-protocol/src/notice_response.rs +++ b/mason-postgres-protocol/src/notice_response.rs @@ -8,6 +8,7 @@ use std::{ ptr::NonNull, str::{self, FromStr}, }; +use std::borrow::Cow; #[derive(Debug, PartialEq, PartialOrd, Copy, Clone)] pub enum Severity { @@ -48,9 +49,7 @@ pub struct NoticeResponse(Bytes); impl NoticeResponse { #[inline] - pub fn fields(self) -> io::Result { - NoticeResponseFields::decode(self.0) - } + pub fn fields(self) -> io::Result { NoticeResponseFields::decode(self.0) } } impl fmt::Debug for NoticeResponse { @@ -62,9 +61,7 @@ impl fmt::Debug for NoticeResponse { impl Encode for NoticeResponse { #[inline] - fn size_hint(&self) -> usize { - self.0.len() + 5 - } + fn size_hint(&self) -> usize { self.0.len() + 5 } #[inline] fn encode(&self, buf: &mut Vec) -> io::Result<()> { @@ -82,6 +79,7 @@ impl Decode for NoticeResponse { where Self: Sized, { + // NOTE: Further decoding is delayed until `.fields()` Ok(Self(src)) } } @@ -388,9 +386,72 @@ impl Decode for NoticeResponseFields { } } +pub struct NoticeResponseBuilder<'a> { + severity: Severity, + code: Cow<'a, str>, + message: Cow<'a, str>, + detail: Option>, + hint: Option>, + position: Option, + internal_position: Option, + internal_query: Option>, + where_: Option>, + schema: Option>, + table: Option>, + column: Option>, + data_type: Option>, + constraint: Option>, + file: Option>, + line: Option, + routine: Option>, +} + +impl Default for NoticeResponseBuilder<'_> { + fn default() -> Self { + Self { + severity: Severity::Notice, + message: Cow::Borrowed(""), + code: Cow::Borrowed("XX000"), // internal_error + detail: None, + hint: None, + position: None, + internal_position: None, + internal_query: None, + where_: None, + schema: None, + table: None, + column: None, + data_type: None, + constraint: None, + file: None, + line: None, + routine: None, + } + } +} + +impl<'a> NoticeResponseBuilder<'a> { + #[inline] + pub fn new() -> NoticeResponseBuilder<'a> { + Self::default() + } + + #[inline] + pub fn severity(mut self, severity: Severity) -> Self { + self.severity = severity; + self + } + + #[inline] + pub fn message(mut self, message: impl Into>) -> Self { + self.message = message.into(); + self + } +} + #[cfg(test)] mod tests { - use crate::{Message, Severity, Decode}; + use crate::{Decode, Message, Severity}; use bytes::Bytes; use std::io;