This commit is contained in:
Sashanoraa 2023-04-20 18:51:21 -04:00
parent 22109d7d31
commit 5d1396767f
3 changed files with 27 additions and 35 deletions

7
Cargo.lock generated
View file

@ -60,6 +60,12 @@ dependencies = [
"windows-sys 0.48.0", "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]] [[package]]
name = "async-broadcast" name = "async-broadcast"
version = "0.5.1" version = "0.5.1"
@ -772,6 +778,7 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
name = "rings" name = "rings"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"clap", "clap",
"protobuf", "protobuf",
"smol", "smol",

View file

@ -11,3 +11,4 @@ smol = "1.3.0"
zbus = "3.12.0" zbus = "3.12.0"
clap = { version = "4.2.4", features = ["derive"] } clap = { version = "4.2.4", features = ["derive"] }
uds = "0.2.6" uds = "0.2.6"
anyhow = "1.0.70"

View file

@ -1,71 +1,55 @@
mod args; mod args;
use anyhow::bail;
use args::Args; use args::Args;
use clap::Parser; use clap::Parser;
use uds::{UnixSeqpacketListener, UnixSeqpacketConn, UnixSocketAddr}; use uds::{UnixSeqpacketConn, UnixSeqpacketListener};
extern crate uds;
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let result = validate_arguments(&args); let result = validate_arguments(&args);
match result { match result {
Ok(_) => {}, Ok(_) => {}
Err(err) => println!("{}", err.message) Err(err) => println!("{err}"),
} }
} }
fn validate_arguments(args: &Args) -> Result<(), RingsError> { fn validate_arguments(args: &Args) -> anyhow::Result<()> {
if !args.server && !args.client { 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 { if args.server && args.client {
return Err(RingsError{message: "You can't have both flags active at the same time".to_string()}); bail!("You can't have both flags active at the same time");
} }
if args.server { if args.server {
server()?; server()?;
return Ok(());
} else if args.client { } else if args.client {
client(todo!())?;
return Ok(());
} }
Ok(()) Ok(())
} }
fn server() -> Result<(), RingsError> { fn server() -> anyhow::Result<()> {
create_rings_socket()?; create_rings_socket()?;
Ok(()) 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); let client = uds::UnixSeqpacketConn::connect_unix_addr(addr);
match client { match client {
Ok(client) => { Ok(_client) => {
return Err(RingsError { message: "to be implemented".to_string() }) todo!()
}, }
Err(_) => { Err(_) => {
return Err(RingsError { message: "to be written".to_string() }) todo!()
} }
} }
} }
fn create_rings_socket() -> Result<UnixSeqpacketListener, RingsError> { fn create_rings_socket() -> anyhow::Result<UnixSeqpacketListener> {
let addr = uds::UnixSocketAddr::new("@abstract"); let addr = uds::UnixSocketAddr::new("@abstract")?;
match addr {
Ok(addr) => {
let listener = UnixSeqpacketListener::bind_unix_addr(&addr).expect("create seqpacket listener"); let listener = UnixSeqpacketListener::bind_unix_addr(&addr).expect("create seqpacket listener");
return Ok(listener) Ok(listener)
},
Err(_) => {
return Err(RingsError { message: "boop".to_string() })
},
_ => {
return Err(RingsError { message: "something has gone wrong".to_string() })
}
};
}
struct RingsError {
message: String
} }