Skip to content

Commit

Permalink
add: rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 22, 2023
1 parent 1a1b414 commit 9f9baf0
Show file tree
Hide file tree
Showing 18 changed files with 801 additions and 707 deletions.
5 changes: 3 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
max_width = 50
reorder_imports = true
max_width = 100
reorder_imports = true
tab_spaces = 2
101 changes: 61 additions & 40 deletions src/analyzer/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,86 @@ pub struct IndexedRefBlock {
pub rel: refs::Relation,
pub lhs: refs::RefIdent,
pub rhs: refs::RefIdent,
pub settings: Option<refs::RefSettings>
pub settings: Option<refs::RefSettings>,
}

impl IndexedRefBlock {
pub fn from_inline(ref_blocks: Vec<refs::RefInline>, table_ident: table::TableIdent, col_name: String) -> Vec<Self> {
ref_blocks.into_iter().map(|ref_block| {
let table_ident = table_ident.clone();
let col_name = col_name.clone();

let refs::RefInline {
rel,
rhs,
} = ref_block;

let lhs = refs::RefIdent {
schema: table_ident.schema,
table: table_ident.name,
compositions: vec![col_name]
};

Self {
rel,
lhs,
rhs,
settings: None
}.normalize()
}).collect()
pub fn from_inline(
ref_blocks: Vec<refs::RefInline>,
table_ident: table::TableIdent,
col_name: String,
) -> Vec<Self> {
ref_blocks
.into_iter()
.map(|ref_block| {
let table_ident = table_ident.clone();
let col_name = col_name.clone();

let refs::RefInline { rel, rhs } = ref_block;

let lhs = refs::RefIdent {
schema: table_ident.schema,
table: table_ident.name,
compositions: vec![col_name],
};

Self {
rel,
lhs,
rhs,
settings: None,
}
.normalize()
})
.collect()
}

pub fn validate_ref_type(
&self,
tables: &Vec<table::TableBlock>,
indexer: &indexer::Indexer
indexer: &indexer::Indexer,
) -> Result<(), String> {
let lhs_ident = indexer.refer_ref_alias(&self.lhs);
let rhs_ident = indexer.refer_ref_alias(&self.rhs);

if lhs_ident.compositions.len() != rhs_ident.compositions.len() {
return Err(format!("relation composition must have number of fields equal in both side"));
return Err(format!(
"relation composition must have number of fields equal in both side"
));
}

indexer.lookup_table_fields(&lhs_ident.schema, &lhs_ident.table, &lhs_ident.compositions)?;
indexer.lookup_table_fields(&rhs_ident.schema, &rhs_ident.table, &rhs_ident.compositions)?;

let lhs_table = tables.iter().find(|table| {
table.ident.schema == lhs_ident.schema && table.ident.name == lhs_ident.table
}).ok_or_else(|| format!("cannot find lhs table"))?;
let lhs_table = tables
.iter()
.find(|table| table.ident.schema == lhs_ident.schema && table.ident.name == lhs_ident.table)
.ok_or_else(|| format!("cannot find lhs table"))?;

let rhs_table = tables.iter().find(|table| {
table.ident.schema == rhs_ident.schema && table.ident.name == rhs_ident.table
}).ok_or_else(|| format!("cannot find rhs table"))?;
let rhs_table = tables
.iter()
.find(|table| table.ident.schema == rhs_ident.schema && table.ident.name == rhs_ident.table)
.ok_or_else(|| format!("cannot find rhs table"))?;

let field_pairs = lhs_ident.compositions.iter().zip(rhs_ident.compositions.iter());
let field_pairs = lhs_ident
.compositions
.iter()
.zip(rhs_ident.compositions.iter());

for (l, r) in field_pairs.into_iter() {
let l_field = lhs_table.cols.iter().find(|col| &col.name == l).ok_or_else(|| format!("cannot find l col"))?;
let r_field = rhs_table.cols.iter().find(|col| &col.name == r).ok_or_else(|| format!("cannot find r col"))?;
let l_field = lhs_table
.cols
.iter()
.find(|col| &col.name == l)
.ok_or_else(|| format!("cannot find l col"))?;
let r_field = rhs_table
.cols
.iter()
.find(|col| &col.name == r)
.ok_or_else(|| format!("cannot find r col"))?;

if l_field.r#type != r_field.r#type {
return Err(format!("reference (composite) column type is mismatched"))
return Err(format!("reference (composite) column type is mismatched"));
}
}

Expand All @@ -93,14 +113,15 @@ impl From<refs::RefBlock> for IndexedRefBlock {
rel,
lhs,
rhs,
settings
settings,
} = ref_block;

Self {
rel,
lhs,
rhs,
settings
}.normalize()
settings,
}
.normalize()
}
}
}
32 changes: 16 additions & 16 deletions src/analyzer/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::ops::Range;

use crate::parser::Rule;

use pest::Span;
use pest::error::{Error, ErrorVariant};
use pest::iterators::Pair;
use pest::Span;

pub type AnalyzerResult<T> = Result<T, Error<Rule>>;

Expand All @@ -29,30 +29,30 @@ pub enum Err {
EnumValueNotFound,
MismatchedForeignKeyType,
MismatchedCompositeForeignKey,
UnsupportedSyntax
UnsupportedSyntax,
}

pub fn throw_msg<T>(msg: impl ToString, pair: Pair<Rule>) -> AnalyzerResult<T> {
Err(
Error::new_from_span(
ErrorVariant::CustomError { message: msg.to_string() },
pair.as_span()
)
)
Err(Error::new_from_span(
ErrorVariant::CustomError {
message: msg.to_string(),
},
pair.as_span(),
))
}

pub fn throw_err<T>(err: Err, span_range: Range<usize>, input: &str) -> AnalyzerResult<T> {
let span = Span::new(input, span_range.start, span_range.end).unwrap();

let msg = match err {
Err::ProjectSettingNotFound => "the project block is required",
_ => "error unexpected"
_ => "error unexpected",
};
Err(
Error::new_from_span(
ErrorVariant::CustomError { message: msg.to_string() },
span
)
)

Err(Error::new_from_span(
ErrorVariant::CustomError {
message: msg.to_string(),
},
span,
))
}
76 changes: 48 additions & 28 deletions src/analyzer/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::{HashSet, HashMap};
use std::collections::{HashMap, HashSet};

use super::*;

#[derive(Debug, PartialEq, Clone, Default)]
pub struct IndexedSchemaBlock {
table_map: HashMap<String, HashSet<String>>,
enum_map: HashMap<String, HashSet<String>>
enum_map: HashMap<String, HashSet<String>>,
}

#[derive(Debug, PartialEq, Clone, Default)]
Expand All @@ -25,7 +25,7 @@ impl Indexer {
span_range,
schema,
name,
alias
alias,
} = table.ident.clone();

let schema_name = schema.clone().unwrap_or_else(|| DEFAULT_SCHEMA.into());
Expand All @@ -46,7 +46,9 @@ impl Indexer {
if let Some(dup_alias) = self.alias_map.get(&alias) {
panic!("alias_name_dup");
} else {
self.alias_map.insert(alias.clone(), (schema.clone(), name.clone()));
self
.alias_map
.insert(alias.clone(), (schema.clone(), name.clone()));
}
}
} else {
Expand All @@ -55,7 +57,9 @@ impl Indexer {
index_block.table_map.insert(name.clone(), col_sets);

if let Some(alias) = alias {
self.alias_map.insert(alias.clone(), (schema.clone(), name.clone()));
self
.alias_map
.insert(alias.clone(), (schema.clone(), name.clone()));
}

self.schema_map.insert(schema_name, index_block);
Expand All @@ -67,10 +71,7 @@ impl Indexer {

pub fn index_enums(&mut self, enums: &Vec<enums::EnumBlock>) -> Result<(), String> {
for r#enum in enums.iter() {
let enums::EnumIdent {
schema,
name,
} = r#enum.ident.clone();
let enums::EnumIdent { schema, name } = r#enum.ident.clone();

let schema_name = schema.clone().unwrap_or_else(|| DEFAULT_SCHEMA.into());
let mut value_sets = HashSet::new();
Expand All @@ -97,7 +98,10 @@ impl Indexer {
Ok(())
}

pub fn index_table_groups(&mut self, table_groups: &Vec<table_group::TableGroupBlock>) -> Result<(), String> {
pub fn index_table_groups(
&mut self,
table_groups: &Vec<table_group::TableGroupBlock>,
) -> Result<(), String> {
for group_each in table_groups.into_iter() {
for table in group_each.table_idents.iter() {
let ident_alias = table.ident_alias.clone();
Expand Down Expand Up @@ -135,13 +139,20 @@ impl Indexer {
}
}

self.table_group_map.insert(group_each.name.clone(), table_sets);
self
.table_group_map
.insert(group_each.name.clone(), table_sets);
}

Ok(())
}

pub fn lookup_enum_values(&self, schema: &Option<String>, enum_name: &String, values: &Vec<String>) -> Result<(), String> {
pub fn lookup_enum_values(
&self,
schema: &Option<String>,
enum_name: &String,
values: &Vec<String>,
) -> Result<(), String> {
let schema = schema.clone().unwrap_or_else(|| DEFAULT_SCHEMA.into());

if let Some(block) = self.schema_map.get(&schema) {
Expand All @@ -161,23 +172,36 @@ impl Indexer {
}
}

pub fn lookup_table_fields(&self, schema: &Option<String>, table: &String, fields: &Vec<String>) -> Result<(), String> {
pub fn lookup_table_fields(
&self,
schema: &Option<String>,
table: &String,
fields: &Vec<String>,
) -> Result<(), String> {
let schema = schema.clone().unwrap_or_else(|| DEFAULT_SCHEMA.into());

if let Some(block) = self.schema_map.get(&schema) {
if let Some(col_set) = block.table_map.get(table) {
let unlisted_fields: Vec<_> = fields.iter().filter(|v| !col_set.contains(*v)).cloned().collect();
let unlisted_fields: Vec<_> = fields
.iter()
.filter(|v| !col_set.contains(*v))
.cloned()
.collect();

if unlisted_fields.is_empty() {
return Ok(())
return Ok(());
} else {
return Err(format!("not found '{}' column in table '{}'", unlisted_fields.join(", "), table));
return Err(format!(
"not found '{}' column in table '{}'",
unlisted_fields.join(", "),
table
));
}
}

return Err(format!("table_not_found"));
}

return Err(format!("schema_not_found"));
}

Expand All @@ -187,16 +211,12 @@ impl Indexer {

pub fn refer_ref_alias(&self, ident: &refs::RefIdent) -> refs::RefIdent {
match self.refer_alias(&ident.table) {
Some((schema, table)) => {
refs::RefIdent {
schema: schema.clone(),
table: table.clone(),
compositions: ident.compositions.clone()
}
Some((schema, table)) => refs::RefIdent {
schema: schema.clone(),
table: table.clone(),
compositions: ident.compositions.clone(),
},
None => {
ident.clone()
}
None => ident.clone(),
}
}
}
}
Loading

0 comments on commit 9f9baf0

Please sign in to comment.