Skip to content

Commit 86a1c66

Browse files
committed
refactor(stackable-versioned): Split utils into separate files
1 parent 3a10e4d commit 86a1c66

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub trait DocComments {
2+
/// Converts lines of doc-comments into a trimmed list which can be expanded via repetition in
3+
/// [`quote::quote`].
4+
fn into_doc_comments(self) -> Vec<String>;
5+
}
6+
7+
impl DocComments for &str {
8+
fn into_doc_comments(self) -> Vec<String> {
9+
self
10+
// Trim the leading and trailing whitespace, deleting superfluous
11+
// empty lines.
12+
.trim()
13+
.lines()
14+
// Trim the leading and trailing whitespace on each line that can be
15+
// introduced when the developer indents multi-line comments.
16+
.map(|line| line.trim().to_owned())
17+
.collect()
18+
}
19+
}
20+
21+
impl DocComments for Option<&str> {
22+
fn into_doc_comments(self) -> Vec<String> {
23+
self.map_or(vec![], |s| s.into_doc_comments())
24+
}
25+
}

crates/stackable-versioned-macros/src/utils.rs renamed to crates/stackable-versioned-macros/src/utils/mod.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,27 @@ use std::ops::Deref;
33
use convert_case::{Case, Casing};
44
use darling::util::IdentString;
55
use k8s_version::Version;
6+
use proc_macro2::Span;
67
use quote::{ToTokens, format_ident};
7-
use syn::{Ident, spanned::Spanned};
8+
use syn::{Ident, Path, spanned::Spanned};
9+
10+
pub mod doc_comments;
811

912
pub trait VersionExt {
1013
fn as_variant_ident(&self) -> IdentString;
14+
fn as_module_ident(&self) -> IdentString;
1115
}
1216

1317
impl VersionExt for Version {
1418
fn as_variant_ident(&self) -> IdentString {
15-
format_ident!("{ident}", ident = self.to_string().to_case(Case::Pascal)).into()
19+
IdentString::new(Ident::new(
20+
&self.to_string().to_case(Case::Pascal),
21+
Span::call_site(),
22+
))
23+
}
24+
25+
fn as_module_ident(&self) -> IdentString {
26+
IdentString::new(Ident::new(&self.to_string(), Span::call_site()))
1627
}
1728
}
1829

@@ -22,7 +33,7 @@ pub trait ContainerIdentExt {
2233
fn as_cleaned_kubernetes_ident(&self) -> IdentString;
2334

2435
/// Transforms the [`IdentString`] into one usable in the [`From`] impl.
25-
fn as_from_impl_ident(&self) -> IdentString;
36+
fn as_parameter_ident(&self) -> IdentString;
2637
}
2738

2839
impl ContainerIdentExt for Ident {
@@ -31,12 +42,22 @@ impl ContainerIdentExt for Ident {
3142
IdentString::new(ident)
3243
}
3344

34-
fn as_from_impl_ident(&self) -> IdentString {
45+
fn as_parameter_ident(&self) -> IdentString {
3546
let ident = format_ident!("__sv_{}", self.to_string().to_lowercase());
3647
IdentString::new(ident)
3748
}
3849
}
3950

51+
impl ContainerIdentExt for IdentString {
52+
fn as_cleaned_kubernetes_ident(&self) -> IdentString {
53+
self.as_ident().as_cleaned_kubernetes_ident()
54+
}
55+
56+
fn as_parameter_ident(&self) -> IdentString {
57+
self.as_ident().as_parameter_ident()
58+
}
59+
}
60+
4061
pub trait ItemIdentExt: Deref<Target = IdentString> + From<Ident> + Spanned {
4162
const DEPRECATED_PREFIX: &'static str;
4263

@@ -115,3 +136,17 @@ impl ToTokens for VariantIdent {
115136
self.0.to_tokens(tokens);
116137
}
117138
}
139+
140+
pub fn path_to_string(path: &Path) -> String {
141+
let pretty_path = path
142+
.segments
143+
.iter()
144+
.map(|s| s.ident.to_string())
145+
.collect::<Vec<String>>()
146+
.join("::");
147+
148+
match path.leading_colon {
149+
Some(_) => format!("::{}", pretty_path),
150+
None => pretty_path,
151+
}
152+
}

0 commit comments

Comments
 (0)