Skip to content

Commit

Permalink
Move logic into append_out_param
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Oct 29, 2024
1 parent f3112d1 commit f174945
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion tool/src/demo_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Designed to work in conjunction with the JS backend.
//!
//! See docs/demo_gen.md for more.
use std::{collections::BTreeSet, fmt::Write};
use std::{collections::{BTreeSet, HashMap}, fmt::Write};

use askama::{self, Template};
use diplomat_core::hir::{BackendAttrSupport, TypeContext};
Expand Down Expand Up @@ -201,6 +201,8 @@ pub(crate) fn run<'tcx>(
imports: BTreeSet::new(),
},

out_param_collision: HashMap::new(),

relative_import_path: import_path.clone(),
module_name: module_name.clone(),
};
Expand Down
29 changes: 14 additions & 15 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
pub errors: &'ctx ErrorStore<'tcx, String>,
pub terminus_info: TerminusInfo,

/// To avoid similar parameter names while we're collecting [`OutParam`]s.
pub out_param_collision : HashMap<String, i32>,

pub relative_import_path: String,
pub module_name: String,
}
Expand Down Expand Up @@ -172,20 +175,6 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
self.relative_import_path.clone(),
);

let mut param_collision_dict : HashMap<String, i32> = HashMap::new();

for out_param in &mut self.terminus_info.out_params {
if param_collision_dict.contains_key(&out_param.param_name) {
let num = param_collision_dict.get(&out_param.param_name).unwrap();

out_param.param_name = format!("{}_{}", out_param.param_name, num);

param_collision_dict.insert(out_param.param_name.clone(), num + 1);
} else {
param_collision_dict.insert(out_param.param_name.clone(), 1);
}
}

self.terminus_info.imports.insert(format);
}

Expand Down Expand Up @@ -250,8 +239,18 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
}
};

let (p, n) = if self.out_param_collision.contains_key(&param_name) {
let n = self.out_param_collision.get(&param_name).unwrap();

(format!("{param_name}_{n}"), n+1)
} else {
(param_name.clone(), 1)
};

self.out_param_collision.insert(param_name, n);

let out_param = OutParam {
param_name,
param_name: p,
label,
type_name: type_name.clone(),
type_use,
Expand Down

0 comments on commit f174945

Please sign in to comment.