gix_filter/worktree/
encoding.rs

1use bstr::BStr;
2use encoding_rs::Encoding;
3
4///
5pub mod for_label {
6    use bstr::BString;
7
8    /// The error returned by [for_label()][super::for_label()].
9    #[derive(Debug, thiserror::Error)]
10    #[allow(missing_docs)]
11    pub enum Error {
12        #[error("An encoding named '{name}' is not known")]
13        Unknown { name: BString },
14    }
15}
16
17/// Try to produce a new `Encoding` for `label` or report an error if it is not known.
18///
19/// ### Deviation
20///
21/// * There is no special handling of UTF-16LE/BE with checks if data contains a BOM or not, like `git` as we don't expect to have
22///   data available here.
23/// * Special `-BOM` suffixed versions of `UTF-16` encodings are not supported.
24pub fn for_label<'a>(label: impl Into<&'a BStr>) -> Result<&'static Encoding, for_label::Error> {
25    let mut label = label.into();
26    if label == "latin-1" {
27        label = "ISO-8859-1".into();
28    }
29    let enc = Encoding::for_label(label.as_ref()).ok_or_else(|| for_label::Error::Unknown { name: label.into() })?;
30    Ok(enc)
31}