Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit aa86a34

Browse files
committed
refactor + feat: use ron for config files
1 parent 7b94b62 commit aa86a34

File tree

5 files changed

+164
-142
lines changed

5 files changed

+164
-142
lines changed

Diff for: Cargo.lock

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ freedesktop-desktop-entry = "0.5.2"
1414
freedesktop-icon-lookup = "0.1.3"
1515
log = "0.4.21"
1616
regex = "1.10.4"
17+
ron = "0.8.1"
1718
serde = { version = "1.0.197", features = ["derive"] }
1819
serde_json = "1.0.115"
1920
walkdir = "2.5.0"

Diff for: src/container_type.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use std::path::Path;
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
6+
pub enum ContainerType {
7+
Podman,
8+
Docker,
9+
Toolbox,
10+
Unknown,
11+
}
12+
13+
impl ContainerType {
14+
pub fn not_supported(self) -> bool {
15+
matches!(
16+
self,
17+
ContainerType::Docker | ContainerType::Podman | ContainerType::Unknown
18+
)
19+
}
20+
21+
pub fn format_copy(self, container_name: &str, from: &Path, to: &Path) -> String {
22+
match self {
23+
ContainerType::Toolbox => {
24+
format!(
25+
"podman container cp {}:{}/. {}/",
26+
container_name,
27+
from.to_str().unwrap(),
28+
to.to_str().unwrap()
29+
)
30+
}
31+
_ => "".to_string(), // TODO: Support more container types
32+
}
33+
}
34+
35+
pub fn format_exec(self, container_name: &str, command: &str) -> String {
36+
match self {
37+
ContainerType::Toolbox => {
38+
format!("toolbox run -c {} {}", container_name, command)
39+
}
40+
_ => "".to_string(), // TODO: Support more container types
41+
}
42+
}
43+
44+
pub fn format_exec_regex_pattern(self) -> String {
45+
match self {
46+
ContainerType::Toolbox | ContainerType::Podman | ContainerType::Docker => {
47+
r"(Exec=\s?)(.*)".to_string()
48+
}
49+
_ => "".to_string(),
50+
}
51+
}
52+
53+
pub fn format_desktop_exec(self, container_name: &str) -> String {
54+
match self {
55+
ContainerType::Toolbox => {
56+
format!(r"Exec=toolbox run -c {} ${{2}}", container_name)
57+
}
58+
ContainerType::Podman => {
59+
// TODO: Currently not always functional
60+
format!(
61+
r"Exec=sh -c 'podman container start {} && podman container exec {} ${{2}}'",
62+
container_name, container_name
63+
)
64+
}
65+
_ => "".to_string(), // TODO: Support more container types
66+
}
67+
}
68+
69+
pub fn format_name_regex_pattern(self) -> String {
70+
match self {
71+
ContainerType::Toolbox | ContainerType::Podman | ContainerType::Docker => {
72+
r"(Name=\s?)(.*)".to_string()
73+
}
74+
_ => "".to_string(),
75+
}
76+
}
77+
78+
pub fn format_desktop_name(self, container_name: &str) -> String {
79+
match self {
80+
ContainerType::Toolbox => {
81+
format!(r"Name=${{2}} ({})", container_name)
82+
}
83+
_ => "".to_string(), // TODO: Support more container types
84+
}
85+
}
86+
87+
pub fn format_start(self, container_name: &str) -> String {
88+
match self {
89+
ContainerType::Toolbox => {
90+
format!("toolbox run -c {} echo 'Started'", container_name)
91+
}
92+
_ => "".to_string(), // TODO: Support more container types
93+
}
94+
}
95+
}

Diff for: src/main.rs

+42-130
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use clap::Parser;
2-
use server::ServerError;
2+
use container_type::ContainerType;
3+
use ron::de::SpannedError;
4+
use serde::{Deserialize, Serialize};
5+
use std::error::Error;
6+
use std::fmt::Display;
37
use std::{env, fs, io};
48

59
use std::{fs::read_to_string, path::Path};
610

11+
mod container_type;
712
mod desktop_entry;
813
mod server;
914

@@ -13,150 +18,66 @@ mod server;
1318
struct Args {
1419
#[arg(short, long, requires = "server", value_name = "CONFIG_PATH")]
1520
/// [AS SERVER] Path to an alternate config for the program.
16-
/// Default is $HOME/.config/container-desktop-entries/containers.conf
21+
/// Default is $HOME/.config/container-desktop-entries/containers.ron
1722
config: Option<String>,
1823
}
1924

20-
#[derive(Debug, Clone, Copy)]
21-
enum ContainerType {
22-
Podman,
23-
Docker,
24-
Toolbox,
25-
Unknown,
25+
#[derive(Debug, Clone, Deserialize, Serialize)]
26+
struct ContainerList {
27+
pub containers: Vec<(String, ContainerType)>,
2628
}
2729

28-
impl From<String> for ContainerType {
29-
fn from(value: String) -> Self {
30-
match value.as_str() {
31-
"toolbox" => ContainerType::Toolbox,
32-
"docker" => ContainerType::Docker,
33-
"podman" => ContainerType::Podman,
34-
_ => ContainerType::Unknown,
35-
}
36-
}
37-
}
38-
impl From<ContainerType> for String {
39-
fn from(value: ContainerType) -> Self {
40-
match value {
41-
ContainerType::Toolbox => "toolbox".to_string(),
42-
ContainerType::Docker => "docker".to_string(),
43-
ContainerType::Podman => "podman".to_string(),
44-
ContainerType::Unknown => "".to_string(),
45-
}
46-
}
30+
#[derive(Debug)]
31+
enum CDEError {
32+
IO(io::Error),
33+
NoEnv(std::env::VarError),
34+
Ron(SpannedError),
4735
}
4836

49-
impl ContainerType {
50-
fn not_supported(self) -> bool {
51-
matches!(
52-
self,
53-
ContainerType::Docker | ContainerType::Podman | ContainerType::Unknown
54-
)
55-
}
56-
57-
fn format_copy(self, container_name: &str, from: &Path, to: &Path) -> String {
58-
match self {
59-
ContainerType::Toolbox => {
60-
format!(
61-
"podman container cp {}:{}/. {}/",
62-
container_name,
63-
from.to_str().unwrap(),
64-
to.to_str().unwrap()
65-
)
66-
}
67-
_ => "".to_string(), // TODO: Support more container types
68-
}
69-
}
70-
71-
fn format_exec(self, container_name: &str, command: &str) -> String {
72-
match self {
73-
ContainerType::Toolbox => {
74-
format!("toolbox run -c {} {}", container_name, command)
75-
}
76-
_ => "".to_string(), // TODO: Support more container types
77-
}
37+
impl Error for CDEError {
38+
fn source(&self) -> Option<&(dyn Error + 'static)> {
39+
None
7840
}
7941

80-
fn format_exec_regex_pattern(self) -> String {
81-
match self {
82-
ContainerType::Toolbox | ContainerType::Podman | ContainerType::Docker => {
83-
r"(Exec=\s?)(.*)".to_string()
84-
}
85-
_ => "".to_string(),
86-
}
42+
fn description(&self) -> &str {
43+
"description() is deprecated; use Display"
8744
}
8845

89-
fn format_desktop_exec(self, container_name: &str) -> String {
90-
match self {
91-
ContainerType::Toolbox => {
92-
format!(r"Exec=toolbox run -c {} ${{2}}", container_name)
93-
}
94-
ContainerType::Podman => {
95-
// TODO: Currently not always functional
96-
format!(
97-
r"Exec=sh -c 'podman container start {} && podman container exec {} ${{2}}'",
98-
container_name, container_name
99-
)
100-
}
101-
_ => "".to_string(), // TODO: Support more container types
102-
}
103-
}
104-
105-
fn format_name_regex_pattern(self) -> String {
106-
match self {
107-
ContainerType::Toolbox | ContainerType::Podman | ContainerType::Docker => {
108-
r"(Name=\s?)(.*)".to_string()
109-
}
110-
_ => "".to_string(),
111-
}
112-
}
113-
114-
fn format_desktop_name(self, container_name: &str) -> String {
115-
match self {
116-
ContainerType::Toolbox => {
117-
format!(r"Name=${{2}} ({})", container_name)
118-
}
119-
_ => "".to_string(), // TODO: Support more container types
120-
}
46+
fn cause(&self) -> Option<&dyn Error> {
47+
self.source()
12148
}
49+
}
12250

123-
fn format_start(self, container_name: &str) -> String {
51+
impl Display for CDEError {
52+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12453
match self {
125-
ContainerType::Toolbox => {
126-
format!("toolbox run -c {} echo 'Started'", container_name)
127-
}
128-
_ => "".to_string(), // TODO: Support more container types
54+
Self::IO(e) => e.fmt(f),
55+
Self::NoEnv(e) => e.fmt(f),
56+
Self::Ron(e) => e.fmt(f),
12957
}
13058
}
13159
}
13260

133-
#[derive(Debug)]
134-
enum Error {
135-
Server(ServerError),
136-
IO(io::Error),
137-
NoEnv(std::env::VarError),
138-
}
139-
140-
impl From<ServerError> for Error {
141-
fn from(value: ServerError) -> Self {
142-
Error::Server(value)
61+
impl From<io::Error> for CDEError {
62+
fn from(value: io::Error) -> Self {
63+
Self::IO(value)
14364
}
14465
}
14566

146-
impl From<io::Error> for Error {
147-
fn from(value: io::Error) -> Self {
148-
Error::IO(value)
67+
impl From<std::env::VarError> for CDEError {
68+
fn from(value: std::env::VarError) -> Self {
69+
Self::NoEnv(value)
14970
}
15071
}
15172

152-
impl From<std::env::VarError> for Error {
153-
fn from(value: std::env::VarError) -> Self {
154-
Error::NoEnv(value)
73+
impl From<SpannedError> for CDEError {
74+
fn from(value: SpannedError) -> Self {
75+
Self::Ron(value)
15576
}
15677
}
15778

15879
#[async_std::main]
159-
async fn main() -> Result<(), Error> {
80+
async fn main() -> Result<(), CDEError> {
16081
env_logger::init();
16182

16283
if !cfg!(target_os = "linux") {
@@ -167,7 +88,7 @@ async fn main() -> Result<(), Error> {
16788
let args = Args::parse();
16889

16990
let default_path_str = format!(
170-
"{}/.config/container-desktop-entries/containers.conf",
91+
"{}/.config/container-desktop-entries/containers.ron",
17192
env::var("HOME")?
17293
);
17394
let conf_path = match args.config.as_ref() {
@@ -181,18 +102,9 @@ async fn main() -> Result<(), Error> {
181102
}
182103
_ => {}
183104
}
184-
log::info!("Running as server! Getting config at '{:?}'", conf_path);
185-
let config_data = read_to_string(conf_path)?
186-
.lines()
187-
.map(|s| {
188-
let ss = s
189-
.split_once(" ")
190-
.expect("Config invalid. make sure all lines are <<NAME>> <<TYPE>>");
191-
(ss.0.to_string(), ContainerType::from(ss.1.to_string()))
192-
})
193-
.collect::<Vec<_>>();
194-
195-
server::server(config_data).await?;
105+
let config_data: ContainerList = ron::from_str(&read_to_string(conf_path)?)?;
106+
107+
server::server(config_data).await;
196108

197109
Ok(())
198110
}

0 commit comments

Comments
 (0)