Add dinopkg-package-json

This commit is contained in:
SkyfallWasTaken 2024-07-09 20:58:05 +01:00
parent 4d4223acdc
commit 7117a31926
5 changed files with 43 additions and 2 deletions

23
Cargo.lock generated
View file

@ -230,6 +230,7 @@ version = "0.1.0"
dependencies = [
"clap",
"color-eyre",
"dinopkg-package-json",
"owo-colors 4.0.0",
"reqwest",
"tokio",
@ -241,6 +242,8 @@ version = "0.1.0"
dependencies = [
"serde",
"serde_json",
"thiserror",
"tokio",
]
[[package]]
@ -1089,6 +1092,26 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "thiserror"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.8"

View file

@ -9,3 +9,4 @@ color-eyre = "0.6.3"
owo-colors = "4.0.0"
reqwest = "0.12.5"
tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
dinopkg-package-json = { path = "../dinopkg-package-json" }

View file

@ -1 +1,3 @@
use dinopkg_package_json::PackageJson;
pub async fn run(script_name: Option<String>) {}

View file

@ -6,3 +6,5 @@ edition = "2021"
[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["fs"], optional = true }

View file

@ -17,8 +17,21 @@ pub struct PackageJson {
pub type Scripts = HashMap<String, String>;
pub type Dependencies = HashMap<String, String>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("deserialization error: {0}")]
Serde(#[from] serde_json::Error),
}
impl PackageJson {
pub fn parse(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
pub fn parse(json: &str) -> Result<Self, Error> {
Ok(serde_json::from_str(json)?)
}
#[cfg(feature = "tokio")]
pub async fn from_file() -> Result<Self, Error> {
let file = tokio::fs::read("package.json").await?;
let file = String::from_utf8(file)?;
Self::parse(&file)
}
}