Skip to content

Commit

Permalink
Made code more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
nik012003 committed May 28, 2023
1 parent 9483626 commit f294bfa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -11,7 +11,7 @@ exclude = ["/.vscode"]
clap = { version = "4.3.0", features = ["derive"] }
mpvipc = "1.2.2"
tempfile = "3.5.0"
tokio = { version = "1.28.1" , features = ["full"] }
tokio = { version = "1.28.1", features = ["full"] }
url = "2.3.1"

[profile.release]
Expand Down
21 changes: 13 additions & 8 deletions src/main.rs
Expand Up @@ -115,13 +115,16 @@ async fn main() {
let state = Arc::new(Mutex::new(Shared::new()));

let cloned_state = Arc::clone(&state);
let mpv_socket = start_mpv(args.accept_source, args.mpv_args);
let mpv_socket =
start_mpv(args.accept_source, args.mpv_args).expect("Coudln't start or connect to mpv");

let settings = Settings {
is_serving: args.serve,
username: args.username,
accept_source: args.accept_source,
standalone: args.standalone,
};

// Handle server
if args.serve {
let listener = TcpListener::bind(&args.address)
Expand Down Expand Up @@ -170,13 +173,16 @@ async fn main() {
}
}

fn start_mpv(accept_source: bool, mpv_args: Vec<String>) -> String {
fn start_mpv(accept_source: bool, mpv_args: Vec<String>) -> Result<String, Error> {
// generate temp path for the socket
let binding = tempdir()
.expect("Failed to create a tmp directory for the mpv socket")
.into_path()
.join("mpv.sock");
let mpv_socket = binding.to_str().unwrap().to_owned();
let mpv_socket = binding
.to_str()
.expect("Path isn't valid unicode")
.to_owned();

// start mpv
let mut gui_mode_args = vec![];
Expand All @@ -198,11 +204,10 @@ fn start_mpv(accept_source: bool, mpv_args: Vec<String>) -> String {
while mpv.is_err() {
mpv = Mpv::connect(mpv_socket.as_str());
}
let mpv = mpv.unwrap();
mpv.pause().unwrap();
let mpv = mpv?;
mpv.pause()?;

mpv.run_command_raw("show-text", &["Connected to voyeurs", "5000"])
.unwrap();
mpv.run_command_raw("show-text", &["Connected to voyeurs", "5000"])?;

mpv_socket
Ok(mpv_socket)
}
4 changes: 2 additions & 2 deletions src/proto.rs
Expand Up @@ -174,7 +174,7 @@ impl VoyeursCommand {
args[2..].to_vec(),
)?))
}
0x01 => Ok(VoyeursCommand::Ready(*args.first().unwrap() == 1)),
0x01 => Ok(VoyeursCommand::Ready(*args.first().ok_or(TooShort)? == 1)),
0x02 => {
let time: f64 = f64::from_be_bytes(args.get(0..8).ok_or(TooShort)?.try_into()?);
Ok(VoyeursCommand::Seek(time))
Expand All @@ -193,7 +193,7 @@ impl VoyeursCommand {
pub fn craft_packet(self) -> Packet {
let timestamp: TsSize = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.expect("Couldn't get system time")
.as_millis() as TsSize;
Packet {
timestamp,
Expand Down

0 comments on commit f294bfa

Please sign in to comment.