gitignore_template_generator/
helper.rs1use std::{borrow::Cow, ffi::OsString, fs};
2
3use crate::constant;
4
5pub struct CliOptionName {
6 pub short: char,
7 pub long: &'static str,
8}
9
10pub fn load_expectation_file_as_string(expectation_file_name: &str) -> String {
11 let expectation_file_path = format!(
12 "{}/{}/{expectation_file_name}.txt",
13 env!("CARGO_MANIFEST_DIR"),
14 constant::path::TEST_EXPECTATIONS
15 );
16
17 fs::read_to_string(expectation_file_path)
18 .expect(constant::error_messages::FILE_READ_TO_STRING_FAILURE)
19}
20
21pub fn parse_bytes(bytes: &[u8]) -> Cow<str> {
22 String::from_utf8_lossy(bytes)
23}
24
25pub fn parse_pos_args(pos_args: &str) -> Vec<&str> {
26 pos_args.split_whitespace().collect()
27}
28
29pub fn parse_cli_args(pos_args: &str) -> Vec<OsString> {
30 format!("{} {pos_args}", env!("CARGO_PKG_NAME"))
31 .split_whitespace()
32 .map(OsString::from)
33 .collect()
34}
35
36pub fn make_string_vec(values: &str) -> Vec<String> {
37 values.split_whitespace().map(String::from).collect()
38}
39
40pub fn get_help_message() -> String {
41 get_help_message_for("help_message")
42}
43
44pub fn get_ansi_help_message() -> String {
45 get_help_message_for("ansi_help_message")
46}
47
48pub fn get_help_message_for(template_name: &str) -> String {
49 load_expectation_file_as_string(template_name)
50 .replace("{pkg_name}", env!("CARGO_PKG_NAME"))
51 .replace("{about}", constant::parser_infos::ABOUT)
52 .replace(
53 "{template_names_desc}",
54 constant::help_messages::TEMPLATE_NAMES,
55 )
56 .replace("{author_desc}", constant::help_messages::AUTHOR)
57 .replace("{server_url_desc}", constant::help_messages::SERVER_URL)
58 .replace("{help_desc}", constant::help_messages::HELP)
59 .replace("{version_desc}", constant::help_messages::VERSION)
60 .replace("{version}", env!("CARGO_PKG_VERSION"))
61 .replace("{author}", env!("CARGO_PKG_AUTHORS"))
62 .replace(
63 "{author_short}",
64 constant::cli_options::AUTHOR.short.to_string().as_str(),
65 )
66 .replace("{author_long}", constant::cli_options::AUTHOR.long)
67 .replace(
68 "{server_url_short}",
69 constant::cli_options::SERVER_URL.short.to_string().as_str(),
70 )
71 .replace("{server_url_long}", constant::cli_options::SERVER_URL.long)
72 .replace(
73 "{help_short}",
74 constant::cli_options::HELP.short.to_string().as_str(),
75 )
76 .replace("{help_long}", constant::cli_options::HELP.long)
77 .replace(
78 "{version_short}",
79 constant::cli_options::VERSION.short.to_string().as_str(),
80 )
81 .replace("{version_long}", constant::cli_options::VERSION.long)
82 .replace(
83 "{server_url_default}",
84 constant::template_generator::BASE_URL,
85 )
86}