sqlgen 0.1.6

A library to generate SQL Statements.
Documentation
use crate::prelude::*;

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug)]
pub enum SelectList {
    All,
    TableAll { table: Table },
    TableColumn { table: Table, column: Column },
    Column { column: Column },
    None,
}

impl Sql for SelectList {
    fn sql(&self, mut s: String, ctx: &Context) -> Result<String> {
        match self {
            SelectList::All => {
                s.push('*');

                Ok(s)
            }
            SelectList::TableAll { table } => {
                let mut s = table.sql(s, ctx)?;
                s.push('.');
                s.push('*');

                Ok(s)
            }
            SelectList::TableColumn { table, column } => {
                let mut s = table.sql(s, ctx)?;
                s.push('.');
                let s = column.sql(s, ctx);

                s
            }
            SelectList::Column { column } => column.sql(s, ctx),
            SelectList::None => Err(SqlGenError::ParseFailed(
                "Was not able to parse your table input".into(),
            )),
        }
    }
}

impl<T> From<T> for SelectList
where
    T: AsRef<str>,
{
    fn from(value: T) -> Self {
        let splitted: Vec<&str> = value.as_ref().split('.').collect();

        match splitted[..] {
            [column] if column == "*" => SelectList::All,
            [column] => SelectList::Column {
                column: Column::new(column),
            },
            [table, column] if column == "*" => SelectList::TableAll {
                table: Table::new(table),
            },
            [table, column] => SelectList::TableColumn {
                table: Table::new(table),
                column: Column::new(column),
            },
            _ => SelectList::None,
        }
    }
}