Skip to content

Commit 0208dc7

Browse files
author
Gonzalo Diaz
committed
New utility function to load JSON
1 parent 4658760 commit 0208dc7

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

Cargo.lock

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ name = "exercises"
88
crate-type = ["rlib", "cdylib"]
99

1010
[dependencies]
11+
serde_json = "1.0"
12+
serde = { version = "1.0", features = ["derive"] }
13+
once_cell = "1.21.3"

tests/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod utils;

tests/common/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use serde::de::DeserializeOwned;
2+
use std::fs;
3+
4+
/// Carga un archivo JSON en un `Vec<T>`.
5+
pub fn load_json<T: DeserializeOwned>(path: &str) -> Vec<T> {
6+
let contents = fs::read_to_string(path)
7+
.unwrap_or_else(|_| panic!("No se pudo leer el archivo: {}", path));
8+
serde_json::from_str(&contents)
9+
.unwrap_or_else(|_| panic!("JSON inválido en {}", path))
10+
}

0 commit comments

Comments
 (0)