Skip to content

Commit

Permalink
move derive_dimension to codegen, move to_snakcase out to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Dec 31, 2023
1 parent 51d2c7a commit 6b76943
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
use quote::quote;
use syn::Ident;

use crate::types::Defs;

fn str_to_snakecase(s: &str) -> String {
let s = s.chars().rev().collect::<String>();
let words = s.split_inclusive(|c: char| c.is_uppercase());
words
.map(|word| word.chars().rev().collect::<String>().to_lowercase())
.rev()
.collect::<Vec<_>>()
.join("_")
}

pub fn to_snakecase(dim: &proc_macro2::Ident) -> Ident {
let snake_case = str_to_snakecase(&dim.to_string());
Ident::new(&snake_case, dim.span())
}

impl Defs {
pub(crate) fn dimension_impl(&self) -> proc_macro::TokenStream {
let name = &self.dimension_type;
Expand Down Expand Up @@ -216,16 +200,3 @@ impl Defs {
}
}
}

#[cfg(test)]
mod tests {
#[test]
fn str_to_snakecase() {
assert_eq!(super::str_to_snakecase("MyType"), "my_type".to_owned());
assert_eq!(super::str_to_snakecase("My"), "my".to_owned());
assert_eq!(
super::str_to_snakecase("MyVeryLongType"),
"my_very_long_type".to_owned()
);
}
}
1 change: 1 addition & 0 deletions crates/diman_unit_system/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod base_dimension_type;
mod debug;
mod dimension_def;
mod float_methods;
mod generic_methods;
#[cfg(feature = "hdf5")]
Expand Down
2 changes: 1 addition & 1 deletion crates/diman_unit_system/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![feature(proc_macro_diagnostic)]

mod codegen;
mod derive_dimension;
mod dimension_math;
mod expression;
mod parse;
mod prefixes;
mod resolve;
mod storage_types;
mod to_snakecase;
mod types;

use proc_macro2::TokenStream;
Expand Down
29 changes: 29 additions & 0 deletions crates/diman_unit_system/src/to_snakecase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use proc_macro2::Ident;

fn str_to_snakecase(s: &str) -> String {
let s = s.chars().rev().collect::<String>();
let words = s.split_inclusive(|c: char| c.is_uppercase());
words
.map(|word| word.chars().rev().collect::<String>().to_lowercase())
.rev()
.collect::<Vec<_>>()
.join("_")
}

pub fn to_snakecase(dim: &Ident) -> Ident {
let snake_case = str_to_snakecase(&dim.to_string());
Ident::new(&snake_case, dim.span())
}

#[cfg(test)]
mod tests {
#[test]
fn str_to_snakecase() {
assert_eq!(super::str_to_snakecase("MyType"), "my_type".to_owned());
assert_eq!(super::str_to_snakecase("My"), "my".to_owned());
assert_eq!(
super::str_to_snakecase("MyVeryLongType"),
"my_very_long_type".to_owned()
);
}
}
2 changes: 1 addition & 1 deletion crates/diman_unit_system/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use proc_macro2::Span;
use syn::*;

use crate::{
derive_dimension::to_snakecase,
dimension_math::BaseDimensions,
expression::{self, BinaryOperator, Expr, Operator},
parse::One,
prefixes::Prefix,
to_snakecase::to_snakecase,
};

pub type IntExponent = i32;
Expand Down

0 comments on commit 6b76943

Please sign in to comment.