From 90b0361cf89ef84e2b0fbf7e7105fa0131365948 Mon Sep 17 00:00:00 2001 From: Julien Capellari Date: Wed, 1 May 2024 12:08:42 +0200 Subject: [PATCH] feat: add parse file utility --- crates/ring-js-project-parser/Cargo.toml | 2 ++ crates/ring-js-project-parser/src/lib.rs | 15 +-------- .../src/manifest/mod.rs | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 crates/ring-js-project-parser/src/manifest/mod.rs diff --git a/crates/ring-js-project-parser/Cargo.toml b/crates/ring-js-project-parser/Cargo.toml index 8edb587..88fbbe1 100644 --- a/crates/ring-js-project-parser/Cargo.toml +++ b/crates/ring-js-project-parser/Cargo.toml @@ -5,3 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +serde = { version = "^1.0", features = ["derive"] } +serde_json = "^1.0" \ No newline at end of file diff --git a/crates/ring-js-project-parser/src/lib.rs b/crates/ring-js-project-parser/src/lib.rs index 7d12d9a..640fc64 100644 --- a/crates/ring-js-project-parser/src/lib.rs +++ b/crates/ring-js-project-parser/src/lib.rs @@ -1,14 +1 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod manifest; diff --git a/crates/ring-js-project-parser/src/manifest/mod.rs b/crates/ring-js-project-parser/src/manifest/mod.rs new file mode 100644 index 0000000..5a178c2 --- /dev/null +++ b/crates/ring-js-project-parser/src/manifest/mod.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; +use std::io; +use std::fs::File; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct PackageManifest { + pub name: String, + pub version: String, + #[serde(default)] + pub workspaces: Vec, + #[serde(default)] + pub scripts: HashMap, + #[serde(default)] + pub dependencies: HashMap, + #[serde(default, rename = "devDependencies")] + pub dev_dependencies: HashMap, +} + +pub enum Error { + Io(io::Error), + Parse(serde_json::Error) +} + +impl PackageManifest { + pub fn parse_file(path: &str) -> Result { + let file = File::open(path).map_err(Error::Io)?; + let manifest = serde_json::from_reader(&file).map_err(Error::Parse)?; + + Ok(manifest) + } +} \ No newline at end of file