Skip to content

Commit

Permalink
add cargo mod
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 9, 2024
1 parent 8bc7e7e commit c178f9a
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 189 deletions.
177 changes: 177 additions & 0 deletions workspace/gh-workflow/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
use std::marker::PhantomData;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, strum_macros::Display)]
#[serde(rename_all = "kebab-case")]
pub enum CargoCommand {
Add,
Bench,
Build,
Check,
Chef,
Clean,
Clippy,
Component,
Config,
Criterion,
Doc,
Expand,
Fetch,
Fix,
Flamegraph,
Fmt,
Generate,
GenerateLockfile,
GitCheckout,
Help,
Info,
Init,
Insta,
Install,
LocateProject,
Login,
Logout,
Make,
Metadata,
Miri,
New,
Owner,
Package,
ReadManifest,
Remove,
Report,
Run,
Rustc,
Rustdoc,
Search,
Test,
Tree,
Udeps,
Uninstall,
Update,
Vendor,
VerifyProject,
Version,
Watch,
Yank,
}
#[derive(Debug, PartialEq, Eq)]
pub struct CargoTest;

#[derive(Debug, PartialEq, Eq)]
pub struct CargoClippy;

#[derive(Debug, PartialEq, Eq)]
pub struct CargoFmt;

#[derive(Debug, PartialEq, Eq)]
pub struct CargoCheck;

#[derive(Debug, PartialEq, Eq)]
pub struct CargoClean;

#[derive(Debug, PartialEq, Eq)]
pub struct Cargo<Ty> {
pub command: CargoCommand,
pub args: Vec<String>,
marker: PhantomData<Ty>,
}

impl<Ty> Cargo<Ty> {
// TODO: add function to handle command
// specific sub command
pub fn add_arg<S: ToString>(mut self, arg: S) -> Self {
self.args.push(arg.to_string());

self
}
}

impl Cargo<CargoClean> {
pub fn clean() -> Cargo<CargoClean> {
Cargo {
command: CargoCommand::Clean,
args: vec![],
marker: Default::default(),
}
}
}

impl Cargo<CargoCheck> {
pub fn check() -> Cargo<CargoCheck> {
Cargo {
command: CargoCommand::Check,
args: vec![],
marker: Default::default(),
}
}
}

impl Cargo<CargoTest> {
pub fn test() -> Cargo<CargoTest> {
Cargo {
command: CargoCommand::Test,
args: vec![],
marker: Default::default(),
}
}
pub fn all_features(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--all-features") {
self.args.push("--all-features".to_string());
}

self
}
pub fn workspace(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--workspace") {
self.args.push("--workspace".to_string());
}
self
}
}

impl Cargo<CargoClippy> {
pub fn clippy() -> Cargo<CargoClippy> {
Cargo {
command: CargoCommand::Clippy,
args: vec![],
marker: Default::default(),
}
}
pub fn all_features(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--all-features") {
self.args.push("--all-features".to_string());
}
self
}
pub fn workspace(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--workspace") {
self.args.push("--workspace".to_string());
}
self
}
}

impl Cargo<CargoFmt> {
pub fn fmt() -> Cargo<CargoFmt> {
Cargo {
command: CargoCommand::Fmt,
args: vec![],
marker: Default::default(),
}
}
pub fn all(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--all") {
self.args.push("--all".to_string());
}

self
}
pub fn check(mut self) -> Self {
if !self.args.iter().any(|arg| arg == "--check") {
self.args.push("--check".to_string());
}
self
}
}
2 changes: 2 additions & 0 deletions workspace/gh-workflow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod cargo;
pub mod error;
mod event;
mod generate;
mod rust_flag;
mod toolchain;
pub(crate) mod workflow;

pub use cargo::*;
pub use event::*;
pub use rust_flag::*;
pub use toolchain::*;
Expand Down
Loading

0 comments on commit c178f9a

Please sign in to comment.