Parse from lowercase hex (40 or 64 characters).
(hex: &str)
| 131 | |
| 132 | /// Parse from lowercase hex (40 or 64 characters). |
| 133 | pub fn from_hex(hex: &str) -> Result<Self, BindingEncodeError> { |
| 134 | match hex.len() { |
| 135 | 40 => { |
| 136 | let bytes = decode_fixed_hex(hex)?; |
| 137 | let oid: [u8; 20] = bytes |
| 138 | .try_into() |
| 139 | .map_err(|_| BindingEncodeError::OidWidth("sha1", hex.len()))?; |
| 140 | Ok(GitOid::Sha1(oid)) |
| 141 | } |
| 142 | 64 => { |
| 143 | let bytes = decode_fixed_hex(hex)?; |
| 144 | let oid: [u8; 32] = bytes |
| 145 | .try_into() |
| 146 | .map_err(|_| BindingEncodeError::OidWidth("sha256", hex.len()))?; |
| 147 | Ok(GitOid::Sha256(oid)) |
| 148 | } |
| 149 | _ => Err(BindingEncodeError::OidWidth("hex", hex.len())), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fn decode_fixed_hex(hex: &str) -> Result<Vec<u8>, BindingEncodeError> { |
nothing calls this directly
no test coverage detected