📦️ package_json_schema
Load a
package.json
file as a PackageJsonstruct
.
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
:
= "0.2"
If you would like to include validation then add the validate
feature.
= { = "0.2", = ["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 PackageJson;
let contents = r###"
{
"name": "my-package",
"version": "0.0.1",
"dependencies": {
"@sveltejs/kit": "1.0.0-next.396"
},
"peerDependencies": {
"aws-sdk": "2.1185.0"
}
}
"###;
let package_json = try_from.unwrap;
assert_eq!;
assert_eq!;
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 read_to_string;
use PackageJson;
let contents = read_to_string.unwrap;
let package_json = try_from.unwrap;
assert_eq!;
A package.json
file can also be created from a builder.
use PackageJson;
use AdditionalFields;
use Person;
use IndexMap;
let mut additional_fields: AdditionalFields = new;
additional_fields.insert;
let package_json = builder
.name
.author
.other
.build;
let string_value = package_json.try_to_string.unwrap;
assert_eq!;
To validate the package.json
fields, enable the validate
feature.
= { = "0.2.1", = ["validate"] }
And then use the validate
method.
use read_to_string;
use PackageJson;
use Validate;
let contents = read_to_string.unwrap;
let package_json = try_from.unwrap;
package_json.validate.unwrap;
Contributing
devenv
is used to provide a reproducible development environment for this project. Follow the getting started instructions.
To automatically load the environment you should install direnv and then load the direnv
.
# The security mechanism didn't allow to load the `.envrc`.
# Since we trust it, let's allow it execution.
At this point you should see the nix
commands available in your terminal.
Run the following commands to install all the required dependencies.
This installs all cargo binaries locally so you don't need to worry about polluting your global namespace with required versions. The following scripts are added to your PATH
.
scripts
- build:all: Build all crates with all features activated.
- build:docs: Build documentation site.
- coverage:all: Generate a coverage report for the project
- fix:all: Fix all fixable issues
- fix:clippy: Fix all clippy issues
- fix:format: Format the files with dprint
- install:all: Install all packages.
- lint:all: Lint the whole codebase and fail if any issues found.
- lint:clippy: Check that lint rules are maintained for the entire project
- lint:format: Check that formatting is correct for the project.
- setup:ci: Setup the CI environment.
- snapshot:update: Update all snapshots when running the test suite.
- test:all: Run all tests for the project.
- update:deps: Update dependencies.
Upgrading devenv
If you have an outdated version of devenv
you can update it by running the following commands. If you have an easier way, please create a PR and I'll update these docs.
Editor Setup
To setup recommended configuration for your favorite editor run the following commands.
License
Unlicense, see the LICENSE file.