mirror of
https://github.com/SkyfallWasTaken/dinopkg.git
synced 2024-11-21 20:53:38 +00:00
Add author_variants
, repository_variants
, test_parse
tests
This commit is contained in:
parent
f72f4d372c
commit
b590710152
3 changed files with 113 additions and 3 deletions
24
Cargo.lock
generated
24
Cargo.lock
generated
|
@ -367,6 +367,12 @@ dependencies = [
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diff"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dinopkg-cli"
|
name = "dinopkg-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -404,6 +410,8 @@ dependencies = [
|
||||||
name = "dinopkg-package-json"
|
name = "dinopkg-package-json"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"maplit",
|
||||||
|
"pretty_assertions",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_with",
|
"serde_with",
|
||||||
|
@ -1400,6 +1408,16 @@ version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pretty_assertions"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
|
||||||
|
dependencies = [
|
||||||
|
"diff",
|
||||||
|
"yansi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.86"
|
version = "1.0.86"
|
||||||
|
@ -2453,6 +2471,12 @@ dependencies = [
|
||||||
"linked-hash-map",
|
"linked-hash-map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yansi"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zeroize"
|
name = "zeroize"
|
||||||
version = "1.8.1"
|
version = "1.8.1"
|
||||||
|
|
|
@ -9,3 +9,7 @@ serde_json = "1.0.120"
|
||||||
serde_with = "3.9.0"
|
serde_with = "3.9.0"
|
||||||
thiserror = "1.0.61"
|
thiserror = "1.0.61"
|
||||||
tokio = { version = "1.38.0", features = ["fs"], optional = true }
|
tokio = { version = "1.38.0", features = ["fs"], optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
maplit = "1.0.2"
|
||||||
|
pretty_assertions = "1.4.0"
|
||||||
|
|
|
@ -8,7 +8,7 @@ mod util;
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[skip_serializing_none]
|
#[skip_serializing_none]
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Default)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct PackageJson {
|
pub struct PackageJson {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -28,14 +28,14 @@ pub struct PackageJson {
|
||||||
pub dev_dependencies: Option<Dependencies>,
|
pub dev_dependencies: Option<Dependencies>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum AuthorVariant {
|
pub enum AuthorVariant {
|
||||||
Author { name: String, url: Option<String> },
|
Author { name: String, url: Option<String> },
|
||||||
String(String),
|
String(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub enum RepositoryVariant {
|
pub enum RepositoryVariant {
|
||||||
Repository { r#type: String, url: Option<String> },
|
Repository { r#type: String, url: Option<String> },
|
||||||
|
@ -91,3 +91,85 @@ impl PackageJson {
|
||||||
Ok((Self::parse(&file)?, path))
|
Ok((Self::parse(&file)?, path))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use maplit::hashmap;
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse() {
|
||||||
|
let json = r#"{
|
||||||
|
"name": "dinopkg-package-json",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"author": "Skyfall",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.17.1"
|
||||||
|
}
|
||||||
|
}"#;
|
||||||
|
let package_json = PackageJson::parse(json).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
package_json,
|
||||||
|
PackageJson {
|
||||||
|
name: "dinopkg-package-json".into(),
|
||||||
|
version: "0.1.0".into(),
|
||||||
|
author: Some(AuthorVariant::String("Skyfall".into())),
|
||||||
|
dependencies: Some(hashmap! {
|
||||||
|
"express".into() => "^4.17.1".into(),
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn author_variants() {
|
||||||
|
let json = r#"{
|
||||||
|
"name": "dinopkg-package-json",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"author": {
|
||||||
|
"name": "Skyfall",
|
||||||
|
"url": "https://skyfall.dev"
|
||||||
|
}
|
||||||
|
}"#;
|
||||||
|
let package_json = PackageJson::parse(json).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
package_json,
|
||||||
|
PackageJson {
|
||||||
|
name: "dinopkg-package-json".into(),
|
||||||
|
version: "0.1.0".into(),
|
||||||
|
author: Some(AuthorVariant::Author {
|
||||||
|
name: "Skyfall".into(),
|
||||||
|
url: Some("https://skyfall.dev".into())
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn repository_variants() {
|
||||||
|
let json = r#"{
|
||||||
|
"name": "dinopkg-package-json",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/SkyfallWasTaken/choco.git"
|
||||||
|
}
|
||||||
|
}"#;
|
||||||
|
let package_json = PackageJson::parse(json).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
package_json,
|
||||||
|
PackageJson {
|
||||||
|
name: "dinopkg-package-json".into(),
|
||||||
|
version: "0.1.0".into(),
|
||||||
|
repository: Some(RepositoryVariant::Repository {
|
||||||
|
r#type: "git".into(),
|
||||||
|
url: Some("git+https://github.com/SkyfallWasTaken/choco.git".into())
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue