gitignore_template_generator/
helper.rs1use std::{borrow::Cow, ffi::OsString, fs};
6
7use crate::constant;
8
9pub struct CliOptionName {
10 pub short: char,
11 pub long: &'static str,
12}
13
14pub fn load_expectation_file_as_string(expectation_file_name: &str) -> String {
15 let expectation_file_path = format!(
16 "{}/{}/{expectation_file_name}.txt",
17 env!("CARGO_MANIFEST_DIR"),
18 constant::path::TEST_EXPECTATIONS
19 );
20
21 fs::read_to_string(expectation_file_path)
22 .expect(constant::error_messages::FILE_READ_TO_STRING_FAILURE)
23}
24
25pub fn parse_bytes(bytes: &[u8]) -> Cow<str> {
26 String::from_utf8_lossy(bytes)
27}
28
29pub fn parse_pos_args(pos_args: &str) -> Vec<&str> {
30 pos_args.split_whitespace().collect()
31}
32
33pub fn parse_cli_args(pos_args: &str) -> Vec<OsString> {
34 format!("{} {pos_args}", env!("CARGO_PKG_NAME"))
35 .split_whitespace()
36 .map(OsString::from)
37 .collect()
38}
39
40pub fn make_string_vec(values: &str) -> Vec<String> {
41 values.split_whitespace().map(String::from).collect()
42}
43
44pub fn get_help_message() -> String {
45 get_help_message_for("help_message")
46}
47
48pub fn get_ansi_help_message() -> String {
49 get_help_message_for("ansi_help_message")
50}
51
52pub fn get_help_message_for(template_name: &str) -> String {
53 load_expectation_file_as_string(template_name)
54 .replace("{pkg_name}", env!("CARGO_PKG_NAME"))
55 .replace("{about}", constant::parser_infos::ABOUT)
56 .replace(
57 "{template_names_desc}",
58 constant::help_messages::TEMPLATE_NAMES,
59 )
60 .replace("{author_desc}", constant::help_messages::AUTHOR)
61 .replace("{server_url_desc}", constant::help_messages::SERVER_URL)
62 .replace("{help_desc}", constant::help_messages::HELP)
63 .replace("{version_desc}", constant::help_messages::VERSION)
64 .replace("{version}", env!("CARGO_PKG_VERSION"))
65 .replace("{author}", env!("CARGO_PKG_AUTHORS"))
66 .replace(
67 "{author_short}",
68 constant::cli_options::AUTHOR.short.to_string().as_str(),
69 )
70 .replace("{author_long}", constant::cli_options::AUTHOR.long)
71 .replace(
72 "{server_url_short}",
73 constant::cli_options::SERVER_URL.short.to_string().as_str(),
74 )
75 .replace("{server_url_long}", constant::cli_options::SERVER_URL.long)
76 .replace(
77 "{help_short}",
78 constant::cli_options::HELP.short.to_string().as_str(),
79 )
80 .replace("{help_long}", constant::cli_options::HELP.long)
81 .replace(
82 "{version_short}",
83 constant::cli_options::VERSION.short.to_string().as_str(),
84 )
85 .replace("{version_long}", constant::cli_options::VERSION.long)
86 .replace("{server_url_default}", constant::template_manager::BASE_URL)
87 .replace(
88 "{generator_uri_short}",
89 constant::cli_options::GENERATOR_URI
90 .short
91 .to_string()
92 .as_str(),
93 )
94 .replace(
95 "{generator_uri_long}",
96 constant::cli_options::GENERATOR_URI.long,
97 )
98 .replace(
99 "{generator_uri_desc}",
100 constant::help_messages::GENERATOR_URI,
101 )
102 .replace(
103 "{generator_uri_default}",
104 constant::template_manager::GENERATOR_URI,
105 )
106 .replace(
107 "{list_short}",
108 constant::cli_options::LIST.short.to_string().as_str(),
109 )
110 .replace("{list_long}", constant::cli_options::LIST.long)
111 .replace("{list_desc}", constant::help_messages::LIST)
112 .replace(
113 "{lister_uri_short}",
114 constant::cli_options::LISTER_URI.short.to_string().as_str(),
115 )
116 .replace("{lister_uri_long}", constant::cli_options::LISTER_URI.long)
117 .replace("{lister_uri_desc}", constant::help_messages::LISTER_URI)
118 .replace(
119 "{lister_uri_default}",
120 constant::template_manager::LISTER_URI,
121 )
122 .replace(
123 "{check_short}",
124 constant::cli_options::CHECK.short.to_string().as_str(),
125 )
126 .replace("{check_long}", constant::cli_options::CHECK.long)
127 .replace("{check_desc}", constant::help_messages::CHECK)
128 .replace(
129 "{timeout_short}",
130 constant::cli_options::TIMEOUT.short.to_string().as_str(),
131 )
132 .replace("{timeout_long}", constant::cli_options::TIMEOUT.long)
133 .replace("{timeout_desc}", constant::help_messages::TIMEOUT)
134 .replace(
135 "{timeout_default}",
136 format!(
137 "{}s/{}ms",
138 constant::template_manager::TIMEOUT,
139 constant::template_manager::TIMEOUT_MILLISECOND
140 )
141 .as_str(),
142 )
143 .replace(
144 "{timeout_unit_short}",
145 constant::cli_options::TIMEOUT_UNIT
146 .short
147 .to_string()
148 .as_str(),
149 )
150 .replace(
151 "{timeout_unit_long}",
152 constant::cli_options::TIMEOUT_UNIT.long,
153 )
154 .replace("{timeout_unit_desc}", constant::help_messages::TIMEOUT_UNIT)
155 .replace(
156 "{timeout_unit_default}",
157 constant::template_manager::TIMEOUT_UNIT,
158 )
159 .replace("{timeout_unit_values}", "millisecond, second")
160}