Refactor
This commit is contained in:
parent
22109d7d31
commit
5d1396767f
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -60,6 +60,12 @@ dependencies = [
|
|||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.70"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4"
|
||||
|
||||
[[package]]
|
||||
name = "async-broadcast"
|
||||
version = "0.5.1"
|
||||
|
@ -772,6 +778,7 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
|||
name = "rings"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"protobuf",
|
||||
"smol",
|
||||
|
|
|
@ -11,3 +11,4 @@ smol = "1.3.0"
|
|||
zbus = "3.12.0"
|
||||
clap = { version = "4.2.4", features = ["derive"] }
|
||||
uds = "0.2.6"
|
||||
anyhow = "1.0.70"
|
||||
|
|
54
src/main.rs
54
src/main.rs
|
@ -1,71 +1,55 @@
|
|||
mod args;
|
||||
use anyhow::bail;
|
||||
use args::Args;
|
||||
use clap::Parser;
|
||||
use uds::{UnixSeqpacketListener, UnixSeqpacketConn, UnixSocketAddr};
|
||||
extern crate uds;
|
||||
use uds::{UnixSeqpacketConn, UnixSeqpacketListener};
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let result = validate_arguments(&args);
|
||||
match result {
|
||||
Ok(_) => {},
|
||||
Err(err) => println!("{}", err.message)
|
||||
Ok(_) => {}
|
||||
Err(err) => println!("{err}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_arguments(args: &Args) -> Result<(), RingsError> {
|
||||
fn validate_arguments(args: &Args) -> anyhow::Result<()> {
|
||||
if !args.server && !args.client {
|
||||
return Err(RingsError{message: "Please provide either --client or --server".to_string()});
|
||||
bail!("Please provide either --client or --server");
|
||||
}
|
||||
|
||||
if args.server && args.client {
|
||||
return Err(RingsError{message: "You can't have both flags active at the same time".to_string()});
|
||||
if args.server && args.client {
|
||||
bail!("You can't have both flags active at the same time");
|
||||
}
|
||||
|
||||
if args.server {
|
||||
server()?;
|
||||
return Ok(());
|
||||
} else if args.client {
|
||||
|
||||
return Ok(());
|
||||
client(todo!())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn server() -> Result<(), RingsError> {
|
||||
fn server() -> anyhow::Result<()> {
|
||||
create_rings_socket()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn client( addr: &uds::UnixSocketAddr ) -> Result<UnixSeqpacketConn, RingsError> {
|
||||
fn client(addr: &uds::UnixSocketAddr) -> anyhow::Result<UnixSeqpacketConn> {
|
||||
let client = uds::UnixSeqpacketConn::connect_unix_addr(addr);
|
||||
match client {
|
||||
Ok(client) => {
|
||||
return Err(RingsError { message: "to be implemented".to_string() })
|
||||
},
|
||||
Ok(_client) => {
|
||||
todo!()
|
||||
}
|
||||
Err(_) => {
|
||||
return Err(RingsError { message: "to be written".to_string() })
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_rings_socket() -> Result<UnixSeqpacketListener, RingsError> {
|
||||
let addr = uds::UnixSocketAddr::new("@abstract");
|
||||
match addr {
|
||||
Ok(addr) => {
|
||||
let listener = UnixSeqpacketListener::bind_unix_addr(&addr).expect("create seqpacket listener");
|
||||
return Ok(listener)
|
||||
},
|
||||
Err(_) => {
|
||||
return Err(RingsError { message: "boop".to_string() })
|
||||
},
|
||||
_ => {
|
||||
return Err(RingsError { message: "something has gone wrong".to_string() })
|
||||
}
|
||||
};
|
||||
fn create_rings_socket() -> anyhow::Result<UnixSeqpacketListener> {
|
||||
let addr = uds::UnixSocketAddr::new("@abstract")?;
|
||||
let listener = UnixSeqpacketListener::bind_unix_addr(&addr).expect("create seqpacket listener");
|
||||
Ok(listener)
|
||||
}
|
||||
|
||||
struct RingsError {
|
||||
message: String
|
||||
}
|
Loading…
Reference in a new issue