Struct PackageJsonManager

Source
pub struct PackageJsonManager { /* private fields */ }
Expand description

A manager for manipulating package.json file.

Implementations§

Source§

impl PackageJsonManager

Source

pub fn new() -> Self

Constructs a new, empty PackageJsonManager.

Source

pub fn with_file_path<FilePath>(path: FilePath) -> Self
where FilePath: AsRef<Path>,

Constructs a new, empty PackageJsonManger with the specified package.json file path.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::with_file_path("/path/to/package.json");
Source

pub fn with_write_options(options: WriteOptions) -> Self

Construct a new, empty PackageJsonManager with the specified WriteOptions.

use package_json::{PackageJsonManager, WriteOptions, WriteOptionsBuilder};
let mut manager = PackageJsonManager::with_write_options(WriteOptions::default());

let mut manager = PackageJsonManager::with_write_options(
  WriteOptionsBuilder::default().pretty(false).build().unwrap()
);
Source

pub fn locate_closest(&mut self) -> Result<PathBuf>

Try to locate the closest package.json file from current working directory to sys root.

Source

pub fn locate_closest_from<P: AsRef<Path>>( &mut self, from: P, ) -> Result<PathBuf>

Try to locate the closest package.json file from specific directory to sys root.

Source

pub fn set_file_path<FilePath: AsRef<Path>>(&mut self, file_path: FilePath)

Specify the package.json file path which is used to read and write.

Source

pub fn get_file_path(&mut self) -> Option<&Path>

Get the located file path after locate_closest or locate_closest_from evaluated.

Source

pub fn take_file_path(&mut self) -> Option<PathBuf>

Take the located file path out of PackageJsonManager, leaving a None in its place.

Source

pub fn read_ref(&mut self) -> Result<&PackageJson>

Evaluate package.json parser and return the immutable PackageJson reference.

Note: It always reads the file again. In the most case, you should call as_ref to get a immutable reference if you have read it before.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_ok() {
  assert!(manager.read_ref().is_ok());
}
Source

pub fn read_mut(&mut self) -> Result<&mut PackageJson>

Evaluate package.json parser and return the mutable PackageJson reference.

Note: It always reads the file again. In the most case, you should call as_mut to get a mutable reference if you have read it before.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_ok() {
  assert!(manager.read_mut().is_ok());
}
Source

pub fn write(&mut self) -> Result<()>

Use the current package.json content to write the target package.json file.

use package_json::PackageJsonManager;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_ok() {
  if let Ok(mut json) = manager.read_mut() {
    json.name = "new name".to_string();
    json.version = "1.0.0".to_string();
  }
  manager.write().expect("Couldn't write package.json");
}
Source

pub fn write_to(&mut self, file_path: &Path) -> Result<()>

Write the current package.json content to the specific package.json file.

use package_json::PackageJsonManager;
use std::path::Path;
let mut manager = PackageJsonManager::new();
if manager.locate_closest().is_ok() {
  if let Ok(mut json) = manager.read_mut() {
    json.name = "new name".to_string();
    json.version = "1.0.0".to_string();
  }
  manager
    .write_to(&Path::new("/path/to/package.json"))
    .expect("Couldn't write package.json");
}

Trait Implementations§

Source§

impl AsMut<PackageJson> for PackageJsonManager

Source§

fn as_mut(&mut self) -> &mut PackageJson

Return a mutable reference to the current PackageJson struct.

Source§

impl AsRef<PackageJson> for PackageJsonManager

Source§

fn as_ref(&self) -> &PackageJson

Return a immutable reference to the current PackageJson struct.

Source§

impl Debug for PackageJsonManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PackageJsonManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.