#yaml-parser #database-schema #table #table-column #column-name #required #materynskyis

bin+lib materynskyis_yaml_schema_parser

A YAML parser for database schema files

3 releases

0.1.2 Nov 13, 2024
0.1.1 Nov 12, 2024
0.1.0 Nov 3, 2024

#1550 in Database interfaces

Download history 57/week @ 2024-10-28 57/week @ 2024-11-04 238/week @ 2024-11-11 22/week @ 2024-11-18

374 downloads per month

MIT license

14KB
196 lines

My YAML Parser

A Rust program for parsing .yaml files that define a database schema. This parser identifies tables, and columns to allow structured interpretation of database schema files.

Technical Description

The parser uses the Pest library to define grammar rules that interpret specific .yaml structures. It parses:

  • Schema name
  • Tables within the schema
  • Columns their types and modes within each table

The parsed data can be used to generate structured representation of database schemas for your application.

Example of Schema

Schema should be structured like this  
File
 └── Schema Section
      ├── Schema Entry
      │    ├── Name: "name"
      │    └── Tables
      │         └── Table Section
      │              ├── Table Entry
      │              │    ├── Name: table_name
      │              │    └── Columns
      │              │         └── Column Section
      │              │              ├── Column Entry
      │              │              │    ├── Column Name
      │              │              │    ├── Type
      │              │              │    └── Mode

And generally look like this

schema:
  name: myschema
  tables:
    user:
      columns:
        id:
          type: int
          mode: required
        name:
          type: str
          mode: required
        avg_grade:
          type: float
          mode: nullable

This schema could be processed like this.

file_name => {
            let content = fs::read_to_string(file_name);
            match content {
                Ok(file_content) => match parse_schema(&file_content) {
                    Ok(schema) => println!("{:#?}", schema),
                    Err(SchemaErr::ParseError(e)) => {
                        eprintln!("Error while parsing the file: {}", e)
                    }
                    Err(materynskyis_yaml_schema_parser::SchemaErr::MissingValue) => {
                        eprintln!("Missing value");
                    }
                },
                Err(e) => eprintln!("Failed to read file '{}': {}", file_name, e),
            }
        }

And in result we will have this struct:

Schema {
    name: "myschema",
    tables: [
        Table {
            name: "user",
            columns: [
                Column {
                    name: "id",
                    datatype: "int",
                    mode: "required",
                },
                Column {
                    name: "name",
                    datatype: "str",
                    mode: "required",
                },
                Column {
                    name: "avg_grade",
                    datatype: "float",
                    mode: "nullable",
                },
            ],
        },
    ],
}

Dependencies

~2.2–3MB
~60K SLoC