Skip to content

Commit e483b37

Browse files
committed
feat: create misc folder with load_text function
1 parent ea37c54 commit e483b37

File tree

6 files changed

+26
-31
lines changed

6 files changed

+26
-31
lines changed

Diff for: day04a/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
misc = { path = "../misc" }

Diff for: day04a/src/main.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::fs;
2-
1+
use misc::load_text;
32

43
fn scratchcards(text: String) -> u32{
54
let mut res = 0;
@@ -26,20 +25,6 @@ fn scratchcards(text: String) -> u32{
2625
return res;
2726
}
2827

29-
30-
fn load_text(path_file: String) -> String {
31-
let file_path: String = String::from(path_file);
32-
let contents = fs::read_to_string(file_path);
33-
34-
let mut text: String = String::from("");
35-
match contents {
36-
Ok(v) => text = v,
37-
Err(e) => println!("error parsing header: {e:?}"),
38-
}
39-
40-
return text;
41-
}
42-
4328
fn main() {
4429
let test_text: String = load_text(String::from("test.txt"));
4530
let test_value = scratchcards(test_text);

Diff for: day04b/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
misc = { path = "../misc" }

Diff for: day04b/src/main.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fs;
1+
use misc::load_text;
22
use std::collections::HashMap;
33

44
fn _scratchcards(text: &str, card_number: u32) -> u32{
@@ -51,20 +51,6 @@ fn scratchcards(text: &str) -> u32{
5151
return res;
5252
}
5353

54-
55-
fn load_text(path_file: String) -> String {
56-
let file_path: String = String::from(path_file);
57-
let contents = fs::read_to_string(file_path);
58-
59-
let mut text: String = String::from("");
60-
match contents {
61-
Ok(v) => text = v,
62-
Err(e) => println!("error parsing header: {e:?}"),
63-
}
64-
65-
return text;
66-
}
67-
6854
fn main() {
6955
let test_text: String = load_text(String::from("test.txt"));
7056
let test_value = scratchcards(test_text.as_str());

Diff for: misc/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "misc"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

Diff for: misc/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::fs;
2+
3+
pub fn load_text(path_file: String) -> String {
4+
let file_path: String = String::from(path_file);
5+
let contents = fs::read_to_string(file_path);
6+
7+
let mut text: String = String::from("");
8+
match contents {
9+
Ok(v) => text = v,
10+
Err(e) => println!("error parsing header: {e:?}"),
11+
}
12+
13+
return text;
14+
}

0 commit comments

Comments
 (0)