nounsql-core 0.1.1

Core of the NounSQL compiler: lexer, parser, resolver, and DDL code generation.
Documentation
/// 出力ターゲット。型名・予約語・DDLの書き方を持つ。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dialect {
    pub name: &'static str,
    pub types: &'static [&'static str],
    pub reserved: &'static [&'static str],
}

pub const POSTGRES: Dialect = Dialect {
    name: "postgres",
    types: POSTGRES_TYPES,
    reserved: POSTGRES_RESERVED,
};

pub const ALL: &[Dialect] = &[POSTGRES];

pub fn default() -> Dialect {
    POSTGRES
}

pub fn by_name(name: &str) -> Option<Dialect> {
    ALL.iter().copied().find(|d| d.name == name)
}

pub fn names() -> Vec<&'static str> {
    ALL.iter().map(|d| d.name).collect()
}

impl Dialect {
    pub fn has_type(&self, name: &str) -> bool {
        self.types.contains(&name)
    }

    pub fn is_reserved(&self, name: &str) -> bool {
        self.reserved.contains(&name.to_ascii_lowercase().as_str())
    }

    /// serial 系のPKを参照するFKは対応する整数型になる。
    pub fn fk_type(&self, pk_type: &str) -> String {
        match pk_type {
            "smallserial" | "serial2" => "smallint",
            "serial" | "serial4" => "integer",
            "bigserial" | "serial8" => "bigint",
            other => other,
        }
        .to_string()
    }
}

const POSTGRES_TYPES: &[&str] = &[
    "smallint",
    "integer",
    "bigint",
    "int2",
    "int4",
    "int8",
    "decimal",
    "numeric",
    "real",
    "float4",
    "float8",
    "smallserial",
    "serial",
    "bigserial",
    "serial2",
    "serial4",
    "serial8",
    "money",
    "varchar",
    "char",
    "text",
    "bytea",
    "timestamp",
    "timestamptz",
    "date",
    "time",
    "timetz",
    "interval",
    "boolean",
    "bool",
    "point",
    "line",
    "lseg",
    "box",
    "path",
    "polygon",
    "circle",
    "cidr",
    "inet",
    "macaddr",
    "macaddr8",
    "bit",
    "varbit",
    "tsvector",
    "tsquery",
    "uuid",
    "xml",
    "json",
    "jsonb",
    "jsonpath",
    "int4range",
    "int8range",
    "numrange",
    "tsrange",
    "tstzrange",
    "daterange",
    "int4multirange",
    "int8multirange",
    "nummultirange",
    "tsmultirange",
    "tstzmultirange",
    "datemultirange",
    "pg_lsn",
    "pg_snapshot",
    "oid",
    "regclass",
    "regproc",
    "regprocedure",
    "regoper",
    "regoperator",
    "regtype",
    "regrole",
    "regnamespace",
    "regconfig",
    "regdictionary",
];

const POSTGRES_RESERVED: &[&str] = &[
    "all",
    "analyse",
    "analyze",
    "and",
    "any",
    "array",
    "as",
    "asc",
    "asymmetric",
    "authorization",
    "binary",
    "both",
    "case",
    "cast",
    "check",
    "collate",
    "collation",
    "column",
    "concurrently",
    "constraint",
    "create",
    "cross",
    "current_catalog",
    "current_date",
    "current_role",
    "current_schema",
    "current_time",
    "current_timestamp",
    "current_user",
    "default",
    "deferrable",
    "desc",
    "distinct",
    "do",
    "else",
    "end",
    "except",
    "false",
    "fetch",
    "for",
    "foreign",
    "freeze",
    "from",
    "full",
    "grant",
    "group",
    "having",
    "ilike",
    "in",
    "initially",
    "inner",
    "intersect",
    "into",
    "is",
    "isnull",
    "join",
    "lateral",
    "leading",
    "left",
    "like",
    "limit",
    "localtime",
    "localtimestamp",
    "natural",
    "not",
    "notnull",
    "null",
    "offset",
    "on",
    "only",
    "or",
    "order",
    "outer",
    "overlaps",
    "placing",
    "primary",
    "references",
    "returning",
    "right",
    "select",
    "session_user",
    "similar",
    "some",
    "symmetric",
    "table",
    "tablesample",
    "then",
    "to",
    "trailing",
    "true",
    "union",
    "unique",
    "user",
    "using",
    "variadic",
    "verbose",
    "when",
    "where",
    "window",
    "with",
];