pub mod bash;
pub mod c_lang;
pub mod config;
pub mod cpp_lang;
pub mod dart;
pub mod go_lang;
pub mod haskell;
pub mod java_lang;
pub mod javascript;
pub mod kotlin;
pub mod ocaml;
pub mod python;
pub mod rust_lang;
pub mod scala;
pub mod swift;
pub mod typescript;
pub mod zsh;
use crate::import::ImportGroup;
pub trait CodeLang: std::fmt::Debug + 'static {
fn file_extension(&self) -> &str;
fn reserved_words(&self) -> &[&str];
fn render_imports(&self, imports: &ImportGroup) -> String;
fn render_string_literal(&self, s: &str) -> String;
fn render_doc_comment(&self, lines: &[&str]) -> String;
fn line_comment_prefix(&self) -> &str;
fn line_comment_suffix(&self) -> &str {
""
}
fn escape_reserved(&self, name: &str) -> String {
if self.reserved_words().contains(&name) {
format!("{name}_")
} else {
name.to_string()
}
}
fn render_visibility(
&self,
vis: crate::spec::modifiers::Visibility,
ctx: crate::spec::modifiers::DeclarationContext,
) -> &str;
fn function_keyword(&self, ctx: crate::spec::modifiers::DeclarationContext) -> &str;
fn type_keyword(&self, kind: crate::spec::modifiers::TypeKind) -> &str;
fn methods_inside_type_body(&self, kind: crate::spec::modifiers::TypeKind) -> bool;
fn qualify_import_name(&self, _module: &str, resolved_name: &str) -> String {
resolved_name.to_string()
}
fn module_separator(&self) -> Option<&str> {
None
}
fn type_kind_suffix(&self, _kind: crate::spec::modifiers::TypeKind) -> &str {
""
}
fn render_newtype_line(&self, vis: &str, name: &str, inner: &str) -> String {
format!("{vis}struct {name}({inner});")
}
fn fun_block_open(&self) -> &str {
" {"
}
fn type_header_block_open(&self, _kind: crate::spec::modifiers::TypeKind) -> &str {
" {"
}
fn doc_comment_inside_body(&self) -> bool {
false
}
fn doc_before_annotations(&self) -> bool {
true
}
fn optional_field_style(&self) -> crate::lang::config::OptionalFieldStyle {
crate::lang::config::OptionalFieldStyle::Ignored
}
fn property_style(&self) -> crate::spec::modifiers::PropertyStyle {
crate::spec::modifiers::PropertyStyle::Accessor
}
fn property_getter_keyword(&self) -> &str {
"get"
}
fn render_type_context(&self, _type_params: &[crate::spec::fun_spec::TypeParamSpec]) -> String {
String::new()
}
fn type_body_prefix(&self, _name: &str, _kind: crate::spec::modifiers::TypeKind) -> String {
String::new()
}
fn type_body_suffix(&self, _name: &str, _kind: crate::spec::modifiers::TypeKind) -> String {
String::new()
}
fn render_type_close_suffix(
&self,
_kind: crate::spec::modifiers::TypeKind,
_impl_types: &[String],
) -> String {
String::new()
}
fn render_type_param_kind(&self, _kind: &crate::spec::fun_spec::TypeParamKind) -> String {
String::new()
}
fn type_presentation(&self) -> config::TypePresentationConfig<'_> {
config::TypePresentationConfig::default()
}
fn generic_syntax(&self) -> config::GenericSyntaxConfig<'_> {
config::GenericSyntaxConfig::default()
}
fn block_syntax(&self) -> config::BlockSyntaxConfig<'_> {
config::BlockSyntaxConfig::default()
}
fn function_syntax(&self) -> config::FunctionSyntaxConfig<'_> {
config::FunctionSyntaxConfig::default()
}
fn type_decl_syntax(&self) -> config::TypeDeclSyntaxConfig<'_> {
config::TypeDeclSyntaxConfig::default()
}
fn enum_and_annotation(&self) -> config::EnumAndAnnotationConfig<'_> {
config::EnumAndAnnotationConfig::default()
}
}
pub(crate) fn module_to_alias(module: &str) -> String {
let last_segment = module
.rsplit(['/', ':', '.', '\\'])
.find(|s| !s.is_empty())
.unwrap_or(module);
let mut chars = last_segment.chars();
match chars.next() {
None => "Module".to_string(),
Some(first) => {
let upper: String = first.to_uppercase().collect();
format!("{upper}{}", chars.as_str())
}
}
}
pub fn lang_from_extension(ext: &str) -> Option<Box<dyn CodeLang>> {
match ext {
"ts" | "tsx" => Some(Box::new(typescript::TypeScript::default())),
"js" | "jsx" | "mjs" | "cjs" => Some(Box::new(javascript::JavaScript::default())),
"rs" => Some(Box::new(rust_lang::RustLang::default())),
"go" => Some(Box::new(go_lang::GoLang::default())),
"py" | "pyi" => Some(Box::new(python::Python::default())),
"java" => Some(Box::new(java_lang::JavaLang::default())),
"kt" | "kts" => Some(Box::new(kotlin::Kotlin::default())),
"swift" => Some(Box::new(swift::Swift::default())),
"dart" => Some(Box::new(dart::DartLang::default())),
"scala" | "sc" => Some(Box::new(scala::Scala::default())),
"hs" => Some(Box::new(haskell::Haskell::default())),
"ml" | "mli" => Some(Box::new(ocaml::OCaml::default())),
"c" | "h" => Some(Box::new(c_lang::CLang::default())),
"cpp" | "cxx" | "cc" | "hpp" | "hxx" => Some(Box::new(cpp_lang::CppLang::default())),
"sh" | "bash" => Some(Box::new(bash::Bash::default())),
"zsh" => Some(Box::new(zsh::Zsh::default())),
_ => None,
}
}