Skip to content

Commit

Permalink
Clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Wünsche committed Nov 7, 2021
1 parent bfcce87 commit fa47ac9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 65 deletions.
20 changes: 9 additions & 11 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn process_img(pt: ImagePoint) -> Result<()> {

// Add to Background Structure
pt.background.images.push(Static {
duration: 1 as f32,
duration: 1f32,
file: format!("{}/{}.png", pt.parent_directory.to_string_lossy(), pt.index),
idx: pt.index,
});
Expand Down Expand Up @@ -62,12 +62,11 @@ pub fn process_img(pt: ImagePoint) -> Result<()> {
}

pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) -> Result<()> {
println!("{}: {}", "Conversion".yellow(), "Done!");
println!("{}: Done!", "Conversion".yellow());

println!(
"{}: {}",
"{}: Creating xml description for new wallpaper",
"Conversion".green(),
"Creating xml description for new wallpaper"
);
xml.images.sort_by(|a, b| match (a, b) {
(
Expand All @@ -78,7 +77,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str)
idx: transition_idx,
..
},
) => static_idx.cmp(&transition_idx),
) => static_idx.cmp(transition_idx),
(
Static {
idx: static_idx, ..
Expand All @@ -87,7 +86,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str)
idx: transition_idx,
..
},
) => static_idx.cmp(&transition_idx),
) => static_idx.cmp(transition_idx),
(
Transition {
idx: static_idx, ..
Expand All @@ -96,7 +95,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str)
idx: transition_idx,
..
},
) => static_idx.cmp(&transition_idx),
) => static_idx.cmp(transition_idx),
(
Transition {
idx: static_idx, ..
Expand All @@ -105,13 +104,12 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str)
idx: transition_idx,
..
},
) => static_idx.cmp(&transition_idx),
) => static_idx.cmp(transition_idx),
});

println!(
"{}: {}",
"{}: Writing wallpaper description",
"Conversion".green(),
"Writing wallpaper description"
);
let result_file = std::fs::OpenOptions::new()
.write(true)
Expand All @@ -124,7 +122,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str)
))?;
let mut result = BufWriter::new(result_file);
let mut ser = GnomeXMLBackgroundSerializer::new(&mut result);
ser.serialize(&xml)?;
ser.serialize(xml)?;
println!("{}", "Done".green());
Ok(())
}
40 changes: 10 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() -> Result<()> {
.get_matches();
let path = matches
.value_of(INPUT)
.ok_or(anyhow::Error::msg("Could not read INPUT"))?;
.ok_or_else(|| anyhow::Error::msg("Could not read INPUT"))?;

let parent_directory;
if matches.is_present(DIR) {
Expand All @@ -47,35 +47,18 @@ fn main() -> Result<()> {
}
parent_directory = nu_path.canonicalize()?;
} else {
let p = std::path::Path::new(path)
parent_directory = std::path::Path::new(path)
.ancestors()
.nth(1);

if p.is_none() {
return Err(anyhow::Error::msg("Cannot get parent of given image path."));
}
let pd = p.unwrap()
.canonicalize();

if let Err(e) = pd {
let msg = format!("Cannot get absolute path: {}", e);
return Err(anyhow::Error::msg(msg));
}
parent_directory = pd.unwrap();
}
let image_ctx_opt = HeifContext::read_from_file(path);

if let Err(e) = image_ctx_opt {
let msg = format!("{}", e.message);
return Err(anyhow::Error::msg(msg))
.nth(1)
.ok_or_else(|| anyhow::Error::msg("Cannot get parent of given image path."))?
.canonicalize()?;
}
let image_ctx = image_ctx_opt.unwrap();
let image_ctx = HeifContext::read_from_file(path)?;

// FETCH file wide metadata
println!(
"{}: {}",
"{}: Fetch metadata from image",
"Preparation".bright_blue(),
"Fetch metadata from image"
);
let base64plist = metadata::get_wallpaper_metadata(&image_ctx);

Expand All @@ -89,16 +72,14 @@ fn main() -> Result<()> {
.to_string_lossy();

println!(
"{}: {}",
"{}: Detecting wallpaper description kind",
"Preparation".bright_blue(),
"Detecting wallpaper description kind"
);
match base64plist.unwrap() {
metadata::WallPaperMode::H24(content) => {
println!(
"{}: {}",
"{}: Detected time-based wallpaper",
"Preparation".bright_blue(),
"Detected time-based wallpaper"
);
timebased::compute_time_based_wallpaper(
image_ctx,
Expand All @@ -109,9 +90,8 @@ fn main() -> Result<()> {
}
metadata::WallPaperMode::Solar(content) => {
println!(
"{}: {}",
"{}: Detected solar-based wallpaper",
"Preparation".bright_blue(),
"Detected solar-based wallpaper"
);
solar::compute_solar_based_wallpaper(image_ctx, content, &parent_directory, &image_name)
}
Expand Down
6 changes: 3 additions & 3 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub fn get_wallpaper_metadata(image_ctx: &HeifContext) -> Option<WallPaperMode>
.get(0)
.expect("Could not get metadata information");
let base64plist = {
let foo = image_ctx
let tmp = image_ctx
.primary_image_handle()
.unwrap()
.metadata(*metadata_id)
.unwrap();
let content = String::from_utf8_lossy(&foo);
let content = String::from_utf8_lossy(&tmp);
//println!("{:?}", content);
let mut reader = Reader::from_str(&content);
reader.trim_text(true);
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn get_wallpaper_metadata(image_ctx: &HeifContext) -> Option<WallPaperMode>
}
h24
};
return base64plist;
base64plist
}

pub fn get_time_plist_from_base64(input: &str) -> Result<WallpaperMetaTime> {
Expand Down
25 changes: 11 additions & 14 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ where

pub fn serialize(&mut self, background: &Background) -> Result<()> {
// By definition we can only find one starttime
match background.starttime {
StartTime {
let StartTime {
year,
month,
day,
hour,
minute,
second,
} => {
writeln!(self.writer, "<background>")?;
writeln!(self.writer, "\t<starttime>")?;
writeln!(self.writer, "\t\t<year>{}</year>", year)?;
writeln!(self.writer, "\t\t<month>{}</month>", month)?;
writeln!(self.writer, "\t\t<day>{}</day>", day)?;
writeln!(self.writer, "\t\t<hour>{}</hour>", hour)?;
writeln!(self.writer, "\t\t<minute>{}</minute>", minute)?;
writeln!(self.writer, "\t\t<second>{}</second>", second)?;
writeln!(self.writer, "\t</starttime>")?;
}
}
} = background.starttime;
writeln!(self.writer, "<background>")?;
writeln!(self.writer, "\t<starttime>")?;
writeln!(self.writer, "\t\t<year>{}</year>", year)?;
writeln!(self.writer, "\t\t<month>{}</month>", month)?;
writeln!(self.writer, "\t\t<day>{}</day>", day)?;
writeln!(self.writer, "\t\t<hour>{}</hour>", hour)?;
writeln!(self.writer, "\t\t<minute>{}</minute>", minute)?;
writeln!(self.writer, "\t\t<second>{}</second>", second)?;
writeln!(self.writer, "\t</starttime>")?;

for entry in background.images.iter() {
match entry {
Expand Down
7 changes: 2 additions & 5 deletions src/timebased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ pub fn compute_time_based_wallpaper(

let number_of_images = image_ctx.number_of_top_level_images();
println!(
"{}: {} {} {}",
"{}: Found {} images",
"Preparation".bright_blue(),
"Found",
number_of_images,
"images"
);
let mut image_ids = vec![0u32; number_of_images];
image_ctx.top_level_image_ids(&mut image_ids);
println!(
"{}: {}",
"{}: Converting embedded images to png format",
"Conversion".yellow(),
"Converting embedded images to png format"
);
let pb = ProgressBar::new(number_of_images as u64).with_style(
ProgressStyle::default_bar()
Expand Down
3 changes: 1 addition & 2 deletions src/util/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ pub fn write_png(path: &str, handle: ImageHandle) -> Result<()> {
return Ok(());
}
println!(
"{}: {}",
"{}: Could not determine color space. Colorspace RGB C444 could not be applied",
"Error".red(),
"Could not determine color space. Colorspace RGB C444 could not be applied"
);
Err(anyhow::Error::msg("Colorspace invalid"))
}

0 comments on commit fa47ac9

Please sign in to comment.