Skip to content

Commit ee7929e

Browse files
committed
Refactor code a bit
1 parent 1b3cdee commit ee7929e

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

bin/generate_practice_exercise

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,9 @@ message "info" "The exercise difficulty has been set to ${YELLOW}${EXERCISE_DIFF
6060
AUTHOR_HANDLE=${3:-$(get_author_handle)}
6161
message "info" "Using ${YELLOW}${AUTHOR_HANDLE}${BLUE} as author's handle. You can edit this later in the 'authors' field in the ${EXERCISE_DIR}/.meta/config.json file"
6262

63-
message "info" "Creating Rust files"
64-
cargo new --lib "$EXERCISE_DIR" -q
65-
mkdir -p "$EXERCISE_DIR"/tests
66-
touch "${EXERCISE_DIR}/tests/${SLUG}.rs"
6763

68-
create_test_file_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
69-
create_lib_rs_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
70-
create_example_rs_template "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
71-
overwrite_gitignore "$EXERCISE_DIR"
64+
create_rust_files "$EXERCISE_DIR" "$SLUG" "$HAS_CANONICAL_DATA"
7265

73-
message "success" "Created Rust files succesfully!"
7466

7567
# ==================================================
7668

bin/generator-utils/templates.sh

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
# shellcheck source=/dev/null
44
source ./bin/generator-utils/utils.sh
55

6+
function create_fn_name() {
7+
slug=$1
8+
has_canonical_data=$2
9+
10+
if [ "$has_canonical_data" == false ]; then
11+
fn_name=$(dash_to_underscore "$slug")
12+
else
13+
fn_name=$(jq -r 'first(.. | .property? // empty)' canonical_data.json)
14+
fi
15+
16+
echo "$fn_name"
17+
18+
}
19+
620
function create_test_file_template() {
721
local exercise_dir=$1
822
local slug=$2
@@ -49,7 +63,7 @@ fn ${desc}() {
4963
let expected = ${expected};
5064
5165
// TODO: Add assertion
52-
assert_eq!(${fn_name}(input), expected)
66+
assert_eq!(${fn_name}(input), expected);
5367
}
5468
5569
EOT
@@ -66,8 +80,8 @@ function create_lib_rs_template() {
6680
local has_canonical_data=$3
6781
fn_name=$(create_fn_name "$slug" "$has_canonical_data")
6882
cat <<EOT >"${exercise_dir}/src/lib.rs"
69-
pub fn $(dash_to_underscore "$slug")() {
70-
unimplemented!("implement ${slug} exercise")
83+
pub fn ${fn_name}() {
84+
unimplemented!("implement ${slug} exercise");
7185
}
7286
EOT
7387
message "success" "Stub file for lib.rs has been created!"
@@ -99,23 +113,28 @@ function create_example_rs_template() {
99113
cat <<EOT >"${exercise_dir}/.meta/example.rs"
100114
pub fn ${fn_name}() {
101115
// TODO: Create a solution that passes all the tests
102-
unimplemented!("implement ${slug} exercise")
116+
unimplemented!("implement ${slug} exercise");
103117
}
104118
105119
EOT
106120
message "success" "Stub file for example.rs has been created!"
107121
}
108122

109-
function create_fn_name() {
110-
slug=$1
111-
has_canonical_data=$2
123+
function create_rust_files() {
124+
local exercise_dir=$1
125+
local slug=$2
126+
local has_canonical_data=$3
112127

113-
if [ "$has_canonical_data" == true ]; then
114-
fn_name=$(dash_to_underscore "$slug")
115-
else
116-
fn_name=$(jq -r 'first(.. | .property? // empty)' canonical_data.json)
117-
fi
128+
message "info" "Creating Rust files"
129+
cargo new --lib "$exercise_dir" -q
130+
mkdir -p "$exercise_dir"/tests
131+
touch "${exercise_dir}/tests/${slug}.rs"
118132

119-
echo "$fn_name"
133+
create_test_file_template "$exercise_dir" "$slug" "$has_canonical_data"
134+
create_lib_rs_template "$exercise_dir" "$slug" "$has_canonical_data"
135+
create_example_rs_template "$exercise_dir" "$slug" "$has_canonical_data"
136+
overwrite_gitignore "$exercise_dir"
137+
138+
message "success" "Created Rust files succesfully!"
120139

121140
}

0 commit comments

Comments
 (0)