From 7117a31926198de5fc845a3a5a9b31185b0f1cf6 Mon Sep 17 00:00:00 2001 From: SkyfallWasTaken Date: Tue, 9 Jul 2024 20:58:05 +0100 Subject: [PATCH] Add dinopkg-package-json --- Cargo.lock | 23 +++++++++++++++++++ crates/dinopkg-cli/Cargo.toml | 1 + crates/dinopkg-cli/src/command/run.rs | 2 ++ crates/dinopkg-package-json/Cargo.toml | 2 ++ .../src/{main.rs => lib.rs} | 17 ++++++++++++-- 5 files changed, 43 insertions(+), 2 deletions(-) rename crates/dinopkg-package-json/src/{main.rs => lib.rs} (53%) diff --git a/Cargo.lock b/Cargo.lock index 1473415..3fd0525 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/dinopkg-cli/Cargo.toml b/crates/dinopkg-cli/Cargo.toml index cb77d30..fc2b69d 100644 --- a/crates/dinopkg-cli/Cargo.toml +++ b/crates/dinopkg-cli/Cargo.toml @@ -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" } diff --git a/crates/dinopkg-cli/src/command/run.rs b/crates/dinopkg-cli/src/command/run.rs index 3783125..95cd974 100644 --- a/crates/dinopkg-cli/src/command/run.rs +++ b/crates/dinopkg-cli/src/command/run.rs @@ -1 +1,3 @@ +use dinopkg_package_json::PackageJson; + pub async fn run(script_name: Option) {} diff --git a/crates/dinopkg-package-json/Cargo.toml b/crates/dinopkg-package-json/Cargo.toml index 16da509..0db8401 100644 --- a/crates/dinopkg-package-json/Cargo.toml +++ b/crates/dinopkg-package-json/Cargo.toml @@ -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 } diff --git a/crates/dinopkg-package-json/src/main.rs b/crates/dinopkg-package-json/src/lib.rs similarity index 53% rename from crates/dinopkg-package-json/src/main.rs rename to crates/dinopkg-package-json/src/lib.rs index 18b8ed6..48d0915 100644 --- a/crates/dinopkg-package-json/src/main.rs +++ b/crates/dinopkg-package-json/src/lib.rs @@ -17,8 +17,21 @@ pub struct PackageJson { pub type Scripts = HashMap; pub type Dependencies = HashMap; +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("deserialization error: {0}")] + Serde(#[from] serde_json::Error), +} + impl PackageJson { - pub fn parse(json: &str) -> Result { - serde_json::from_str(json) + pub fn parse(json: &str) -> Result { + Ok(serde_json::from_str(json)?) + } + + #[cfg(feature = "tokio")] + pub async fn from_file() -> Result { + let file = tokio::fs::read("package.json").await?; + let file = String::from_utf8(file)?; + Self::parse(&file) } }