algosul/app/apps/rust/
cargo.rs

1use std::{
2  borrow::Cow,
3  ffi::OsStr,
4  path::{Path, PathBuf},
5  sync::Arc,
6};
7
8use algosul_core::{args, cows};
9use tokio::process::Command;
10
11use crate::app::{apps::rust::utils, AppInfo, AppLicense, AppPath};
12
13#[derive(Default, Debug, Clone, Eq, PartialEq, Hash)]
14pub struct Cargo
15{
16  home_path: Arc<PathBuf>,
17}
18
19#[derive(Default, Debug, Clone, Eq, PartialEq, Hash)]
20pub enum CargoCommandArgs
21{
22  #[default]
23  Version,
24  Metadata
25  {
26    format_version: Option<Cow<'static, str>>,
27    no_deps:        bool,
28    features:       Vec<Cow<'static, str>>,
29  },
30}
31impl CargoCommandArgs
32{
33  pub fn into_args(self) -> Vec<Cow<'static, OsStr>>
34  {
35    let mut args = Vec::<Cow<'static, OsStr>>::new();
36    match self
37    {
38      CargoCommandArgs::Version =>
39      {
40        args!(args:
41          cows!["version"]
42        );
43      }
44      CargoCommandArgs::Metadata { format_version, no_deps, features } =>
45      {
46        args!(args:
47          cows!["metadata"];
48          if no_deps => cows!["--no-deps"];
49          features
50            .into_iter()
51            .flat_map(|x| cows!["-F", x]);
52          if let Some(format_version) = format_version =>
53            cows!["--format-version", format_version];
54        );
55      }
56    }
57    args
58  }
59
60  pub fn into_command(self, cargo_path: Cow<'static, OsStr>) -> Command
61  {
62    let mut command = Command::new(cargo_path);
63    command.args(self.into_args());
64    command
65  }
66}
67
68impl Cargo
69{
70  pub fn as_home_path(&self) -> Arc<PathBuf>
71  {
72    self.home_path.clone()
73  }
74}
75impl AppInfo for Cargo
76{
77  type Error = super::Error;
78
79  async fn name(&self) -> Cow<'_, str>
80  {
81    Cow::Borrowed("cargo")
82  }
83
84  async fn license(&self) -> Result<Cow<'_, AppLicense>, Self::Error>
85  {
86    Ok(Cow::Owned(utils::rust_license()))
87  }
88
89  async fn readme(&self) -> Result<String, Self::Error>
90  {
91    todo!()
92  }
93
94  async fn readme_md(&self) -> Result<String, Self::Error>
95  {
96    todo!()
97  }
98
99  async fn documentation(&self) -> Result<Cow<'_, str>, Self::Error>
100  {
101    Ok(Cow::Borrowed("https://doc.rust-lang.org/cargo"))
102  }
103
104  async fn homepage(&self) -> Result<Cow<'_, str>, Self::Error>
105  {
106    self.repository().await
107  }
108
109  async fn repository(&self) -> Result<Cow<'_, str>, Self::Error>
110  {
111    Ok(Cow::Borrowed("https://github.com/rust-lang/cargo"))
112  }
113
114  async fn version(&self) -> Result<Cow<'_, str>, Self::Error>
115  {
116    todo!()
117  }
118}
119impl AppPath for Cargo
120{
121  async fn home_path(&self) -> Result<Cow<'_, Path>, Self::Error>
122  {
123    Ok(Cow::Borrowed(self.home_path.as_ref()))
124  }
125
126  async fn bin_path(&self) -> Result<Cow<'_, Path>, Self::Error>
127  {
128    Ok(Cow::Owned(self.home_path.join("cargo")))
129  }
130}
131impl utils::RustAppExt for Cargo
132{
133  fn new(home_path: Arc<PathBuf>) -> crate::app::apps::rust::Result<Self>
134  {
135    Ok(Self { home_path })
136  }
137}