(
algorithm: GitHashAlgorithm,
bytes: &[u8],
)
| 1549 | } |
| 1550 | |
| 1551 | fn parse_gitlink_bytes( |
| 1552 | algorithm: GitHashAlgorithm, |
| 1553 | bytes: &[u8], |
| 1554 | ) -> Result<GitObjectId, ProjectTreeError> { |
| 1555 | let expected = match algorithm { |
| 1556 | GitHashAlgorithm::Sha1 => 40, |
| 1557 | GitHashAlgorithm::Sha256 => 64, |
| 1558 | }; |
| 1559 | if bytes.len() != expected || !bytes.iter().all(u8::is_ascii_hexdigit) { |
| 1560 | return Err(ProjectTreeError::InvalidGitlink(format!( |
| 1561 | "expected {expected} hexadecimal bytes" |
| 1562 | ))); |
| 1563 | } |
| 1564 | let mut decoded = Vec::with_capacity(expected / 2); |
| 1565 | for pair in bytes.as_chunks::<2>().0 { |
| 1566 | let text = std::str::from_utf8(pair).expect("ASCII hex checked"); |
| 1567 | decoded.push(u8::from_str_radix(text, 16).expect("hex checked")); |
| 1568 | } |
| 1569 | GitObjectId::new(algorithm, decoded) |
| 1570 | .map_err(|error| ProjectTreeError::GitObjectId(error.to_string())) |
| 1571 | } |
| 1572 | |
| 1573 | fn repo_error(error: impl fmt::Display) -> ProjectTreeError { |
| 1574 | ProjectTreeError::Repository(error.to_string()) |
no test coverage detected