Skip to main content

olai_codegen/
utils.rs

1//! Shared utilities for the proto-gen crate
2//!
3//! This module contains common functions used across different parts of the code generation
4//! pipeline to reduce duplication and improve maintainability.
5use convert_case::{Case, Casing};
6
7/// Extract the last dot-separated segment of a fully-qualified protobuf type name.
8///
9/// For example, `".unitycatalog.catalog.v1.Catalog"` returns `"Catalog"`.
10/// If the name contains no dots, the original string is returned unchanged.
11pub fn extract_simple_type_name(name: &str) -> String {
12    name.split('.').next_back().unwrap_or(name).to_string()
13}
14
15/// String manipulation utilities
16pub mod strings {
17    use super::*;
18
19    /// Convert service name to handler trait name
20    /// e.g., "CatalogsService" -> "CatalogHandler"
21    pub fn service_to_handler_name(service_name: &str) -> String {
22        if let Some(base) = service_name.strip_suffix("Service") {
23            format!("{}Handler", base.trim_end_matches('s'))
24        } else {
25            format!("{}Handler", service_name)
26        }
27    }
28
29    /// Convert operation ID to handler method name
30    /// e.g., "ListCatalogs" -> "list_catalogs"
31    pub fn operation_to_method_name(operation_id: &str) -> String {
32        operation_id.to_case(Case::Snake)
33    }
34
35    /// Extract base path from service name
36    /// e.g., "CatalogsService" -> "catalogs"
37    pub fn service_to_base_path(service_name: &str) -> String {
38        if let Some(base) = service_name.strip_suffix("Service") {
39            base.to_case(Case::Snake)
40        } else {
41            service_name.to_case(Case::Snake)
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    mod string_tests {
51        use super::*;
52
53        #[test]
54        fn test_service_to_handler_name() {
55            assert_eq!(
56                strings::service_to_handler_name("CatalogsService"),
57                "CatalogHandler"
58            );
59            assert_eq!(
60                strings::service_to_handler_name("RecipientsService"),
61                "RecipientHandler"
62            );
63            assert_eq!(
64                strings::service_to_handler_name("SchemasService"),
65                "SchemaHandler"
66            );
67        }
68
69        #[test]
70        fn test_operation_to_method_name() {
71            assert_eq!(
72                strings::operation_to_method_name("ListCatalogs"),
73                "list_catalogs"
74            );
75            assert_eq!(
76                strings::operation_to_method_name("CreateCatalog"),
77                "create_catalog"
78            );
79            assert_eq!(
80                strings::operation_to_method_name("GetCatalog"),
81                "get_catalog"
82            );
83        }
84
85        #[test]
86        fn test_service_to_base_path() {
87            assert_eq!(strings::service_to_base_path("CatalogsService"), "catalogs");
88            assert_eq!(
89                strings::service_to_base_path("RecipientsService"),
90                "recipients"
91            );
92        }
93    }
94}