(
algorithm: GitHashAlgorithm,
bytes: &[u8],
)
| 1567 | } |
| 1568 | |
| 1569 | fn parse_gitlink_bytes( |
| 1570 | algorithm: GitHashAlgorithm, |
| 1571 | bytes: &[u8], |
| 1572 | ) -> Result<GitObjectId, ProjectTreeError> { |
| 1573 | let expected = match algorithm { |
| 1574 | GitHashAlgorithm::Sha1 => 40, |
| 1575 | GitHashAlgorithm::Sha256 => 64, |
| 1576 | }; |
| 1577 | if bytes.len() != expected || !bytes.iter().all(u8::is_ascii_hexdigit) { |
| 1578 | return Err(ProjectTreeError::InvalidGitlink(format!( |
| 1579 | "expected {expected} hexadecimal bytes" |
| 1580 | ))); |
| 1581 | } |
| 1582 | let mut decoded = Vec::with_capacity(expected / 2); |
| 1583 | for pair in bytes.as_chunks::<2>().0 { |
| 1584 | let text = std::str::from_utf8(pair).expect("ASCII hex checked"); |
| 1585 | decoded.push(u8::from_str_radix(text, 16).expect("hex checked")); |
| 1586 | } |
| 1587 | GitObjectId::new(algorithm, decoded) |
| 1588 | .map_err(|error| ProjectTreeError::GitObjectId(error.to_string())) |
| 1589 | } |
| 1590 | |
| 1591 | fn repo_error(error: impl fmt::Display) -> ProjectTreeError { |
| 1592 | ProjectTreeError::Repository(error.to_string()) |
no test coverage detected