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(
87 "{server_url_default}",
88 constant::template_generator::BASE_URL,
89 )
90 .replace(
91 "{endpoint_uri_short}",
92 constant::cli_options::ENDPOINT_URI
93 .short
94 .to_string()
95 .as_str(),
96 )
97 .replace(
98 "{endpoint_uri_long}",
99 constant::cli_options::ENDPOINT_URI.long,
100 )
101 .replace("{endpoint_uri_default}", constant::template_generator::URI)
102}