Add 'manifest' package and integrate it with 'command_init'

A new package named 'manifest' is introduced in this commit. It comprises a single structure and related functions for managing manifests. Additionally, 'command_init' has been altered to use this new module instead of using its previously hard-coded functionality. A reference to 'manifest' has also been added in the Cargo.lock and command_init/Cargo.toml files.
This commit is contained in:
Ian Wijma 2024-01-05 20:11:17 +11:00
parent b09a4a41ff
commit b3d7c776e8
7 changed files with 55 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target /target
/.idea /.idea
/projects

5
Cargo.lock generated
View File

@ -110,6 +110,7 @@ name = "command_init"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"manifest",
] ]
[[package]] [[package]]
@ -140,6 +141,10 @@ version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "manifest"
version = "0.1.0"
[[package]] [[package]]
name = "mccli" name = "mccli"
version = "0.1.0" version = "0.1.0"

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
manifest = {path = "../manifest"}
clap = { version = "4.4.8", features = ["derive", "unicode", "wrap_help"] } clap = { version = "4.4.8", features = ["derive", "unicode", "wrap_help"] }

View File

@ -1,4 +1,6 @@
use std::path::PathBuf;
use clap::{Args}; use clap::{Args};
use manifest::Manifest;
#[derive(Args, Debug)] #[derive(Args, Debug)]
pub struct Arguments { pub struct Arguments {
@ -7,20 +9,8 @@ pub struct Arguments {
} }
pub fn execute (_arguments: &Arguments) { pub fn execute (_arguments: &Arguments) {
println!("Hello from init!"); let directory: PathBuf = PathBuf::from(_arguments.directory.clone());
}
pub fn add(left: usize, right: usize) -> usize { let manifest = Manifest::new(&directory);
left + right manifest.init();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
} }

8
manifest/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "manifest"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

33
manifest/src/lib.rs Normal file
View File

@ -0,0 +1,33 @@
use std::fs;
use std::path::{PathBuf};
#[derive(Debug)]
pub struct Manifest {
directory: PathBuf
}
impl Manifest {
pub fn new(path: &PathBuf) -> Manifest {
let mut directory: PathBuf = path.into();
if path.is_file() {
directory = path.parent()
.map(|parent| parent.to_path_buf())
.unwrap_or_else(|| path.into())
}
if let Err(_err) = fs::create_dir_all(&directory) {
panic!("Failed to created directory: {:?}", &directory);
}
Manifest { directory: directory.canonicalize().unwrap() }
}
pub fn init(&self) -> bool {
let directory: PathBuf = self.directory.clone();
println!("init: {:?}", directory);
true
}
}