ephemeris_release/adapters/utf8.rs
1//! Decoding of file bytes shared by the adapters.
2
3use std::path::{Path, PathBuf};
4use std::str;
5
6/// A file is not UTF-8 text.
7#[derive(Debug, thiserror::Error)]
8#[error("{} is not UTF-8 text", path.display())]
9pub(crate) struct NotUtf8 {
10 /// The file.
11 pub path: PathBuf,
12 /// The decoding failure.
13 #[source]
14 pub source: str::Utf8Error,
15}
16
17/// The text of `bytes`, which were read from `path`.
18pub(super) fn decode<'a>(path: &Path, bytes: &'a [u8]) -> Result<&'a str, NotUtf8> {
19 str::from_utf8(bytes).map_err(|source| NotUtf8 {
20 path: path.to_path_buf(),
21 source,
22 })
23}