5个版本

0.2.0 2020年8月8日
0.1.3 2019年3月18日
0.1.2 2018年12月3日
0.1.1 2018年12月3日
0.1.0 2018年11月21日

#927解析器实现

Download history 267/week @ 2024-03-13 488/week @ 2024-03-20 292/week @ 2024-03-27 379/week @ 2024-04-03 284/week @ 2024-04-10 283/week @ 2024-04-17 289/week @ 2024-04-24 311/week @ 2024-05-01 279/week @ 2024-05-08 163/week @ 2024-05-15 245/week @ 2024-05-22 262/week @ 2024-05-29 381/week @ 2024-06-05 347/week @ 2024-06-12 351/week @ 2024-06-19 442/week @ 2024-06-26

1,632 每月下载量

MIT/Apache

25KB
502 代码行

Serde Postgres

Build status Crate Lines Of Code Documentation

轻松将来自tokio-postgrespostgres的行反序列化为任意结构体。(仅支持反序列化)。

示例

tokio-postgres(异步)

use std::error::Error;
use serde::Deserialize;
use tokio_postgres::{connect, NoTls};

#[derive(Clone, Debug, Deserialize)]
struct Person {
    name: String,
    age: i32,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let (client, connection) = connect("postgres://postgres@localhost:5432", NoTls).await?;
    tokio::spawn(connection);

    client.execute("CREATE TABLE IF NOT EXISTS Person (
        name VARCHAR NOT NULL,
        age INT NOT NULL
    )", &[]).await?;

    client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
                       &[&"Jane", &23i32]).await?;

    client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
                       &[&"Alice", &32i32]).await?;

    let rows = client.query("SELECT name, age FROM Person", &[]).await?;

    let people: Vec<Person> = serde_postgres::from_rows(&rows)?;

    for person in people {
        println!("{:?}", person);
    }

    Ok(())
}

postgres(同步)

use postgres::{Client, NoTls};
use serde::Deserialize;
use std::error::Error;

#[derive(Clone, Debug, Deserialize)]
struct Person {
    name: String,
    age: i32,
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut client = Client::connect("postgres://postgres@localhost:5432", NoTls)?;

    client.execute("CREATE TABLE IF NOT EXISTS Person (
        name VARCHAR NOT NULL,
        age INT NOT NULL
    )", &[])?;

    client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
                   &[&"Jane", &23i32])?;
    
    client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
                   &[&"Alice", &32i32])?;

    let rows = client.query("SELECT name, age FROM Person", &[])?;

    let people: Vec<Person> = serde_postgres::from_rows(&rows)?;

    for person in people {
        println!("{:?}", person);
    }

    Ok(())
}

依赖关系

~10MB
~202K SLoC