diff options
| author | Mike Buland <mike@xagasoft.com> | 2025-11-19 13:42:32 -0800 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2025-11-19 13:42:32 -0800 |
| commit | 0193a1fb63c0cefcb352d5be50bf160cb3418cd8 (patch) | |
| tree | ae7d10706c2e0fae9d17e3c0cc5c822f9aeb7f55 /rust/src/lib.rs | |
| parent | 61d7cdcc9ae82020f61d5c9ac1e8a2123ed7b9ac (diff) | |
| download | libgats-0193a1fb63c0cefcb352d5be50bf160cb3418cd8.tar.gz libgats-0193a1fb63c0cefcb352d5be50bf160cb3418cd8.tar.bz2 libgats-0193a1fb63c0cefcb352d5be50bf160cb3418cd8.tar.xz libgats-0193a1fb63c0cefcb352d5be50bf160cb3418cd8.zip | |
Applied clippy suggestions.
Learned some cool new things, otherwise it was pretty minor.
Diffstat (limited to 'rust/src/lib.rs')
| -rw-r--r-- | rust/src/lib.rs | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 5a5f96a..88d3410 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs | |||
| @@ -71,11 +71,11 @@ fn write_packed_int( x: i64 ) -> Vec<u8> | |||
| 71 | 0x00_u8 | 71 | 0x00_u8 |
| 72 | } | (v&0x3F) as u8 | if v > (v & 0x3F) { 0x80_u8 } else { 0x00_u8 }; | 72 | } | (v&0x3F) as u8 | if v > (v & 0x3F) { 0x80_u8 } else { 0x00_u8 }; |
| 73 | w.push(bb); | 73 | w.push(bb); |
| 74 | v = v >> 6; | 74 | v >>= 6; |
| 75 | while v > 0 { | 75 | while v > 0 { |
| 76 | bb = (v & 0x7F) as u8 | if v > (v & 0x7F) { 0x80_u8 } else { 0x00_u8 }; | 76 | bb = (v & 0x7F) as u8 | if v > (v & 0x7F) { 0x80_u8 } else { 0x00_u8 }; |
| 77 | w.push(bb); | 77 | w.push(bb); |
| 78 | v = v >> 7; | 78 | v >>= 7; |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | w | 81 | w |
| @@ -84,12 +84,12 @@ fn write_packed_int( x: i64 ) -> Vec<u8> | |||
| 84 | fn read_packed_int<R: Read>( r: &mut R ) -> Result<i64, Error> { | 84 | fn read_packed_int<R: Read>( r: &mut R ) -> Result<i64, Error> { |
| 85 | let mut out : i64; | 85 | let mut out : i64; |
| 86 | let mut bb = [0u8]; | 86 | let mut bb = [0u8]; |
| 87 | r.read( &mut bb )?; | 87 | r.read_exact( &mut bb )?; |
| 88 | let negative = (bb[0]&0x40_u8) == 0x40_u8; | 88 | let negative = (bb[0]&0x40_u8) == 0x40_u8; |
| 89 | out = (bb[0]&0x3F_u8) as i64; | 89 | out = (bb[0]&0x3F_u8) as i64; |
| 90 | let mut c = 0; | 90 | let mut c = 0; |
| 91 | while (bb[0]&0x80_u8) != 0 { | 91 | while (bb[0]&0x80_u8) != 0 { |
| 92 | r.read( &mut bb )?; | 92 | r.read_exact( &mut bb )?; |
| 93 | out |= ((bb[0]&0x7F_u8) as i64) << (6+7*c); | 93 | out |= ((bb[0]&0x7F_u8) as i64) << (6+7*c); |
| 94 | c += 1; | 94 | c += 1; |
| 95 | } | 95 | } |
| @@ -105,65 +105,65 @@ impl Value { | |||
| 105 | pub fn write<W: Write>(&self, w: &mut W) -> Result<(), Error> { | 105 | pub fn write<W: Write>(&self, w: &mut W) -> Result<(), Error> { |
| 106 | match self { | 106 | match self { |
| 107 | Self::Integer(x) => { | 107 | Self::Integer(x) => { |
| 108 | let bb = ['i' as u8]; | 108 | let bb = [b'i']; |
| 109 | w.write_all(&bb)?; | 109 | w.write_all(&bb)?; |
| 110 | w.write_all(&write_packed_int( *x ))?; | 110 | w.write_all(&write_packed_int( *x ))?; |
| 111 | }, | 111 | }, |
| 112 | Self::ByteString(s) => { | 112 | Self::ByteString(s) => { |
| 113 | let bb = ['s' as u8]; | 113 | let bb = [b's']; |
| 114 | w.write_all(&bb)?; | 114 | w.write_all(&bb)?; |
| 115 | w.write_all(&write_packed_int( s.len() as i64 ))?; | 115 | w.write_all(&write_packed_int( s.len() as i64 ))?; |
| 116 | w.write_all( &s )?; | 116 | w.write_all( s )?; |
| 117 | }, | 117 | }, |
| 118 | Self::Boolean(b) => { | 118 | Self::Boolean(b) => { |
| 119 | let mut bb = [0u8]; | 119 | let mut bb = [0u8]; |
| 120 | bb[0] = if *b { '1' } else { '0' } as u8; | 120 | bb[0] = if *b { b'1' } else { b'0' }; |
| 121 | w.write_all( &bb )?; | 121 | w.write_all( &bb )?; |
| 122 | }, | 122 | }, |
| 123 | Self::List(l) => { | 123 | Self::List(l) => { |
| 124 | let mut bb = ['l' as u8]; | 124 | let mut bb = [b'l']; |
| 125 | w.write_all(&bb)?; | 125 | w.write_all(&bb)?; |
| 126 | for v in l { | 126 | for v in l { |
| 127 | v.write( w )?; | 127 | v.write( w )?; |
| 128 | } | 128 | } |
| 129 | bb[0] = 'e' as u8; | 129 | bb[0] = b'e'; |
| 130 | w.write_all(&bb)?; | 130 | w.write_all(&bb)?; |
| 131 | }, | 131 | }, |
| 132 | Self::Dictionary(d) => { | 132 | Self::Dictionary(d) => { |
| 133 | let mut bb = ['d' as u8]; | 133 | let mut bb = [b'd']; |
| 134 | w.write_all(&bb)?; | 134 | w.write_all(&bb)?; |
| 135 | for (k, v) in d { | 135 | for (k, v) in d { |
| 136 | (Value::ByteString( k.clone().into_bytes() )).write( w )?; | 136 | (Value::ByteString( k.clone().into_bytes() )).write( w )?; |
| 137 | v.write( w )?; | 137 | v.write( w )?; |
| 138 | } | 138 | } |
| 139 | bb[0] = 'e' as u8; | 139 | bb[0] = b'e'; |
| 140 | w.write_all(&bb)?; | 140 | w.write_all(&bb)?; |
| 141 | }, | 141 | }, |
| 142 | Self::Float(f) => { | 142 | Self::Float(f) => { |
| 143 | match f.classify() { | 143 | match f.classify() { |
| 144 | FpCategory::Nan => { | 144 | FpCategory::Nan => { |
| 145 | let mut bb = ['F' as u8, 0u8]; | 145 | let mut bb = [b'F', 0u8]; |
| 146 | bb[1] = if f.is_sign_negative() { 'N' } else { 'n' } as u8; | 146 | bb[1] = if f.is_sign_negative() { 'N' } else { 'n' } as u8; |
| 147 | w.write_all( &bb )?; | 147 | w.write_all( &bb )?; |
| 148 | }, | 148 | }, |
| 149 | FpCategory::Infinite => { | 149 | FpCategory::Infinite => { |
| 150 | let mut bb = ['F' as u8, 0u8]; | 150 | let mut bb = [b'F', 0u8]; |
| 151 | bb[1] = if f.is_sign_negative() { 'I' } else { 'i' } as u8; | 151 | bb[1] = if f.is_sign_negative() { 'I' } else { 'i' } as u8; |
| 152 | w.write_all( &bb )?; | 152 | w.write_all( &bb )?; |
| 153 | }, | 153 | }, |
| 154 | FpCategory::Zero => { | 154 | FpCategory::Zero => { |
| 155 | let mut bb = ['F' as u8, 0u8]; | 155 | let mut bb = [b'F', 0u8]; |
| 156 | bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; | 156 | bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; |
| 157 | w.write_all( &bb )?; | 157 | w.write_all( &bb )?; |
| 158 | }, | 158 | }, |
| 159 | FpCategory::Subnormal => { | 159 | FpCategory::Subnormal => { |
| 160 | let mut bb = ['F' as u8, 0u8]; | 160 | let mut bb = [b'F', 0u8]; |
| 161 | // The format doesn't account for these...uh...make them zero? | 161 | // The format doesn't account for these...uh...make them zero? |
| 162 | bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; | 162 | bb[1] = if f.is_sign_negative() { 'Z' } else { 'z' } as u8; |
| 163 | w.write_all( &bb )?; | 163 | w.write_all( &bb )?; |
| 164 | }, | 164 | }, |
| 165 | FpCategory::Normal => { | 165 | FpCategory::Normal => { |
| 166 | let bb = ['f' as u8]; | 166 | let bb = [b'f']; |
| 167 | w.write_all( &bb )?; | 167 | w.write_all( &bb )?; |
| 168 | let mut bin = Vec::<u8>::with_capacity(10); | 168 | let mut bin = Vec::<u8>::with_capacity(10); |
| 169 | let (negative, d) = if f.is_sign_negative() { (true, -*f) } else { (false, *f) }; | 169 | let (negative, d) = if f.is_sign_negative() { (true, -*f) } else { (false, *f) }; |
| @@ -173,7 +173,7 @@ impl Value { | |||
| 173 | bin.push( d as u8 ); | 173 | bin.push( d as u8 ); |
| 174 | d = d.fract(); | 174 | d = d.fract(); |
| 175 | for _ in 0..15 { | 175 | for _ in 0..15 { |
| 176 | d = d*256.0; | 176 | d *= 256.0; |
| 177 | bin.push( d as u8 ); | 177 | bin.push( d as u8 ); |
| 178 | d = d.fract(); | 178 | d = d.fract(); |
| 179 | if d == 0.0 { | 179 | if d == 0.0 { |
| @@ -191,7 +191,7 @@ impl Value { | |||
| 191 | } | 191 | } |
| 192 | }, | 192 | }, |
| 193 | Self::Nothing => { | 193 | Self::Nothing => { |
| 194 | let bb = ['n' as u8]; | 194 | let bb = [b'n']; |
| 195 | w.write_all( &bb )?; | 195 | w.write_all( &bb )?; |
| 196 | }, | 196 | }, |
| 197 | } | 197 | } |
| @@ -218,11 +218,8 @@ impl Value { | |||
| 218 | } | 218 | } |
| 219 | 'l' => { | 219 | 'l' => { |
| 220 | let mut body = Vec::<Value>::new(); | 220 | let mut body = Vec::<Value>::new(); |
| 221 | loop { | 221 | while let ValueParse::Value(v) = Value::read_parse(r)? { |
| 222 | match Value::read_parse(r)? { | 222 | body.push(v); |
| 223 | ValueParse::Value(v) => body.push(v), | ||
| 224 | ValueParse::EndMarker => break, | ||
| 225 | } | ||
| 226 | } | 223 | } |
| 227 | Ok(ValueParse::Value(Value::List(body))) | 224 | Ok(ValueParse::Value(Value::List(body))) |
| 228 | } | 225 | } |
| @@ -263,27 +260,20 @@ impl Value { | |||
| 263 | 'i' => Ok(ValueParse::Value(Value::Float(f64::INFINITY))), | 260 | 'i' => Ok(ValueParse::Value(Value::Float(f64::INFINITY))), |
| 264 | 'Z' => Ok(ValueParse::Value(Value::Float(-0.0f64))), | 261 | 'Z' => Ok(ValueParse::Value(Value::Float(-0.0f64))), |
| 265 | 'z' => Ok(ValueParse::Value(Value::Float(0.0f64))), | 262 | 'z' => Ok(ValueParse::Value(Value::Float(0.0f64))), |
| 266 | _ => { | 263 | _ =>Err(Error::new(ErrorKind::InvalidData, "Unknown exceptional float subtype found.")), |
| 267 | return Err(Error::new(ErrorKind::InvalidData, "Unknown exceptional float subtype found.")); | ||
| 268 | } | ||
| 269 | } | 264 | } |
| 270 | } | 265 | } |
| 271 | 'd' => { | 266 | 'd' => { |
| 272 | let mut body = HashMap::new(); | 267 | let mut body = HashMap::new(); |
| 273 | loop { | 268 | while let ValueParse::Value(k) = Value::read_parse(r)? { |
| 274 | let k = match Value::read_parse(r)? { | 269 | let k = if let Value::ByteString(s) = k { |
| 275 | ValueParse::Value(v) => { | 270 | if let Ok(s) = String::from_utf8(s) { |
| 276 | if let Value::ByteString(s) = v { | 271 | s |
| 277 | if let Ok(s) = String::from_utf8(s) { | 272 | } else { |
| 278 | s | 273 | return Err(Error::new(ErrorKind::InvalidData, "Keys must be utf8 compatbile strings.")); |
| 279 | } else { | 274 | } |
| 280 | return Err(Error::new(ErrorKind::InvalidData, "Keys must be utf8 compatbile strings.")); | 275 | } else { |
| 281 | } | 276 | return Err(Error::new(ErrorKind::InvalidData, "Non-string cannot be dictionary key.")); |
| 282 | } else { | ||
| 283 | return Err(Error::new(ErrorKind::InvalidData, "Non-string cannot be dictionary key.")); | ||
| 284 | } | ||
| 285 | }, | ||
| 286 | ValueParse::EndMarker => break, | ||
| 287 | }; | 277 | }; |
| 288 | let v = match Value::read_parse(r)? { | 278 | let v = match Value::read_parse(r)? { |
| 289 | ValueParse::Value(v) => v, | 279 | ValueParse::Value(v) => v, |
| @@ -358,7 +348,7 @@ impl Value { | |||
| 358 | if r.len() != size as usize { | 348 | if r.len() != size as usize { |
| 359 | return Err(Error::new(ErrorKind::InvalidData, "Packet buffer is the wrong length.")); | 349 | return Err(Error::new(ErrorKind::InvalidData, "Packet buffer is the wrong length.")); |
| 360 | } | 350 | } |
| 361 | Ok(Value::read( &mut Cursor::new( &r[5..] ) )?) | 351 | Value::read( &mut Cursor::new( &r[5..] ) ) |
| 362 | } | 352 | } |
| 363 | 353 | ||
| 364 | pub fn read_packet<R: Read>( r: &mut R ) -> Result<Value,Error> { | 354 | pub fn read_packet<R: Read>( r: &mut R ) -> Result<Value,Error> { |
