QueryFile

Struct QueryFile 

Source
pub struct QueryFile {
    pub queries: Vec<QueryDef>,
}
Expand description

Collection of named queries from a queries.qail file

Fields§

§queries: Vec<QueryDef>

Implementations§

Source§

impl QueryFile

Source

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

Parse a query file from .qail format string

Examples found in repository?
examples/test_query_parse.rs (line 26)
5fn main() {
6    let queries_content = r#"
7-- User queries
8query find_user(id: Uuid) -> User:
9  get users where id = :id
10
11query list_users() -> Vec<User>:
12  get users order by created_at desc
13
14query find_by_email(email: String) -> Option<User>:
15  get users where email = :email limit 1
16
17execute create_user(email: String, name: String):
18  add::users : email, name [ :email, :name ]
19
20execute delete_user(id: Uuid):
21  del::users where id = :id
22"#;
23
24    println!("Testing query file parsing...\n");
25
26    match QueryFile::parse(queries_content) {
27        Ok(qf) => {
28            println!("✓ Parsed {} queries\n", qf.queries.len());
29
30            for q in &qf.queries {
31                let kind = if q.is_execute { "execute" } else { "query" };
32                let params: Vec<_> = q
33                    .params
34                    .iter()
35                    .map(|p| format!("{}: {}", p.name, p.typ))
36                    .collect();
37
38                let return_type = match &q.return_type {
39                    Some(qail_core::parser::query_file::ReturnType::Single(t)) => {
40                        format!("-> {}", t)
41                    }
42                    Some(qail_core::parser::query_file::ReturnType::Vec(t)) => {
43                        format!("-> Vec<{}>", t)
44                    }
45                    Some(qail_core::parser::query_file::ReturnType::Option(t)) => {
46                        format!("-> Option<{}>", t)
47                    }
48                    None => String::new(),
49                };
50
51                println!("{} {}({}) {}", kind, q.name, params.join(", "), return_type);
52                println!("  body: {}", q.body.lines().next().unwrap_or(""));
53                println!();
54            }
55
56            println!("✓ All query parsing tests passed!");
57        }
58        Err(e) => {
59            eprintln!("✗ Parse error: {}", e);
60            std::process::exit(1);
61        }
62    }
63}
Source

pub fn find_query(&self, name: &str) -> Option<&QueryDef>

Find a query by name

Source

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

Export to JSON

Source

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

Import from JSON

Trait Implementations§

Source§

impl Clone for QueryFile

Source§

fn clone(&self) -> QueryFile

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 QueryFile

Source§

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

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

impl Default for QueryFile

Source§

fn default() -> QueryFile

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

impl<'de> Deserialize<'de> for QueryFile

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 QueryFile

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§

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>,