2 releases

0.1.1 Jan 2, 2023
0.1.0 Dec 28, 2022

#197 in Procedural macros

Download history 17/week @ 2022-12-23 223/week @ 2022-12-30 3229/week @ 2023-01-06 5369/week @ 2023-01-13 6686/week @ 2023-01-20 7971/week @ 2023-01-27 7193/week @ 2023-02-03 10685/week @ 2023-02-10 9519/week @ 2023-02-17 13619/week @ 2023-02-24 13688/week @ 2023-03-03 16959/week @ 2023-03-10 15691/week @ 2023-03-17

62,082 downloads per month
Used in 23 crates (2 directly)

Apache-2.0

12KB
185 lines

SQL Parser Derive Macro

Visit

This crate contains a procedural macro that can automatically derive implementations of the Visit trait in the sqlparser crate

#[derive(Visit, VisitMut)]
struct Foo {
    boolean: bool,
    bar: Bar,
}

#[derive(Visit, VisitMut)]
enum Bar {
    A(),
    B(String, bool),
    C { named: i32 },
}

Will generate code akin to

impl Visit for Foo {
    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
        self.boolean.visit(visitor)?;
        self.bar.visit(visitor)?;
        ControlFlow::Continue(())
    }
}

impl Visit for Bar {
    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
        match self {
            Self::A() => {}
            Self::B(_1, _2) => {
                _1.visit(visitor)?;
                _2.visit(visitor)?;
            }
            Self::C { named } => {
                named.visit(visitor)?;
            }
        }
        ControlFlow::Continue(())
    }
}

Additionally certain types may wish to call a corresponding method on visitor before recursing

#[derive(Visit, VisitMut)]
#[visit(with = "visit_expr")]
enum Expr {
    A(),
    B(String, #[cfg_attr(feature = "visitor", visit(with = "visit_relation"))] ObjectName, bool),
}

Will generate

impl Visit for Bar {
    fn visit<V: Visitor>(&self, visitor: &mut V) -> ControlFlow<V::Break> {
        visitor.visit_expr(self)?;
        match self {
            Self::A() => {}
            Self::B(_1, _2, _3) => {
                _1.visit(visitor)?;
                visitor.visit_relation(_3)?;
                _2.visit(visitor)?;
                _3.visit(visitor)?;
            }
        }
        ControlFlow::Continue(())
    }
}

Dependencies

~0.7–1MB
~26K SLoC