Skip to content

Commit

Permalink
Fix missed compile errors from refactoring in 33c84cb for psql
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed May 6, 2020
1 parent 5b828c4 commit d92f66d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
23 changes: 15 additions & 8 deletions asn1rs-model/src/gen/rust/async_psql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ impl GeneratorSupplement<Rust> for AsyncPsqlInserter {
Self::append_retrieve_for_container_type(name, impl_scope);

let fn_load = create_load_fn(impl_scope, true);
for (index, (variant, v_type)) in enumeration.variants().enumerate() {
for (index, variant) in enumeration.variants().enumerate() {
let mut block = Block::new(&format!(
"if row.try_get::<usize, Option<i32>>({})?.is_some()",
index + 1
));
Self::append_load_field(false, name, &mut block, index, "value", v_type);
block.line(&format!("return Ok({}::{}(value));", name, variant));
Self::append_load_field(false, name, &mut block, index, "value", variant.r#type());
block.line(&format!("return Ok({}::{}(value));", name, variant.name()));
fn_load.push_block(block);
}
fn_load.line(format!("Err({}::Error::RowUnloadable)", MODULE_NAME));
Expand All @@ -70,15 +70,22 @@ impl GeneratorSupplement<Rust> for AsyncPsqlInserter {
data_enum_insert_statement(name, enumeration)
));
let mut updated_variants = Vec::with_capacity(enumeration.len());
for (variant, v_type) in enumeration.variants() {
let module_name = RustCodeGenerator::rust_module_name(variant);
for variant in enumeration.variants() {
let module_name = RustCodeGenerator::rust_module_name(variant.name());
fn_insert.line(&format!(
"let {} = if let Self::{}(value) = self {{ Some({}value) }} else {{ None }};",
module_name,
variant,
if v_type.is_primitive() { "*" } else { "" }
variant.name(),
if variant.r#type().is_primitive() {
"*"
} else {
""
}
));
updated_variants.push((
module_name,
RustType::Option(Box::new(variant.r#type().clone())),
));
updated_variants.push((module_name, RustType::Option(Box::new(v_type.clone()))));
}
impl_insert_fn_content(false, false, name, &updated_variants[..], fn_insert);
}
Expand Down
44 changes: 31 additions & 13 deletions asn1rs-model/src/gen/rust/psql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,24 @@ impl PsqlInserter {

fn impl_data_enum_insert_fn(function: &mut Function, name: &str, enumeration: &DataEnum) {
let mut variables = Vec::with_capacity(enumeration.len());
for (variant, rust) in enumeration.variants() {
for variant in enumeration.variants() {
let variable = RustCodeGenerator::rust_field_name(
&RustCodeGenerator::rust_module_name(variant),
&RustCodeGenerator::rust_module_name(variant.name()),
true,
);
let sql_primitive = Model::<Sql>::is_primitive(rust);
let sql_primitive = Model::<Sql>::is_primitive(variant.r#type());
variables.push(format!("&{}", variable));
let mut block_if = Block::new(&format!(
"let {} = if let {}::{}(value) = self",
variable, name, variant
variable,
name,
variant.name()
));

if sql_primitive {
block_if.line(format!(
"Some({})",
Self::wrap_for_insert_in_as_or_from_if_required("*value", rust)
Self::wrap_for_insert_in_as_or_from_if_required("*value", variant.r#type())
.unwrap_or_else(|| "value".to_string())
));
} else {
Expand Down Expand Up @@ -595,31 +597,47 @@ impl PsqlInserter {
.join(", ")
));
let mut block = Block::new("match index");
for (index, (variant, rust)) in enumeration.variants().enumerate() {
let mut block_case =
Block::new(&format!("{} => Ok({}::{}(", index + 1, name, variant,));
for (index, variant) in enumeration.variants().enumerate() {
let mut block_case = Block::new(&format!(
"{} => Ok({}::{}(",
index + 1,
name,
variant.name()
));

if Model::<Sql>::is_primitive(rust.as_inner_type()) {
if let Some(wrap) = Self::wrap_for_query_in_as_or_from_if_required("", rust) {
if Model::<Sql>::is_primitive(variant.r#type().as_inner_type()) {
if let Some(wrap) =
Self::wrap_for_query_in_as_or_from_if_required("", variant.r#type())
{
block_case.line(format!(
"row.get_opt::<_, {}>({}).ok_or_else({}::no_result)??{}",
rust.as_inner_type().to_sql().to_rust().to_string(),
variant
.r#type()
.as_inner_type()
.to_sql()
.to_rust()
.to_string(),
index + 1,
ERROR_TYPE,
wrap
));
} else {
block_case.line(format!(
"row.get_opt::<_, {}>({}).ok_or_else({}::no_result)??",
rust.as_inner_type().to_sql().to_rust().to_string(),
variant
.r#type()
.as_inner_type()
.to_sql()
.to_rust()
.to_string(),
index + 1,
ERROR_TYPE
));
}
} else {
block_case.line(&format!(
"{}::query_with(transaction, row.get({}))?",
rust.clone().into_inner_type().to_string(),
variant.r#type().clone().into_inner_type().to_string(),
index + 1
));
}
Expand Down
2 changes: 1 addition & 1 deletion asn1rs-model/src/gen/rust/shared_psql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn data_enum_insert_statement(name: &str, enumeration: &DataEnum) ->
name,
enumeration
.variants()
.map(|(name, _)| RustCodeGenerator::rust_module_name(name))
.map(|variant| RustCodeGenerator::rust_module_name(variant.name()))
.collect::<Vec<String>>()
.join(", "),
enumeration
Expand Down

0 comments on commit d92f66d

Please sign in to comment.