Skip to main content
Core utilities and abstractions used across the Bomboni project. This crate provides fundamental building blocks for string manipulation and syntax tree processing.

Features

  • String Manipulation: Case conversion utilities for various naming conventions
  • Syntax Processing: Helper utilities for working with Rust syntax trees
  • Proc Macro Support: Tools for building procedural macros

Installation

Add bomboni_core to your Cargo.toml:
[dependencies]
bomboni_core = "*"

String Utilities

The string module provides utilities for converting strings between different case conventions.

Case Conversion

Convert strings to various case formats:
use bomboni_core::string::{str_to_case, Case};

// Convert to snake_case
let snake = str_to_case("MyVariableName", Case::Snake);
assert_eq!(snake, "my_variable_name");

// Convert to camelCase
let camel = str_to_case("my_variable_name", Case::Camel);
assert_eq!(camel, "myVariableName");

// Convert to PascalCase
let pascal = str_to_case("my_variable_name", Case::Pascal);
assert_eq!(pascal, "MyVariableName");

// Convert to kebab-case
let kebab = str_to_case("MyVariableName", Case::Kebab);
assert_eq!(kebab, "my-variable-name");

// Convert to SCREAMING_SNAKE_CASE
let screaming = str_to_case("myVariableName", Case::ScreamingSnake);
assert_eq!(screaming, "MY_VARIABLE_NAME");

Supported Case Types

The Case enum from the convert_case crate provides many case conversion options:

Snake Case

my_variable_name

Camel Case

myVariableName

Pascal Case

MyVariableName

Kebab Case

my-variable-name

Screaming Snake

MY_VARIABLE_NAME

Title Case

My Variable Name

Boundary Detection

The string conversion utilities intelligently detect word boundaries based on:
  • Underscores (_)
  • Hyphens (-)
  • Spaces
  • Lower-to-upper case transitions
  • Acronyms
  • Upper-to-digit transitions
  • Digit-to-upper transitions
  • Digit-to-lower transitions
This allows seamless conversion between different naming conventions:
use bomboni_core::string::{str_to_case, Case};

// Handles complex identifiers
let input = "HTTPSConnection2FA";
let snake = str_to_case(input, Case::Snake);
assert_eq!(snake, "https_connection_2fa");

Syntax Processing Utilities

The syn module provides utilities for working with Rust syntax trees in procedural macros.

Type Checking Utilities

Check if a type matches common patterns:
use bomboni_core::syn::{type_is_phantom, type_is_option};
use syn::{parse_quote, Type};

// Check if a type is PhantomData
let ty: Type = parse_quote!(PhantomData<T>);
assert!(type_is_phantom(&ty));

let ty: Type = parse_quote!(std::marker::PhantomData<T>);
assert!(type_is_phantom(&ty));

// Check if a type is Option
let ty: Type = parse_quote!(Option<String>);
assert!(type_is_option(&ty));

let ty: Type = parse_quote!(std::option::Option<T>);
assert!(type_is_option(&ty));
The type checking functions only examine the last segment of the type path, so they work with both fully qualified and simple type names.

Documentation Comment Formatting

The format_comment! macro helps generate documentation comments in procedural macros:
use bomboni_core::format_comment;
use quote::quote;

// Generate formatted doc comments
let field_name = "user_id";
let doc = format_comment!("The {} field", field_name);

let output = quote! {
    #doc
    pub user_id: i64,
};

// Expands to:
// #[doc = " The user_id field"]
// pub user_id: i64,
This is particularly useful when building derive macros that need to generate documentation:
use bomboni_core::format_comment;
use quote::quote;

let struct_name = "User";
let method_name = "validate";

let impl_block = quote! {
    #(format_comment!("Implementation for {}", struct_name))
    impl #struct_name {
        #(format_comment!("Validates the {} instance", struct_name))
        pub fn #method_name(&self) -> Result<(), Error> {
            // validation logic
            Ok(())
        }
    }
};

Usage in Procedural Macros

These utilities are designed to be used in procedural macros:
use bomboni_core::string::{str_to_case, Case};
use bomboni_core::syn::{type_is_option, type_is_phantom};
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Data, Fields};

#[proc_macro_derive(MyDerive)]
pub fn derive_my_trait(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    
    // Convert struct name to different cases
    let snake_name = str_to_case(name.to_string(), Case::Snake);
    let const_name = str_to_case(name.to_string(), Case::ScreamingSnake);
    
    // Process fields
    if let Data::Struct(data) = &input.data {
        if let Fields::Named(fields) = &data.fields {
            for field in &fields.named {
                let ty = &field.ty;
                
                // Skip PhantomData fields
                if type_is_phantom(ty) {
                    continue;
                }
                
                // Handle optional fields differently
                if type_is_option(ty) {
                    // Special handling for Option<T>
                }
            }
        }
    }
    
    let expanded = quote! {
        // Generated implementation
    };
    
    TokenStream::from(expanded)
}

API Reference

String Module

str_to_case
function
Converts a string to the specified case format.Parameters:
  • s: impl AsRef<str> - The string to convert
  • case: Case - The target case format
Returns: Converted string in the specified case
Case
enum
Enum representing different case formats. Re-exported from convert_case crate.Common variants:
  • Case::Snake - snake_case
  • Case::Camel - camelCase
  • Case::Pascal - PascalCase
  • Case::Kebab - kebab-case
  • Case::ScreamingSnake - SCREAMING_SNAKE_CASE
  • Case::Title - Title Case

Syn Module

type_is_phantom
function
Checks if a type is PhantomData.Parameters:
  • ty: &Type - The type to check
Returns: true if the type is PhantomData
type_is_option
function
Checks if a type is Option.Parameters:
  • ty: &Type - The type to check
Returns: true if the type is Option
format_comment!
macro
Generates a formatted documentation comment for use in procedural macros.Syntax: format_comment!($fmt, $($arg)*)Returns: Token stream containing #[doc = "..."] attribute

bomboni_common

Common utilities and types

bomboni_macros

Convenient utility macros

Build docs developers (and LLMs) love