Skip to content

Commit

Permalink
Moved format_docstr fn to output module
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Sep 10, 2024
1 parent 2386b74 commit 14a0bb4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
mod analyzer;
pub use analyzer::{analyze, Config};
mod output;
pub use output::{Container, MapType, Member, Output};
pub use output::{format_docstr, Container, MapType, Member, Output};
mod derive;
pub use derive::Derive;
56 changes: 1 addition & 55 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::{CommandFactory, Parser, Subcommand};
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition, CustomResourceDefinitionVersion,
};
use kopium::{analyze, Config, Container, Derive, MapType};
use kopium::{analyze, format_docstr, Config, Container, Derive, MapType};
use kube::{api, core::Version, Api, Client, ResourceExt};
use quote::format_ident;

Expand Down Expand Up @@ -475,57 +475,3 @@ fn all_versions(crd: &CustomResourceDefinition) -> String {
vers.sort_by_cached_key(|v| std::cmp::Reverse(Version::parse(v).priority()));
vers.join(", ")
}

fn format_docstr(indent: &str, input: &str) -> String {
let rustdoc_code = "```\n";
let rustdoc_starts: Vec<usize> = input
.match_indices(rustdoc_code)
.step_by(2)
.map(|(index, _)| index)
.collect();
let mut cleaned_input = String::with_capacity(input.len());
let mut start = 0;
for doc in rustdoc_starts {
cleaned_input.push_str(&input[start..doc]);
cleaned_input.push_str("```text\n");
start = doc + rustdoc_code.len();
}
cleaned_input.push_str(&input[start..]);

// TODO: maybe logic to split doc strings by sentence / length here

format!(
"{}/// {}",
indent,
cleaned_input.replace('\n', &format!("\n{}/// ", indent))
)
}

#[cfg(test)]
mod test {
use crate::format_docstr;

#[test]
fn escapes_codes_from_descriptions() {
assert_eq!(
"/// ```text\n/// foobar\n/// ```\n/// ",
format_docstr("", "```\nfoobar\n```\n")
);
assert_eq!(
"/// Some docs\n/// ```text\n/// foobar\n/// ```\n/// ",
format_docstr("", "Some docs\n```\nfoobar\n```\n")
);
assert_eq!(
"/// Some docs\n/// ```text\n/// foobar\n/// ```",
format_docstr("", "Some docs\n```\nfoobar\n```")
);
assert_eq!(
"/// ```text\n/// foobar\n/// ```",
format_docstr("", "```\nfoobar\n```")
);
assert_eq!(
"/// Some docs\n/// with no code blocks!",
format_docstr("", "Some docs\nwith no code blocks!")
);
}
}
51 changes: 50 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,35 @@ impl MapType {
}
}

pub fn format_docstr(indent: &str, input: &str) -> String {
let rustdoc_code = "```\n";
let rustdoc_starts: Vec<usize> = input
.match_indices(rustdoc_code)
.step_by(2)
.map(|(index, _)| index)
.collect();
let mut cleaned_input = String::with_capacity(input.len());
let mut start = 0;
for doc in rustdoc_starts {
cleaned_input.push_str(&input[start..doc]);
cleaned_input.push_str("```text\n");
start = doc + rustdoc_code.len();
}
cleaned_input.push_str(&input[start..]);

// TODO: maybe logic to split doc strings by sentence / length here

format!(
"{}/// {}",
indent,
cleaned_input.replace('\n', &format!("\n{}/// ", indent))
)
}

// unit tests
#[cfg(test)]
mod test {
use super::{Container, Member};
use super::{format_docstr, Container, Member};
fn name_only_enum_member(name: &str) -> Member {
Member {
name: name.to_string(),
Expand Down Expand Up @@ -412,4 +437,28 @@ mod test {
assert!(containers[6].can_derive_default(&containers)); // ReferencesEnumVec
assert!(containers[7].can_derive_default(&containers)); // ReferencesEnumNestedOption
}

#[test]
fn escapes_codes_from_descriptions() {
assert_eq!(
"/// ```text\n/// foobar\n/// ```\n/// ",
format_docstr("", "```\nfoobar\n```\n")
);
assert_eq!(
"/// Some docs\n/// ```text\n/// foobar\n/// ```\n/// ",
format_docstr("", "Some docs\n```\nfoobar\n```\n")
);
assert_eq!(
"/// Some docs\n/// ```text\n/// foobar\n/// ```",
format_docstr("", "Some docs\n```\nfoobar\n```")
);
assert_eq!(
"/// ```text\n/// foobar\n/// ```",
format_docstr("", "```\nfoobar\n```")
);
assert_eq!(
"/// Some docs\n/// with no code blocks!",
format_docstr("", "Some docs\nwith no code blocks!")
);
}
}

0 comments on commit 14a0bb4

Please sign in to comment.