Schema

Struct Schema 

Source
pub struct Schema {
    pub version: Option<u32>,
    pub tables: Vec<TableDef>,
}
Expand description

Schema containing all table definitions

Fields§

§version: Option<u32>

Schema format version (extracted from -- qail: version=N directive)

§tables: Vec<TableDef>

Implementations§

Source§

impl Schema

Source

pub fn parse(input: &str) -> Result<Self, String>

Parse a schema from .qail format string

Examples found in repository?
examples/test_schema_parse.rs (line 26)
5fn main() {
6    let schema_content = r#"
7-- Test Schema
8table users (
9    id uuid primary_key,
10    email text not null,
11    name varchar(255),
12    tags text[],
13    created_at timestamp
14)
15
16table orders (
17    id serial primary_key,
18    user_id uuid not null references users(id),
19    total decimal(10,2),
20    status text check(status in ('pending', 'completed'))
21)
22"#;
23
24    println!("Testing schema parsing...\n");
25
26    match Schema::parse(schema_content) {
27        Ok(schema) => {
28            println!("✓ Parsed {} tables", schema.tables.len());
29
30            for table in &schema.tables {
31                println!("\nTable: {}", table.name);
32                for col in &table.columns {
33                    print!("  - {} {}", col.name, col.typ);
34                    if col.primary_key {
35                        print!(" PK");
36                    }
37                    if !col.nullable {
38                        print!(" NOT NULL");
39                    }
40                    if col.is_array {
41                        print!(" ARRAY");
42                    }
43                    if col.is_serial {
44                        print!(" SERIAL");
45                    }
46                    if let Some(ref check) = col.check {
47                        print!(" CHECK({})", check);
48                    }
49                    if let Some(ref refs) = col.references {
50                        print!(" -> {}", refs);
51                    }
52                    if let Some(ref params) = col.type_params {
53                        print!(" ({})", params.join(","));
54                    }
55                    println!();
56                }
57            }
58
59            println!("\n✓ JSON export:");
60            match schema.to_json() {
61                Ok(json) => println!("{}", &json[..300.min(json.len())]),
62                Err(e) => println!("JSON error: {}", e),
63            }
64        }
65        Err(e) => {
66            eprintln!("✗ Parse error: {}", e);
67            std::process::exit(1);
68        }
69    }
70}
Source

pub fn find_table(&self, name: &str) -> Option<&TableDef>

Find a table by name

Source

pub fn to_json(&self) -> Result<String, String>

Export schema to JSON string (for qail-macros compatibility)

Examples found in repository?
examples/test_schema_parse.rs (line 60)
5fn main() {
6    let schema_content = r#"
7-- Test Schema
8table users (
9    id uuid primary_key,
10    email text not null,
11    name varchar(255),
12    tags text[],
13    created_at timestamp
14)
15
16table orders (
17    id serial primary_key,
18    user_id uuid not null references users(id),
19    total decimal(10,2),
20    status text check(status in ('pending', 'completed'))
21)
22"#;
23
24    println!("Testing schema parsing...\n");
25
26    match Schema::parse(schema_content) {
27        Ok(schema) => {
28            println!("✓ Parsed {} tables", schema.tables.len());
29
30            for table in &schema.tables {
31                println!("\nTable: {}", table.name);
32                for col in &table.columns {
33                    print!("  - {} {}", col.name, col.typ);
34                    if col.primary_key {
35                        print!(" PK");
36                    }
37                    if !col.nullable {
38                        print!(" NOT NULL");
39                    }
40                    if col.is_array {
41                        print!(" ARRAY");
42                    }
43                    if col.is_serial {
44                        print!(" SERIAL");
45                    }
46                    if let Some(ref check) = col.check {
47                        print!(" CHECK({})", check);
48                    }
49                    if let Some(ref refs) = col.references {
50                        print!(" -> {}", refs);
51                    }
52                    if let Some(ref params) = col.type_params {
53                        print!(" ({})", params.join(","));
54                    }
55                    println!();
56                }
57            }
58
59            println!("\n✓ JSON export:");
60            match schema.to_json() {
61                Ok(json) => println!("{}", &json[..300.min(json.len())]),
62                Err(e) => println!("JSON error: {}", e),
63            }
64        }
65        Err(e) => {
66            eprintln!("✗ Parse error: {}", e);
67            std::process::exit(1);
68        }
69    }
70}
Source

pub fn from_json(json: &str) -> Result<Self, String>

Import schema from JSON string

Source

pub fn from_file(path: &Path) -> Result<Self, String>

Load schema from a .qail file

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Schema

Source§

fn default() -> Schema

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Schema

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Schema

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,