zune_core/
result.rs

1/*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9//! Decoding results for images
10use alloc::vec::Vec;
11
12/// A simple enum that can hold decode
13/// results of most images
14#[non_exhaustive]
15pub enum DecodingResult {
16    U8(Vec<u8>),
17    U16(Vec<u16>),
18    F32(Vec<f32>)
19}
20
21impl DecodingResult {
22    /// Return the contents if the enum stores `Vec<u8>` or otherwise
23    /// return `None`.
24    ///
25    /// Useful for de-sugaring the result of a decoding operation
26    /// into raw bytes
27    ///
28    /// # Example
29    /// ```
30    /// use zune_core::result::DecodingResult;
31    /// let data = DecodingResult::U8(vec![0;100]);
32    /// // we know this won't fail because we created it with u8
33    /// assert!(data.u8().is_some());
34    ///
35    /// let data = DecodingResult::U16(vec![0;100]);
36    /// // it should now return nothing since the type is u18
37    /// assert!(data.u8().is_none());
38    ///
39    /// ```
40    pub fn u8(self) -> Option<Vec<u8>> {
41        match self {
42            DecodingResult::U8(data) => Some(data),
43            _ => None
44        }
45    }
46
47    /// Return the contents if the enum stores `Vec<u16>` or otherwise
48    /// return `None`.
49    ///
50    /// Useful for de-sugaring the result of a decoding operation
51    /// into raw bytes
52    ///
53    /// # Example
54    /// ```
55    /// use zune_core::result::DecodingResult;
56    /// let data = DecodingResult::U8(vec![0;100]);
57    /// // we know this will fail because we created it with u16
58    /// assert!(data.u16().is_none());
59    ///
60    ///
61    /// let data = DecodingResult::U16(vec![0;100]);
62    /// // it should now return something since the type is u16
63    /// assert!(data.u16().is_some());
64    ///
65    /// ```
66    pub fn u16(self) -> Option<Vec<u16>> {
67        match self {
68            DecodingResult::U16(data) => Some(data),
69            _ => None
70        }
71    }
72}