mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00
WIP: NoticeResponseBuilder
This commit is contained in:
parent
eaeef57b96
commit
888f18cdf2
@ -12,6 +12,6 @@ pub use self::{
|
|||||||
decode::Decode,
|
decode::Decode,
|
||||||
encode::Encode,
|
encode::Encode,
|
||||||
message::Message,
|
message::Message,
|
||||||
notice_response::{NoticeResponse, Severity},
|
notice_response::{NoticeResponse, NoticeResponseBuilder, NoticeResponseFields, Severity},
|
||||||
ready_for_query::{ReadyForQuery, TransactionStatus},
|
ready_for_query::{ReadyForQuery, TransactionStatus},
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,7 @@ use std::{
|
|||||||
ptr::NonNull,
|
ptr::NonNull,
|
||||||
str::{self, FromStr},
|
str::{self, FromStr},
|
||||||
};
|
};
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
|
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone)]
|
||||||
pub enum Severity {
|
pub enum Severity {
|
||||||
@ -48,9 +49,7 @@ pub struct NoticeResponse(Bytes);
|
|||||||
|
|
||||||
impl NoticeResponse {
|
impl NoticeResponse {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn fields(self) -> io::Result<NoticeResponseFields> {
|
pub fn fields(self) -> io::Result<NoticeResponseFields> { NoticeResponseFields::decode(self.0) }
|
||||||
NoticeResponseFields::decode(self.0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for NoticeResponse {
|
impl fmt::Debug for NoticeResponse {
|
||||||
@ -62,9 +61,7 @@ impl fmt::Debug for NoticeResponse {
|
|||||||
|
|
||||||
impl Encode for NoticeResponse {
|
impl Encode for NoticeResponse {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn size_hint(&self) -> usize {
|
fn size_hint(&self) -> usize { self.0.len() + 5 }
|
||||||
self.0.len() + 5
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
|
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
|
||||||
@ -82,6 +79,7 @@ impl Decode for NoticeResponse {
|
|||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
// NOTE: Further decoding is delayed until `.fields()`
|
||||||
Ok(Self(src))
|
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<Cow<'a, str>>,
|
||||||
|
hint: Option<Cow<'a, str>>,
|
||||||
|
position: Option<usize>,
|
||||||
|
internal_position: Option<usize>,
|
||||||
|
internal_query: Option<Cow<'a, str>>,
|
||||||
|
where_: Option<Cow<'a, str>>,
|
||||||
|
schema: Option<Cow<'a, str>>,
|
||||||
|
table: Option<Cow<'a, str>>,
|
||||||
|
column: Option<Cow<'a, str>>,
|
||||||
|
data_type: Option<Cow<'a, str>>,
|
||||||
|
constraint: Option<Cow<'a, str>>,
|
||||||
|
file: Option<Cow<'a, str>>,
|
||||||
|
line: Option<usize>,
|
||||||
|
routine: Option<Cow<'a, str>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Cow<'a, str>>) -> Self {
|
||||||
|
self.message = message.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{Message, Severity, Decode};
|
use crate::{Decode, Message, Severity};
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user