rust-postgres/postgres-protocol/src/lib.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

2017-03-12 20:44:27 +00:00
//! Low level Postgres protocol APIs.
//!
//! This crate implements the low level components of Postgres's communication
//! protocol, including message and value serialization and deserialization.
//! It is designed to be used as a building block by higher level APIs such as
//! `rust-postgres`, and should not typically be used directly.
//!
//! # Note
//!
//! This library assumes that the `client_encoding` backend parameter has been
//! set to `UTF8`. It will most likely not behave properly if that is not the case.
2017-09-13 01:04:59 +00:00
#![doc(html_root_url="https://docs.rs/postgres-protocol/0.3")]
2017-03-12 20:44:27 +00:00
#![warn(missing_docs)]
2017-05-21 04:45:01 +00:00
extern crate base64;
2017-03-12 20:44:27 +00:00
extern crate byteorder;
2017-05-21 04:45:01 +00:00
extern crate bytes;
2017-03-12 20:44:27 +00:00
extern crate fallible_iterator;
2017-05-21 04:45:01 +00:00
extern crate generic_array;
extern crate hmac;
2017-03-12 20:44:27 +00:00
extern crate md5;
extern crate memchr;
2017-05-21 04:45:01 +00:00
extern crate rand;
extern crate sha2;
extern crate stringprep;
2017-03-12 20:44:27 +00:00
use byteorder::{BigEndian, ByteOrder};
use std::io;
pub mod authentication;
pub mod message;
pub mod types;
/// A Postgres OID.
pub type Oid = u32;
/// An enum indicating if a value is `NULL` or not.
pub enum IsNull {
/// The value is `NULL`.
Yes,
/// The value is not `NULL`.
No,
}
#[inline]
fn write_nullable<F, E>(serializer: F, buf: &mut Vec<u8>) -> Result<(), E>
2017-07-01 03:35:17 +00:00
where
F: FnOnce(&mut Vec<u8>) -> Result<IsNull, E>,
E: From<io::Error>,
2017-03-12 20:44:27 +00:00
{
let base = buf.len();
buf.extend_from_slice(&[0; 4]);
2017-05-23 04:55:51 +00:00
let size = match serializer(buf)? {
IsNull::No => i32::from_usize(buf.len() - base - 4)?,
2017-03-12 20:44:27 +00:00
IsNull::Yes => -1,
};
BigEndian::write_i32(&mut buf[base..], size);
Ok(())
}
trait FromUsize: Sized {
fn from_usize(x: usize) -> Result<Self, io::Error>;
}
macro_rules! from_usize {
($t:ty) => {
impl FromUsize for $t {
#[inline]
fn from_usize(x: usize) -> io::Result<$t> {
if x > <$t>::max_value() as usize {
Err(io::Error::new(io::ErrorKind::InvalidInput, "value too large to transmit"))
} else {
Ok(x as $t)
}
}
}
}
}
from_usize!(i16);
from_usize!(i32);