Skip to content

Commit

Permalink
Merge pull request #22 from golemcloud/escaping-type-params
Browse files Browse the repository at this point in the history
Escaping names with type parameters
  • Loading branch information
vigoo authored Oct 30, 2024
2 parents c195cda + 08bea85 commit e8f0242
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/rust/lib_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use crate::printer::*;
use crate::rust::printer::*;
use crate::rust::types::{escape_keywords, RustPrinter};
use convert_case::{Case, Casing};
use itertools::Itertools;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)]
Expand All @@ -39,27 +40,38 @@ pub struct ModuleName {
}

impl ModuleName {
pub fn name(&self) -> String {
self.name.clone()
}

fn code(&self) -> RustPrinter {
line(unit() + self.verbosity.render() + "mod " + escape_keywords(&self.name) + ";")
}

pub fn new<S: Into<String>>(s: S) -> ModuleName {
pub fn new(s: impl AsRef<str>) -> ModuleName {
ModuleName {
name: s.into(),
name: Self::escape_type_params(s.as_ref()).to_case(Case::Snake),
verbosity: Verbosity::Default,
}
}

pub fn new_pub<S: Into<String>>(s: S) -> ModuleName {
pub fn new_pub(s: impl AsRef<str>) -> ModuleName {
ModuleName {
name: s.into(),
name: Self::escape_type_params(s.as_ref()).to_case(Case::Snake),
verbosity: Verbosity::Pub,
}
}

pub fn file_name(&self) -> String {
format!("{}.rs", &self.name)
}

fn escape_type_params(s: &str) -> String {
s.replace("<", "_")
.replace(",", "_")
.replace(">", "_")
.replace(" ", "")
}
}

#[derive(Debug, Clone)]
Expand Down
8 changes: 5 additions & 3 deletions src/rust/model_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ pub fn model_gen(
"Unexpected reference format: {reference}."
)))?;

let name = original_name.to_case(Case::UpperCamel);
let mod_name = ModuleName::new(original_name);
let name = mod_name.name().to_case(Case::UpperCamel);

let schema = schemas.get(original_name).ok_or(Error::unexpected(format!(
"Can't find schema by reference {original_name}"
Expand Down Expand Up @@ -584,10 +585,11 @@ pub fn model_gen(
}
};

let name = ModuleName::new(name);
Ok(Module {
def: ModuleDef {
name: ModuleName::new(name.to_case(Case::Snake)),
exports: vec![name],
name: name.clone(),
exports: vec![name.name().to_case(Case::Pascal)],
},
code: RustContext::new().print_to_string(code?),
})
Expand Down
4 changes: 3 additions & 1 deletion src/rust/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use crate::printer::TreePrinter;
use crate::rust::lib_gen::ModuleName;
use crate::rust::model_gen::RefCache;
use crate::rust::printer::{rust_name, unit, RustContext};
use crate::{Error, Result};
Expand Down Expand Up @@ -120,7 +121,8 @@ impl DataType {
}
}
DataType::Model(ModelType { name }) => {
let model_type = rust_name("crate::model", name);
let name = ModuleName::new(name);
let model_type = rust_name("crate::model", &name.name().to_case(Case::Pascal));
to_ref(model_type, top_param)
}
DataType::Array(item) => {
Expand Down

0 comments on commit e8f0242

Please sign in to comment.