Skip to main content

CopyInsert

Derive Macro CopyInsert 

Source
#[derive(CopyInsert)]
{
    // Attributes available to this derive:
    #[copy_insert]
    #[copy]
}
Expand description

Implement a PostgreSQL COPY row-binding contract for a named-field structure.

ยงExample

mod support {
    #[derive(Default)]
    pub struct CopyRow(pub Vec<String>);

    impl CopyRow {
        pub fn push_bind(&mut self, value: impl ToString) -> &mut Self {
            self.0.push(value.to_string());
            self
        }
    }

    pub trait CopyInsert {
        const COLUMNS: &'static [&'static str];
        fn copy_row(&self, row: &mut CopyRow, patch: &str);
    }

    pub fn to_json<T>(_value: &T) -> &'static str {
        "null"
    }
}

use support::CopyInsert as _;
use wowlab_engine_macros::CopyInsert;

#[derive(CopyInsert)]
#[copy_insert(crate = "crate::support")]
struct Row {
    id: i32,
    name: String,
}

fn main() {
    let mut output = support::CopyRow::default();
    Row { id: 7, name: "Ada".into() }.copy_row(&mut output, "v1");
    assert_eq!(output.0, ["7", "v1", "Ada"]);
}