1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{diag, diagnostics::Diagnostic, parser::syntax::make_loc};
use move_ir_types::location::*;

pub fn decode(loc: Loc, s: &str) -> Result<Vec<u8>, Diagnostic> {
    match hex::decode(s) {
        Ok(vec) => Ok(vec),
        Err(hex::FromHexError::InvalidHexCharacter { c, index }) => {
            let filename = loc.file();
            let start_offset = loc.start() as usize;
            let offset = start_offset + 2 + index;
            let loc = make_loc(filename, offset, offset);
            Err(diag!(
                Syntax::InvalidHexString,
                (loc, format!("Invalid hexadecimal character: '{}'", c)),
            ))
        }
        Err(hex::FromHexError::OddLength) => Err(diag!(
            Syntax::InvalidHexString,
            (
                loc,
                "Odd number of characters in hex string. Expected 2 hexadecimal digits for each \
                 byte"
                    .to_string(),
            )
        )),
        Err(_) => unreachable!("unexpected error parsing hex byte string value"),
    }
}