1use anyhow::{anyhow, Result};
2use log::info;
3use pulldown_cmark::Tag;
4
5use crate::constant::CUSTOM_PREFIX_KEY;
6
7pub fn cmarktag_stringify(tag: &Tag<'_>) -> Option<String> {
8 match tag {
9 Tag::Heading(idx) => Some(format!("Heading{}", idx)),
10 Tag::List(_) => Some("List".to_string()),
11 _ => None,
12 }
13}
14
15pub fn get_custom_prefix_key(prefix: &str) -> String {
16 format!("{} {}", CUSTOM_PREFIX_KEY.clone(), prefix)
17}
18
19pub fn get_custom_prefix_as_normal_list(prefix: &str) -> String {
20 format!("* !!!{}{}", CUSTOM_PREFIX_KEY.clone(), prefix)
21}
22
23pub fn custom_prefix_to_key(text_with_custom_prefix: Option<&str>) -> Option<String> {
24 if let Some(text) = text_with_custom_prefix {
25 if let Some(stripped) = text.strip_prefix(&format!("!!!{}", CUSTOM_PREFIX_KEY.clone())) {
26 if let Some(prefix) = stripped.chars().next() {
27 return Some(get_custom_prefix_key(&prefix.to_string()));
28 }
29 }
30 }
31 None
32}
33
34pub fn get_custom_prefix_end_idx() -> usize {
35 format!("!!!{}{}", CUSTOM_PREFIX_KEY.clone(), "a").len() + 1
36}
37
38pub fn get_output_filename(filename: &str) -> Result<&str> {
39 if filename.is_empty() {
40 Err(anyhow!("output filename is empty."))
41 } else {
42 let result = if let Some(stripped) = filename.strip_suffix(".xlsx") {
43 stripped
44 } else {
45 filename
46 };
47 info!("output filename without extension: {}", result);
48 Ok(result)
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_get_output_filename() {
58 assert_eq!("output", get_output_filename("output").unwrap());
59 assert_eq!("output", get_output_filename("output.xlsx").unwrap());
60 }
61
62 #[test]
63 fn test_get_output_filename_error() {
64 assert!(get_output_filename("").is_err());
65 }
66}