Skip to content

Commit de37e11

Browse files
committed
refactor: remove more instances of unwrap
1 parent c3c4f3c commit de37e11

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/ffmpeg.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use std::{fs, io, process::Command};
22

3-
fn run_command(command: &str) {
4-
let cmd = Command::new("sh")
5-
.arg("-c")
6-
.arg(command)
7-
.output()
8-
.expect("Error");
3+
fn run_command(command: &str) -> io::Result<()> {
4+
let cmd = Command::new("sh").arg("-c").arg(command).output()?;
95

106
println!("status: {}", cmd.status);
117

@@ -18,34 +14,35 @@ fn run_command(command: &str) {
1814
let result = result.as_str();
1915

2016
println!("{}", result);
17+
Ok(())
2118
}
2219

2320
pub fn create_preview_gif(input: &str, subpath: &str, output: &str) -> io::Result<()> {
2421
let path = format!("{}/{}", output, subpath);
2522
if fs::metadata(&path).is_err() {
26-
fs::create_dir_all(&path).unwrap();
23+
fs::create_dir_all(&path)?;
2724
}
2825

2926
let command = format!(
3027
"ffmpeg -ss 30 -t 3 -i {} -vf \"fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\" -loop 0 {}/{}.gif", input, path, output
3128
);
3229

33-
run_command(&command);
30+
run_command(&command)?;
3431
Ok(())
3532
}
3633

3734
pub fn create_preview_image(input: &str, subpath: &str, output: &str) -> io::Result<()> {
3835
let path = format!("{}/{}", output, subpath);
3936
if fs::metadata(&path).is_err() {
40-
fs::create_dir_all(&path).unwrap();
37+
fs::create_dir_all(&path)?;
4138
}
4239

4340
let command = format!(
4441
"ffmpeg -i {} -vf scale=iw*sar:ih,setsar=1 -ss 00:00:05 -t 1 -vframes 1 {}/{}.jpg",
4542
input, path, output
4643
);
4744

48-
run_command(&command);
45+
run_command(&command)?;
4946
Ok(())
5047
}
5148

@@ -56,15 +53,15 @@ pub fn create_thumbnails(input: &str, subpath: &str, output: &str) -> io::Result
5653

5754
println!("Path: {}", thumbs_path);
5855
if fs::metadata(&thumbs_path).is_err() {
59-
fs::create_dir_all(&thumbs_path).unwrap();
56+
fs::create_dir_all(&thumbs_path)?;
6057
}
6158

6259
let command = format!(
6360
"ffmpeg -i {} -vf \"fps=1/4,scale=320:-1\" {}/img%03d.jpg",
6461
input, thumbs_path
6562
);
6663

67-
run_command(&command);
64+
run_command(&command)?;
6865
Ok(())
6966
}
7067

@@ -74,15 +71,15 @@ pub fn create_hls_encoding(input: &str, subpath: &str, output: &str) -> io::Resu
7471
let hls_path = format!("{}/{}", output, subpath);
7572

7673
if fs::metadata(&hls_path).is_err() {
77-
fs::create_dir_all(&hls_path).unwrap();
74+
fs::create_dir_all(&hls_path)?;
7875
}
7976

8077
// TODO: Sub add paths
8178
// let paths = ["hls", "dash"];
8279
// for path in paths {
8380
// let p = format!("{}/{}", path, output);
8481
// if !fs::metadata(&p).is_ok() {
85-
// fs::create_dir_all(&p).unwrap();
82+
// fs::create_dir_all(&p)?;
8683
// }
8784
// }
8885

@@ -98,6 +95,6 @@ pub fn create_hls_encoding(input: &str, subpath: &str, output: &str) -> io::Resu
9895
-hls_segment_filename \
9996
-hls_playlist_type {}/vod \
10097
{}/720p_%03d.m3u8 {}/720p.m3u8", input, hls_path, hls_path, hls_path);
101-
run_command(&command);
98+
run_command(&command)?;
10299
Ok(())
103100
}

src/fileio.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
use crate::metadata::*;
2-
use std::fs;
32
use std::fs::File;
43
use std::io::Write;
4+
use std::{fs, io};
55

66
pub fn file_to_hyphen(name: &str) -> String {
77
name.replace(['_', ' ', '.'], "-")
88
}
99

10-
pub fn write_metadata(data: &OutputMetadata) {
10+
pub fn write_metadata(data: &OutputMetadata) -> io::Result<()> {
1111
let directory = file_to_hyphen(&data.title);
1212

1313
if fs::metadata(&directory).is_err() {
14-
fs::create_dir(&directory).unwrap();
14+
fs::create_dir(&directory)?;
1515
}
1616

17-
let json = serde_json::to_string(&data).unwrap();
17+
let json = serde_json::to_string(&data)?;
1818
let outpath = format!("{}/{}.json", &directory, data.title);
19-
let mut file = File::create(outpath).unwrap();
20-
file.write_all(json.as_bytes()).unwrap();
19+
let mut file = File::create(outpath)?;
20+
file.write_all(json.as_bytes())?;
21+
Ok(())
2122
}
2223

2324
#[test]
@@ -33,7 +34,11 @@ fn write_metadata_test() {
3334
..Default::default()
3435
};
3536

36-
write_metadata(&output_metadata);
37+
let result = write_metadata(&output_metadata);
38+
match result {
39+
Ok(()) => {}
40+
Err(e) => panic!("Failed to write metadata file: {}", e),
41+
}
3742

3843
let file = File::open("temp/temp.json").expect("error opening file");
3944
let reader = std::io::BufReader::new(file);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn main() -> io::Result<()> {
102102

103103
let out = media_info::get_media_info(input);
104104
println!("{:#?}", &out);
105-
fileio::write_metadata(&out);
105+
fileio::write_metadata(&out)?;
106106

107107
let path = format!("./{}", file_to_hyphen(&out.title));
108108

0 commit comments

Comments
 (0)