A PackageJson struct for your rust code.

Overview

📦️ package_json_schema


Load a package.json file as a PackageJson struct.

Continuous integration badge for github actions


Why?

You want to load a package.json file and interact with it as a struct.


Installation

Add this line to the [dependencies] section of your Cargo.toml:

package_json_schema = "0.1.0"

If you would like to include validation then add the validate feature.

package_json_schema = { version = "0.1.0", features = ["validate"] }

This adds the validator crate as a dependency and adds the .validate() method to the PackageJson struct. The fields are validated according to the JSON schema specification.


Usage

The following example shows how to load a package.json file and use it as a struct.

use package_json_schema::PackageJson;

let contents = r###"
{
  "name": "my-package",
  "version": "0.1.0",
  "dependencies": {
    "@sveltejs/kit": "1.0.0-next.396"
  },
  "peerDependencies": {
    "aws-sdk": "2.1185.0"
  }
}
"###;

let package_json = PackageJson::try_from(contents).unwrap();
assert_eq!(package_json.name.unwrap(), "my-package");
assert_eq!(package_json.version.unwrap(), "0.1.0");

This crate leaves it to the user to load the package.json content from the filesystem. Here is an example of loading the file contents and parsing the contents into a struct.

use std::fs::read_to_string;
use package_json_schema::PackageJson;

let contents = read_to_string("./tests/fixtures/1/package.json").unwrap();
let package_json = PackageJson::try_from(contents).unwrap();

assert_eq!(package_json.name.unwrap(), "test");

A package.json file can also be created from a builder.

use package_json_schema::PackageJson;
use package_json_schema::AdditionalFields;
use package_json_schema::Person;
use indexmap::IndexMap;

let mut additional_fields: AdditionalFields = IndexMap::new();
additional_fields.insert("custom".into(), "value".into());

let package_json = PackageJson::builder()
  .name("awesome")
  .author(Person::String("Tester".into()))
  .other(additional_fields)
  .build();
let string_value = package_json.try_to_string().unwrap();

assert_eq!(
  string_value,
  r#"{"name":"awesome","author":"Tester","custom":"value"}"#
);

To validate the package.json fields, enable the validate feature.

package_json_schema = { version = "0.1.0", features = ["validate"] }

And then use the validate method.

use std::fs::read_to_string;
use package_json_schema::PackageJson;
#[cfg(feature = "validate")]
use validator::Validate;

let contents = read_to_string("./tests/fixtures/1/package.json").unwrap();
let package_json = PackageJson::try_from(contents).unwrap();

#[cfg(feature = "validate")]
package_json.validate().unwrap();

License

This project is licensed under the Unlicense license.

You might also like...
Here you'll find the open sourced code for the MOBIX staking contracts.

Mobix Staking Compile: rustup default stable cargo wasm Test: cargo test Optimize: quick and good: RUSTFLAGS='-C link-arg=-s' cargo wasm slow and b

Generates Solidity code to verify EIP-712 style signatures

eip712 Generates Solidity code to verify EIP-712 style signatures. Usage First, create an abstract contract implementing the functionality you want: /

A mini CLI tool to detect secrets & credentials in source code
A mini CLI tool to detect secrets & credentials in source code

Fencer Fencer is a mini-CLI tool that can used to scan various kind of secrets/credentials that are hardcoded into a project source code files Feature

Tony Hawk's Underground 2: Remix Cheat Cracker - Performs dictionary attack on cheat code hashes for THU2R

Tony Hawk's Underground 2 Cheat Cracker A simple utility for cracking cheat codes for Tony Hawk's Underground 2, written in Rust. Background This proj

cargo-crev to cargo-vet code review exporter

cargo-crev to cargo-vet converter Crev and Vet are supply-chain security tools for auditing Rust/Cargo dependencies. This tool (crevette) is a helper

This monorepository contains the source code for the smart contracts implementing bAsset Protocol on the Terra blockchain.

Crll bAsset Contracts This monorepository contains the source code for the smart contracts implementing bAsset Protocol on the Terra blockchain. You c

A library to help you sew up your Ethereum project with Rust and just like develop in a common backend

SewUp Secondstate EWasm Utility Program, a library helps you sew up your Ethereum project with Rust and just like development in a common backend. The

Create your personal token with rust smart contracts

Solana Rust Token 💰 This application written Rust using Anchor ⚓

A template for your next Rust project.

Opinionated Rust Template A template for your next Rust project. To start coding run: cargo generate tomkarw/opinionated-rust-template Only requires j

Comments
  • Feature request: export validator::Validate directly from package_json_schema

    Feature request: export validator::Validate directly from package_json_schema

    Description

    Export validator::Validate directly from package_json_schema.

    Motivation

    Allow crate's users to directly import validator::Validate from package_json_schema: right now you need to manully specify the depency in your cargo.toml

    Something like:

    use package_json_schema::{ PackageJson, validator::Validate };
    

    I think it should be fairly to implement as well. I was thinking something like this:

    // File: src/lib.rs
    
    cfg_if! {
      if #[cfg(feature = "validate")] {
        mod utils; // Taken from current implementation
        pub mod validator;  
      }
    }
    

    If you think it might be a good addition I'm willing to contribute myself :D

    opened by nickfla1 1
Releases(0.2.0)
Bitcoin Push Notification Service (BPNS) allows you to receive notifications of Bitcoin transactions of your non-custodial wallets on a provider of your choice, all while respecting your privacy

Bitcoin Push Notification Service (BPNS) Description Bitcoin Push Notification Service (BPNS) allows you to receive notifications of Bitcoin transacti

BPNS 1 May 2, 2022
Employ your built-in wetware pattern recognition and signal processing facilities to understand your network traffic

Nethoscope Employ your built-in wetware pattern recognition and signal processing facilities to understand your network traffic. Check video on how it

Vesa Vilhonen 86 Dec 5, 2022
Koofr Vault is an open-source, client-side encrypted folder for your Koofr cloud storage offering an extra layer of security for your most sensitive files.

Koofr Vault https://vault.koofr.net Koofr Vault is an open-source, client-side encrypted folder for your Koofr cloud storage offering an extra layer o

Koofr 12 Dec 30, 2022
a Solana program for granting friends permissions on your account without revealing your private key.

Delegatooooor Granting Permission: An account holder (the delegator) decides to grant permission to a delegate. They create and sign a transaction tha

0xGhostMac 3 Apr 3, 2024
A cryptographically verifiable code review system for the cargo (Rust) package manager.

A cryptographically verifiable code review system for the cargo (Rust) package manager.

crev - Code REView system 1.8k Jan 2, 2023
zink! is a library for developing ink! smart contracts with useful Rust macros that extend functionality and reduce boilerplate code.

zink! Smart Contract Macros This is a helper library for developing ink! smart contracts. It contains useful Rust macros that extend functionality and

Scio Labs 3 Nov 3, 2023
A Minimalistic Rust library to extract all potential function selectors from EVM bytecode without source code.

EVM Hound A Minimalistic Rust library to extract all potential function selectors from EVM bytecode without source code. Installation $ cargo add evm_

null 34 Dec 3, 2023
A node API for the dprint TypeScript and JavaScript code formatter

dprint-node A node API for the dprint TypeScript and JavaScript code formatter. It's written in Rust for blazing fast speed. Usage Pass a file path an

Devon Govett 431 Dec 24, 2022
My code for the terra.academy course on CosmWasm smart contracts

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

Alex Incerti 0 Nov 7, 2021
Cryptographic Primitive Code Generation by Fiat

Fiat-Crypto: Synthesizing Correct-by-Construction Code for Cryptographic Primitives Building This repository requires Coq 8.11 or later. Note that if

Programming Languages and Verification Group at MIT CSAIL 538 Jan 7, 2023