Struct package_json::PackageJsonManager
source · [−]pub struct PackageJsonManager { /* private fields */ }Expand description
A manager for manipulating package.json file.
Implementations
sourceimpl PackageJsonManager
impl PackageJsonManager
sourcepub fn with_file_path<FilePath>(path: FilePath) -> Self where
FilePath: AsRef<Path>,
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");sourcepub fn locate_closest(&mut self) -> Option<PathBuf>
pub fn locate_closest(&mut self) -> Option<PathBuf>
Try to locate the closest package.json file from current working directory to sys root.
sourcepub fn locate_closest_from<P: AsRef<Path>>(&mut self, from: P) -> Option<PathBuf>
pub fn locate_closest_from<P: AsRef<Path>>(&mut self, from: P) -> Option<PathBuf>
Try to locate the closest package.json file from specific directory to sys root.
sourcepub fn set_file_path<FilePath: AsRef<Path>>(&mut self, file_path: FilePath)
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.
sourcepub fn get_file_path(&mut self) -> Option<&Path>
pub fn get_file_path(&mut self) -> Option<&Path>
Get the located file path after locate_closest or locate_closest_from evaluated.
sourcepub fn take_file_path(&mut self) -> Option<PathBuf>
pub fn take_file_path(&mut self) -> Option<PathBuf>
Take the located file path out of PackageJsonManager, leaving a None in its place.
sourcepub fn read_ref(&mut self) -> Result<&PackageJson>
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_some() {
assert!(manager.read_ref().is_ok());
}sourcepub fn read_mut(&mut self) -> Result<&mut PackageJson>
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_some() {
assert!(manager.read_mut().is_ok());
}sourcepub fn write(&mut self) -> Result<()>
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_some() {
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");
}sourcepub fn write_to(&mut self, file_path: &Path) -> Result<()>
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_some() {
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
sourceimpl AsMut<PackageJson> for PackageJsonManager
impl AsMut<PackageJson> for PackageJsonManager
sourcefn as_mut(&mut self) -> &mut PackageJson
fn as_mut(&mut self) -> &mut PackageJson
Return a mutable reference to the current PackageJson struct.
sourceimpl AsRef<PackageJson> for PackageJsonManager
impl AsRef<PackageJson> for PackageJsonManager
sourcefn as_ref(&self) -> &PackageJson
fn as_ref(&self) -> &PackageJson
Return a immutable reference to the current PackageJson struct.
sourceimpl Debug for PackageJsonManager
impl Debug for PackageJsonManager
sourceimpl Default for PackageJsonManager
impl Default for PackageJsonManager
sourcefn default() -> PackageJsonManager
fn default() -> PackageJsonManager
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl RefUnwindSafe for PackageJsonManager
impl Send for PackageJsonManager
impl Sync for PackageJsonManager
impl Unpin for PackageJsonManager
impl UnwindSafe for PackageJsonManager
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more