• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / gpt-disk-rs / 14182859342

31 Mar 2025 10:40PM CUT coverage: 94.803%. Remained the same
14182859342

Pull #277

github

web-flow
Merge 9a5384971 into 32b4a7e87
Pull Request #277: uguid: Unconditionally use the Error trait

1441 of 1520 relevant lines covered (94.8%)

13.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/uguid/src/util.rs
1
// Copyright 2022 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6
// option. This file may not be copied, modified, or distributed
7
// except according to those terms.
8

9
use crate::GuidFromStrError;
10

11
pub(crate) const fn byte_to_ascii_hex_lower(byte: u8) -> (u8, u8) {
178✔
12
    let mut l = byte & 0xf;
178✔
13
    let mut h = byte >> 4;
178✔
14
    if l <= 9 {
178✔
15
        l += b'0';
133✔
16
    } else {
133✔
17
        l += b'a' - 10;
45✔
18
    }
45✔
19
    if h <= 9 {
178✔
20
        h += b'0';
125✔
21
    } else {
125✔
22
        h += b'a' - 10;
53✔
23
    }
53✔
24
    (h, l)
178✔
25
}
178✔
26

27
/// Parse a hexadecimal ASCII character as a `u8`.
28
const fn parse_byte_from_ascii_char(c: u8) -> Option<u8> {
245✔
29
    match c {
245✔
30
        b'0' => Some(0x0),
14✔
31
        b'1' => Some(0x1),
15✔
32
        b'2' => Some(0x2),
16✔
33
        b'3' => Some(0x3),
16✔
34
        b'4' => Some(0x4),
16✔
35
        b'5' => Some(0x5),
16✔
36
        b'6' => Some(0x6),
16✔
37
        b'7' => Some(0x7),
16✔
38
        b'8' => Some(0x8),
15✔
39
        b'9' => Some(0x9),
14✔
40
        b'a' | b'A' => Some(0xa),
16✔
41
        b'b' | b'B' => Some(0xb),
14✔
42
        b'c' | b'C' => Some(0xc),
14✔
43
        b'd' | b'D' => Some(0xd),
14✔
44
        b'e' | b'E' => Some(0xe),
14✔
45
        b'f' | b'F' => Some(0xf),
15✔
46
        _ => None,
4✔
47
    }
48
}
245✔
49

50
/// Parse a pair of hexadecimal ASCII characters as a `u8`. For example,
51
/// `(b'1', b'a')` is parsed as `0x1a`.
52
const fn parse_byte_from_ascii_char_pair(a: u8, b: u8) -> Option<u8> {
124✔
53
    let Some(a) = parse_byte_from_ascii_char(a) else {
124✔
54
        return None;
3✔
55
    };
56

57
    let Some(b) = parse_byte_from_ascii_char(b) else {
121✔
58
        return None;
1✔
59
    };
60

61
    Some((a << 4) | b)
120✔
62
}
124✔
63

64
/// Parse a pair of hexadecimal ASCII characters at position `start` as
65
/// a `u8`.
66
pub(crate) const fn parse_byte_from_ascii_str_at(
120✔
67
    s: &[u8],
120✔
68
    start: u8,
120✔
69
) -> Result<u8, GuidFromStrError> {
120✔
70
    // This `as` conversion is needed because this is a const
120✔
71
    // function. It is always valid since `usize` is always bigger than
120✔
72
    // a u8.
120✔
73
    #![allow(clippy::as_conversions)]
120✔
74
    let start_usize = start as usize;
120✔
75

76
    if let Some(byte) =
118✔
77
        parse_byte_from_ascii_char_pair(s[start_usize], s[start_usize + 1])
120✔
78
    {
79
        Ok(byte)
118✔
80
    } else {
81
        Err(GuidFromStrError::Hex(start))
2✔
82
    }
83
}
120✔
84

85
#[cfg(test)]
86
mod tests {
87
    use super::*;
88

89
    #[test]
90
    fn test_to_ascii() {
1✔
91
        assert_eq!(byte_to_ascii_hex_lower(0x1f), (b'1', b'f'));
1✔
92
        assert_eq!(byte_to_ascii_hex_lower(0xf1), (b'f', b'1'));
1✔
93
    }
1✔
94

95
    #[test]
96
    fn test_parse() {
1✔
97
        assert_eq!(parse_byte_from_ascii_char_pair(b'1', b'a'), Some(0x1a));
1✔
98
        assert_eq!(parse_byte_from_ascii_char_pair(b'8', b'f'), Some(0x8f));
1✔
99

100
        assert_eq!(parse_byte_from_ascii_char_pair(b'g', b'a'), None);
1✔
101
        assert_eq!(parse_byte_from_ascii_char_pair(b'a', b'g'), None);
1✔
102
    }
1✔
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc