Skip to main content

tame_gcs/
util.rs

1#![doc(hidden)]
2
3pub(crate) fn to_hex(input: &[u8]) -> String {
4    const CHARS: &[u8] = b"0123456789abcdef";
5
6    let mut result = String::with_capacity(input.len() * 2);
7    for &byte in input {
8        result.push(CHARS[(byte >> 4) as usize] as char);
9        result.push(CHARS[(byte & 0xf) as usize] as char);
10    }
11
12    result
13}
14
15pub fn get_content_length(headers: &http::HeaderMap) -> Option<usize> {
16    headers.get(http::header::CONTENT_LENGTH).and_then(|h| {
17        h.to_str()
18            .map_err(|_err| ())
19            .and_then(|hv| hv.parse::<u64>().map(|l| l as usize).map_err(|_err| ()))
20            .ok()
21    })
22}
23
24#[allow(clippy::trivially_copy_pass_by_ref)]
25pub(crate) fn if_false(v: &bool) -> bool {
26    !v
27}
28
29pub(crate) const QUERY_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
30    .add(b' ')
31    .add(b'"')
32    .add(b'#')
33    .add(b'<')
34    .add(b'>');
35
36pub(crate) const PATH_ENCODE_SET: &percent_encoding::AsciiSet = &QUERY_ENCODE_SET
37    .add(b'`')
38    .add(b'?')
39    .add(b'{')
40    .add(b'}')
41    .add(b'%')
42    .add(b'/');
43
44#[cfg(test)]
45mod test {
46    #[test]
47    fn converts_to_hex() {
48        let expected = format!("{:x}", 1234529871u32);
49
50        let bytes = 1234529871u32.to_be_bytes();
51
52        assert_eq!(expected, super::to_hex(&bytes));
53    }
54}