From 14a0bb444ad6052734f287e0072d490579738bfa Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Tue, 10 Sep 2024 16:14:02 -0400 Subject: [PATCH] Moved format_docstr fn to output module Signed-off-by: Alex Snaps --- src/lib.rs | 2 +- src/main.rs | 56 +-------------------------------------------------- src/output.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6c1b20f..28b01f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index aa930f2..bd9798a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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 = 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!") - ); - } -} diff --git a/src/output.rs b/src/output.rs index 507dee8..a2374f2 100644 --- a/src/output.rs +++ b/src/output.rs @@ -255,10 +255,35 @@ impl MapType { } } +pub fn format_docstr(indent: &str, input: &str) -> String { + let rustdoc_code = "```\n"; + let rustdoc_starts: Vec = 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(), @@ -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!") + ); + } }