| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#}; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}; |
| 3 | |
| 4 | use anyhowrust-analyzer cargo anyhow 1.0.104 crate/::{bailrust-analyzer cargo anyhow 1.0.104 macros/bail!, Resultrust-analyzer cargo anyhow 1.0.104 Result#}; |
| 5 | use cfg_ifrust-analyzer cargo cfg-if 1.0.4 crate/::cfg_ifrust-analyzer cargo cfg-if 1.0.4 cfg_if!; |
| 6 | use walkdirrust-analyzer cargo walkdir 2.5.0 crate/::{DirEntryrust-analyzer cargo walkdir 2.5.0 dent/DirEntry#, WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#}; |
| 7 | |
| 8 | use qoirust-analyzer cargo qoi 0.4.1 crate/::{decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/decode_to_vec()., encode_to_vecrust-analyzer cargo qoi 0.4.1 encode/encode_to_vec().}; |
| 9 | |
| 10 | fn find_qoi_png_pairsrust-analyzer cargo qoi 0.4.1 find_qoi_png_pairs().(root: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#)> { |
| 11 | let root = root.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(); |
| 12 | |
| 13 | let get_ext = |
| 14 | |path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#| path.extensionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]extension().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().().to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().(); |
| 15 | let check_qoi_png_pair = |path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#| { |
| 16 | let (qoi, png) = (path.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().(), path.with_extensionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]with_extension().("png")); |
| 17 | if qoi.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() && get_ext(&qoi) rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]eq()."qoi" && png.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() { |
| 18 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((qoi, png)) |
| 19 | } else { |
| 20 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | let mut out = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 25 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pair) = check_qoi_png_pair(root) { |
| 26 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(pair); |
| 27 | } else if root.is_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_dir().() { |
| 28 | out.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().( |
| 29 | WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#::newrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]new().(root) |
| 30 | .follow_linksrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]follow_links().(true) |
| 31 | .into_iterrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir][IntoIterator]into_iter().() |
| 32 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(Resultrust-analyzer cargo anyhow 1.0.104 Result#::okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().) |
| 33 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(DirEntryrust-analyzer cargo walkdir 2.5.0 dent/DirEntry#::into_pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]into_path().) |
| 34 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|p| check_qoi_png_pair(&p)), |
| 35 | ) |
| 36 | } |
| 37 | out |
| 38 | } |
| 39 | |
| 40 | struct Imagerust-analyzer cargo qoi 0.4.1 Image# { |
| 41 | pub widthrust-analyzer cargo qoi 0.4.1 Image#width.: u32, |
| 42 | pub heightrust-analyzer cargo qoi 0.4.1 Image#height.: u32, |
| 43 | pub channelsrust-analyzer cargo qoi 0.4.1 Image#channels.: u8, |
| 44 | pub datarust-analyzer cargo qoi 0.4.1 Image#data.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 45 | } |
| 46 | |
| 47 | impl Imagerust-analyzer cargo qoi 0.4.1 Image# { |
| 48 | fn from_pngrust-analyzer cargo qoi 0.4.1 impl#[Image]from_png().(filename: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> Resultrust-analyzer cargo anyhow 1.0.104 Result#<Selfrust-analyzer cargo qoi 0.4.1 impl#[Image]> { |
| 49 | let decoder = pngrust-analyzer cargo png 0.17.16 crate/::Decoderrust-analyzer cargo png 0.17.16 decoder/Decoder#::newrust-analyzer cargo png 0.17.16 decoder/impl#[`Decoder<R>`]new().(Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(filename)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 50 | let mut reader = decoder.read_inforust-analyzer cargo png 0.17.16 decoder/impl#[`Decoder<R>`]read_info().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 51 | let mut buf = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; reader.output_buffer_sizerust-analyzer cargo png 0.17.16 decoder/impl#[`Reader<R>`]output_buffer_size().()]; |
| 52 | let info = reader.next_framerust-analyzer cargo png 0.17.16 decoder/impl#[`Reader<R>`]next_frame().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 53 | let bytes = &buf[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#info.buffer_sizerust-analyzer cargo png 0.17.16 decoder/impl#[OutputInfo]buffer_size().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().; |
| 54 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo qoi 0.4.1 impl#[Image] { |
| 55 | widthrust-analyzer cargo qoi 0.4.1 Image#width.: info.widthrust-analyzer cargo png 0.17.16 decoder/OutputInfo#width., |
| 56 | heightrust-analyzer cargo qoi 0.4.1 Image#height.: info.heightrust-analyzer cargo png 0.17.16 decoder/OutputInfo#height., |
| 57 | channelsrust-analyzer cargo qoi 0.4.1 Image#channels.: info.color_typerust-analyzer cargo png 0.17.16 decoder/OutputInfo#color_type..samplesrust-analyzer cargo png 0.17.16 common/impl#[ColorType]samples().() as u8, |
| 58 | datarust-analyzer cargo qoi 0.4.1 Image#data.: bytes.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(), |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn compare_slicesrust-analyzer cargo qoi 0.4.1 compare_slices().(name: &str, desc: &str, result: &[u8], expected: &[u8]) -> Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 64 | if result rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().expected { |
| 65 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 66 | } else { |
| 67 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(i) = |
| 68 | (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#result.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(expected.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())).positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#position().(|i| result[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().expected[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 69 | { |
| 70 | bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!( |
| 71 | "{}: {} mismatch at byte {}: expected {:?}, got {:?}", |
| 72 | name, |
| 73 | desc, |
| 74 | i, |
| 75 | &expected[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..(i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 4).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(expected.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 76 | &result[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..(i +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 4).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(result.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().())]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., |
| 77 | ); |
| 78 | } else { |
| 79 | bailrust-analyzer cargo anyhow 1.0.104 macros/bail!!( |
| 80 | "{}: {} length mismatch: expected {}, got {}", |
| 81 | name, |
| 82 | desc, |
| 83 | expected.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), |
| 84 | result.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 91 | fn test_reference_imagesrust-analyzer cargo qoi 0.4.1 test_reference_images().() -> Resultrust-analyzer cargo anyhow 1.0.104 Result#<()> { |
| 92 | let pairs = find_qoi_png_pairsrust-analyzer cargo qoi 0.4.1 find_qoi_png_pairs().("assets"); |
| 93 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pairs.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().()); |
| 94 | |
| 95 | for (qoi_path, png_path) in &pairs { |
| 96 | let png_name = png_path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().(); |
| 97 | let img = Imagerust-analyzer cargo qoi 0.4.1 Image#::from_pngrust-analyzer cargo qoi 0.4.1 impl#[Image]from_png().(png_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 98 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{} {} {} {}", png_name, img.widthrust-analyzer cargo qoi 0.4.1 Image#width., img.heightrust-analyzer cargo qoi 0.4.1 Image#height., img.channelsrust-analyzer cargo qoi 0.4.1 Image#channels.); |
| 99 | let encoded = encode_to_vecrust-analyzer cargo qoi 0.4.1 encode/encode_to_vec().(&img.datarust-analyzer cargo qoi 0.4.1 Image#data., img.widthrust-analyzer cargo qoi 0.4.1 Image#width., img.heightrust-analyzer cargo qoi 0.4.1 Image#height.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 100 | let expected = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read().(qoi_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 101 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(encoded.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), expected.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); // this should match regardless |
| 102 | cfg_ifrust-analyzer cargo cfg-if 1.0.4 crate/! { |
| 103 | if #[cfg(feature = "reference")] { |
| 104 | compare_slicesrust-analyzer cargo qoi 0.4.1 compare_slices().(&png_name, "encoding", &encoded, &expected)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 105 | } |
| 106 | } |
| 107 | let (_header1, decoded1) = decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/decode_to_vec().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 108 | let (_header2, decoded2) = decode_to_vecrust-analyzer cargo qoi 0.4.1 decode/decode_to_vec().(&expected)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 109 | compare_slicesrust-analyzer cargo qoi 0.4.1 compare_slices().(&png_name, "decoding [1]", &decoded1, &img.datarust-analyzer cargo qoi 0.4.1 Image#data.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 110 | compare_slicesrust-analyzer cargo qoi 0.4.1 compare_slices().(&png_name, "decoding [2]", &decoded2, &img.datarust-analyzer cargo qoi 0.4.1 Image#data.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 111 | } |
| 112 | |
| 113 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 114 | } |