give 'str' variables more appropriate names
[nihav.git] / nihav-core / src / formats.rs
index bb47838c252018d497e509f970d64acd7cc9da4e..810fff25ddc1a0de03896ac659df1fbf6737e6a9 100644 (file)
@@ -62,6 +62,8 @@ pub const SND_U8_FORMAT: NASoniton = NASoniton { bits: 8, be: false, packed: fal
 pub const SND_S16_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: false, float: false, signed: true };
 /// Predefined format for planar 16-bit signed audio.
 pub const SND_S16P_FORMAT: NASoniton = NASoniton { bits: 16, be: false, packed: false, planar: true, float: false, signed: true };
+/// Predefined format for planar 32-bit signed audio.
+pub const SND_S32P_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: true, float: false, signed: true };
 /// Predefined format for planar 32-bit floating point audio.
 pub const SND_F32P_FORMAT: NASoniton = NASoniton { bits: 32, be: false, packed: false, planar: true, float: true, signed: true };
 
@@ -101,7 +103,7 @@ impl NASoniton {
     }
 
     /// Returns soniton description as a short string.
-    pub fn to_short_string(&self) -> String {
+    pub fn to_short_string(self) -> String {
         let ltype = if self.float { 'f' } else if self.signed { 's' } else { 'u' };
         let endianness = if self.bits == 8 { "" } else if self.be { "be" } else { "le" };
         let planar = if self.planar { "p" } else { "" };
@@ -114,7 +116,7 @@ impl fmt::Display for NASoniton {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let fmt = if self.float { "float" } else if self.signed { "int" } else { "uint" };
         let end = if self.be { "BE" } else { "LE" };
-        write!(f, "({} bps, {} planar: {} packed: {} {})", self.bits, end, self.packed, self.planar, fmt)
+        write!(f, "({} bps, {} planar: {} packed: {} {})", self.bits, end, self.planar, self.packed, fmt)
     }
 }
 
@@ -146,35 +148,29 @@ pub enum NAChannelType {
 impl NAChannelType {
     /// Reports whether this is some center channel.
     pub fn is_center(self) -> bool {
-        match self {
-            NAChannelType::C => true,   NAChannelType::Ch => true,
-            NAChannelType::Cl => true,  NAChannelType::Ov => true,
-            NAChannelType::LFE => true, NAChannelType::LFE2 => true,
-            NAChannelType::Cs => true,  NAChannelType::Chs => true,
-            _ => false,
-        }
+        matches!(self,
+            NAChannelType::C   | NAChannelType::Ch |
+            NAChannelType::Cl  | NAChannelType::Ov |
+            NAChannelType::LFE | NAChannelType::LFE2 |
+            NAChannelType::Cs  | NAChannelType::Chs)
     }
     /// Reports whether this is some left channel.
     pub fn is_left(self) -> bool {
-        match self {
-            NAChannelType::L   => true, NAChannelType::Ls => true,
-            NAChannelType::Lss => true, NAChannelType::Lc => true,
-            NAChannelType::Lh  => true, NAChannelType::Lw => true,
-            NAChannelType::Lhs => true, NAChannelType::Ll => true,
-            NAChannelType::Lt  => true, NAChannelType::Lo => true,
-            _ => false,
-        }
+        matches!(self,
+            NAChannelType::L   | NAChannelType::Ls |
+            NAChannelType::Lss | NAChannelType::Lc |
+            NAChannelType::Lh  | NAChannelType::Lw |
+            NAChannelType::Lhs | NAChannelType::Ll |
+            NAChannelType::Lt  | NAChannelType::Lo)
     }
     /// Reports whether this is some right channel.
     pub fn is_right(self) -> bool {
-        match self {
-            NAChannelType::R   => true, NAChannelType::Rs => true,
-            NAChannelType::Rss => true, NAChannelType::Rc => true,
-            NAChannelType::Rh  => true, NAChannelType::Rw => true,
-            NAChannelType::Rhs => true, NAChannelType::Rl => true,
-            NAChannelType::Rt  => true, NAChannelType::Ro => true,
-            _ => false,
-        }
+        matches!(self,
+            NAChannelType::R   | NAChannelType::Rs |
+            NAChannelType::Rss | NAChannelType::Rc |
+            NAChannelType::Rh  | NAChannelType::Rw |
+            NAChannelType::Rhs | NAChannelType::Rl |
+            NAChannelType::Rt  | NAChannelType::Ro)
     }
 }
 
@@ -398,17 +394,11 @@ impl ColorModel {
     }
     /// Reports whether the current colour model is RGB.
     pub fn is_rgb(self) -> bool {
-        match self {
-            ColorModel::RGB(_) => true,
-            _ => false,
-        }
+        matches!(self, ColorModel::RGB(_))
     }
     /// Reports whether the current colour model is YUV.
     pub fn is_yuv(self) -> bool {
-        match self {
-            ColorModel::YUV(_) => true,
-            _ => false,
-        }
+        matches!(self, ColorModel::YUV(_))
     }
     /// Returns short name for the current colour mode.
     pub fn get_short_name(self) -> &'static str {
@@ -426,8 +416,8 @@ impl ColorModel {
 impl fmt::Display for ColorModel {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let name = match *self {
-            ColorModel::RGB(fmt) => format!("RGB({})", fmt).to_string(),
-            ColorModel::YUV(fmt) => format!("YUV({})", fmt).to_string(),
+            ColorModel::RGB(fmt) => format!("RGB({})", fmt),
+            ColorModel::YUV(fmt) => format!("YUV({})", fmt),
             ColorModel::CMYK     => "CMYK".to_string(),
             ColorModel::HSV      => "HSV".to_string(),
             ColorModel::LAB      => "LAB".to_string(),
@@ -708,6 +698,7 @@ impl NAPixelFormaton {
         }
         ssamp
     }
+    #[allow(clippy::cognitive_complexity)]
     /// Returns a short string description of the format if possible.
     pub fn to_short_string(&self) -> Option<String> {
         match self.model {
@@ -829,13 +820,13 @@ impl fmt::Display for NAPixelFormaton {
         let end = if self.be { "BE" } else { "LE" };
         let palstr = if self.palette { "palette " } else { "" };
         let astr = if self.alpha { "alpha " } else { "" };
-        let mut str = format!("Formaton for {} ({}{}elem {} size {}): ", self.model, palstr, astr,end, self.elem_size);
+        let mut string = format!("Formaton for {} ({}{}elem {} size {}): ", self.model, palstr, astr,end, self.elem_size);
         for i in 0..self.comp_info.len() {
             if let Some(chr) = self.comp_info[i] {
-                str = format!("{} {}", str, chr);
+                string = format!("{} {}", string, chr);
             }
         }
-        write!(f, "[{}]", str)
+        write!(f, "[{}]", string)
     }
 }
 
@@ -857,7 +848,7 @@ fn parse_rgb_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
                     'A' | 'a' => { order[3] = i; has_alpha = true; },
                     '0'..='9' => {
                         pstate = 1; bits_start = i;
-                        bits = ((ch as u8) - b'0') as u32;
+                        bits = u32::from((ch as u8) - b'0');
                     },
                     _ => return Err(FormatParseError {}),
                 };
@@ -866,7 +857,7 @@ fn parse_rgb_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
                 if i > 4 + bits_start { return Err(FormatParseError {}); }
                 match ch {
                     '0'..='9' => {
-                        bits = (bits * 10) + (((ch as u8) - b'0') as u32);
+                        bits = (bits * 10) + u32::from((ch as u8) - b'0');
                     },
                     'B' | 'b' => { pstate = 2; }
                     'L' | 'l' => { pstate = 2; is_be = false; }
@@ -961,18 +952,18 @@ fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
             return Ok(NAPixelFormaton {
                     model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 1,
                     comp_info: [
-                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 1 }),
+                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1 }),
                         None, None, None, None],
-                    elem_size: 1, be: false, alpha: false, palette: false });
+                    elem_size: 1, be: true, alpha: false, palette: false });
         },
         "y8a" | "y400a" | "graya" => {
             return Ok(NAPixelFormaton {
                     model: ColorModel::YUV(YUVSubmodel::YUVJ), components: 2,
                     comp_info: [
-                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 2 }),
-                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 2 }),
+                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 2 }),
+                        Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 1, next_elem: 2 }),
                         None, None, None],
-                    elem_size: 1, be: false, alpha: true, palette: false });
+                    elem_size: 1, be: true, alpha: true, palette: false });
         },
         "uyvy" | "y422" => {
             return Ok(NAPixelFormaton {
@@ -1026,8 +1017,8 @@ fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
     let mut parse_end = components as usize;
     for ch in s.chars().skip(components as usize) {
         parse_end += 1;
-        if ch >= '0' && ch <= '9' {
-            format = format * 10 + (((ch as u8) - b'0') as u32);
+        if ('0'..='9').contains(&ch) {
+            format = format * 10 + u32::from((ch as u8) - b'0');
             if format > 444 { return Err(FormatParseError {}); }
         } else {
             is_planar = ch == 'p';
@@ -1038,7 +1029,7 @@ fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
     let depth = if s.len() == parse_end { 8 } else {
             let mut val = 0;
             for ch in s.chars().skip(parse_end) {
-                if ch >= '0' && ch <= '9' {
+                if ('0'..='9').contains(&ch) {
                     val = val * 10 + ((ch as u8) - b'0');
                     if val > 16 { return Err(FormatParseError {}); }
                 } else {
@@ -1062,8 +1053,8 @@ fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
             444 => [[0, 0], [0, 0], [0, 0], [0, 0]],
             _ => return Err(FormatParseError {}),
         };
-    for (chro, ss) in chromatons.iter_mut().take(components as usize).zip(subsamp.iter()) {
-        *chro = Some(NAPixelChromaton{ h_ss: ss[0], v_ss: ss[1], packed: !is_planar, depth, shift: 0, comp_offs: next_elem, next_elem });
+    for (i, (chro, ss)) in chromatons.iter_mut().take(components as usize).zip(subsamp.iter()).enumerate() {
+        *chro = Some(NAPixelChromaton{ h_ss: ss[0], v_ss: ss[1], packed: !is_planar, depth, shift: 0, comp_offs: if is_planar { i as u8 } else { next_elem }, next_elem });
     }
     Ok(NAPixelFormaton { model: ColorModel::YUV(YUVSubmodel::YUVJ),
                          components,
@@ -1075,6 +1066,7 @@ fn parse_yuv_format(s: &str) -> Result<NAPixelFormaton, FormatParseError> {
 impl FromStr for NAPixelFormaton {
     type Err = FormatParseError;
 
+    #[allow(clippy::single_match)]
     fn from_str(s: &str) -> Result<Self, Self::Err> {
         match s {
             "pal8" => return Ok(PAL8_FORMAT),