Skip to content

Commit

Permalink
Merge pull request #8 from Jujulego/feat/parse-manifest
Browse files Browse the repository at this point in the history
Parse package.json manifest
  • Loading branch information
Jujulego authored May 1, 2024
2 parents ed0e7de + 90b0361 commit 463ea40
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 2 additions & 0 deletions crates/ring-js-project-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 1 addition & 14 deletions crates/ring-js-project-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 32 additions & 0 deletions crates/ring-js-project-parser/src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[serde(default)]
pub scripts: HashMap<String, String>,
#[serde(default)]
pub dependencies: HashMap<String, String>,
#[serde(default, rename = "devDependencies")]
pub dev_dependencies: HashMap<String, String>,
}

pub enum Error {
Io(io::Error),
Parse(serde_json::Error)
}

impl PackageManifest {
pub fn parse_file(path: &str) -> Result<PackageManifest, Error> {
let file = File::open(path).map_err(Error::Io)?;
let manifest = serde_json::from_reader(&file).map_err(Error::Parse)?;

Ok(manifest)
}
}

0 comments on commit 463ea40

Please sign in to comment.