Skip to content

Commit 6b44fb7

Browse files
fix: address clippy lints
Signed-off-by: Oskar Manhart <[email protected]>
1 parent 1b85d3a commit 6b44fb7

File tree

9 files changed

+64
-51
lines changed

9 files changed

+64
-51
lines changed

winapps/src/backend/container.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use std::net::{IpAddr, Ipv4Addr};
66
use tracing::debug;
77

8-
#[derive(Debug)]
8+
#[derive(Debug, Clone)]
99
pub struct Container {
1010
config: &'static Config,
1111
}

winapps/src/backend/manual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{net::IpAddr, str::FromStr};
22

33
use crate::{command::Command, ensure, Backend, Config, Error, Result};
44

5-
#[derive(Debug)]
5+
#[derive(Debug, Clone)]
66
pub struct Manual {
77
config: &'static Config,
88
}

winapps/src/backend/mod.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,29 @@ pub trait Backend {
1919
fn get_host(&self) -> IpAddr;
2020

2121
fn as_remote_command(&self, command: Command) -> Command;
22-
23-
fn get_installed_apps(&self) -> Result<Vec<App>>;
2422
}
2523

2624
#[enum_dispatch(Backend)]
25+
#[derive(Debug, Clone)]
2726
pub enum Backends {
2827
Container,
2928
Manual,
3029
}
3130

3231
impl Config {
33-
pub fn get_backend(&'static self) -> Backends {
34-
assert!(self.libvirt.enable ^ self.container.enable ^ self.manual.enable);
35-
36-
if self.libvirt.enable {
37-
todo!()
38-
}
39-
40-
if self.container.enable {
41-
return Container::new(self).into();
42-
}
43-
44-
if self.manual.enable {
45-
return Manual::new(self).into();
46-
}
47-
48-
unreachable!()
32+
pub fn get_backend(&'static self) -> &'static Backends {
33+
self.backend.get_or_init(|| {
34+
match (
35+
self.libvirt.enable,
36+
self.container.enable,
37+
self.manual.enable,
38+
) {
39+
(true, _, _) => todo!(),
40+
(_, true, _) => Container::new(self).into(),
41+
(_, _, true) => Manual::new(self).into(),
42+
_ => unreachable!(),
43+
}
44+
})
4945
}
5046

5147
pub fn get_host(&'static self) -> IpAddr {
@@ -57,8 +53,8 @@ impl Config {
5753
}
5854

5955
fn get_installed_apps(&'static self) -> Result<Vec<App>> {
60-
Ok(self
61-
.as_remote_command(Command::new("C:\\ExtractPrograms.ps1"))
56+
let apps = Command::new("C:\\ExtractPrograms.ps1")
57+
.into_remote(self.get_backend())
6258
.wait_with_output()?
6359
.lines()
6460
.filter_map(|line| {
@@ -75,6 +71,8 @@ impl Config {
7571
_ => None,
7672
}
7773
})
78-
.collect::<Vec<App>>())
74+
.collect::<Vec<App>>();
75+
76+
Ok(apps)
7977
}
8078
}

winapps/src/command.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ensure, Error, IntoResult, Result};
1+
use crate::{ensure, Backend, Backends, Error, IntoResult, Result};
22
use std::{
33
fmt::{Display, Formatter},
44
process::{Child, Command as StdCommand, Stdio},
@@ -60,6 +60,10 @@ impl Command {
6060
}
6161
}
6262

63+
pub fn into_remote(self, backend: &Backends) -> Self {
64+
backend.as_remote_command(self)
65+
}
66+
6367
pub fn with_err(mut self, message: &'static str) -> Self {
6468
self.error_message = message.to_string();
6569
self

winapps/src/config/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::Backends;
12
use derive_new::new;
23
use serde::{Deserialize, Serialize};
4+
use std::sync::OnceLock;
35

46
mod apps;
57
mod operations;
@@ -20,6 +22,9 @@ pub struct Config {
2022
pub linked_apps: Vec<App>,
2123
#[new(value = "false")]
2224
pub debug: bool,
25+
#[new(value = "OnceLock::new()")]
26+
#[serde(skip)]
27+
pub(crate) backend: OnceLock<Backends>,
2328
}
2429

2530
#[derive(new, Debug, Deserialize, Serialize, Clone)]

winapps/src/config/operations.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
use std::{
2-
fs,
3-
fs::File,
4-
io::Write,
5-
net::IpAddr,
6-
path::{Path, PathBuf},
7-
str::FromStr,
8-
sync::OnceLock,
9-
};
1+
use std::{fs, fs::File, io::Write, net::IpAddr, path::PathBuf, str::FromStr, sync::OnceLock};
102
use tracing::warn;
113

124
use crate::{bail, dirs::path_ok, Config, Error, IntoResult, Result};

winapps/src/dirs.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::{bail, Result};
2-
use std::{fs, path::Path};
2+
use std::{
3+
fs,
4+
path::{Path, PathBuf},
5+
};
36

47
/// Check whether a directory exists and is a directory
58
/// If not, try creating it
@@ -18,33 +21,31 @@ pub fn path_ok(path: &Path) -> Result<()> {
1821
}
1922

2023
/// Get the data dir and validates it exists
21-
pub fn data_dir() -> Result<&'static Path> {
22-
let Some(data_dir) = dirs::data_local_dir() else {
24+
pub fn data_dir() -> Result<PathBuf> {
25+
let Some(data_dir) = dirs::data_dir().map(|path| path.join("winapps")) else {
2326
bail!("Could not determine $XDG_DATA_HOME")
2427
};
2528

26-
let dir = data_dir.join("winapps").as_path();
27-
path_ok(dir)?;
28-
29-
Ok(dir)
29+
path_ok(data_dir.as_path())?;
30+
Ok(data_dir)
3031
}
3132

3233
/// Get the icons dir and validates it exists
33-
pub fn icons_dir() -> Result<&'static Path> {
34-
let dir = data_dir()?.join("icons").as_path();
35-
path_ok(dir)?;
34+
pub fn icons_dir() -> Result<PathBuf> {
35+
let Some(data_dir) = dirs::data_dir().map(|path| path.join("icons")) else {
36+
bail!("Could not determine $XDG_DATA_HOME")
37+
};
3638

37-
Ok(dir)
39+
path_ok(data_dir.as_path())?;
40+
Ok(data_dir)
3841
}
3942

4043
/// Get the XDG applications dir and validates it exists
41-
pub fn desktop_dir() -> Result<&'static Path> {
42-
let Some(data_dir) = dirs::data_local_dir() else {
44+
pub fn desktop_dir() -> Result<PathBuf> {
45+
let Some(data_dir) = dirs::data_dir().map(|path| path.join("applications")) else {
4346
bail!("Could not determine $XDG_DATA_HOME")
4447
};
4548

46-
let dir = data_dir.join("applications").as_path();
47-
path_ok(dir)?;
48-
49-
Ok(dir)
49+
path_ok(data_dir.as_path())?;
50+
Ok(data_dir)
5051
}

winapps/src/errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ pub enum Error {
4444
#[diagnostic(code(winapps::toml_invalid_error))]
4545
Serialize(#[from] toml::ser::Error),
4646

47+
#[error("Icon is invalid base64")]
48+
#[diagnostic(
49+
code(winapps::setup_error),
50+
help("Setup returned a badly formed base64 string, is your config correct and are apps correctly installed?")
51+
)]
52+
InvalidIcon(#[from] base64::DecodeError),
53+
4754
#[error("RDP host is unreachable")]
4855
#[diagnostic(
4956
code(winapps::bad_vm_state),
@@ -57,6 +64,12 @@ pub enum Error {
5764
EmptyCommand,
5865
}
5966

67+
impl From<&str> for Error {
68+
fn from(value: &str) -> Self {
69+
Self::Message(value.to_string())
70+
}
71+
}
72+
6073
pub type Result<T> = std::result::Result<T, Error>;
6174

6275
impl<T> From<Error> for Result<T> {

winapps/src/remote_client/freerdp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55
use tracing::info;
66

7-
use crate::{command::Command, config::App, Config, Error, RemoteClient, Result};
7+
use crate::{command::Command, Config, Error, RemoteClient, Result};
88

99
pub struct Freerdp {
1010
config: &'static Config,

0 commit comments

Comments
 (0)