dynamic_loader_cache/
errors.rs

1// Copyright 2024-2025 Koutheir Attouchi.
2// See the "LICENSE.txt" file at the top-level directory of this distribution.
3//
4// Licensed under the MIT license. This file may not be copied, modified,
5// or distributed except according to those terms.
6
7use std::path::PathBuf;
8
9/// Information about a failure of an operation.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12#[allow(missing_docs)]
13pub enum Error {
14    #[error("failed to read directory")]
15    ReadDir {
16        path: PathBuf,
17        #[source]
18        source: std::io::Error,
19    },
20
21    #[error("failed to open file. Path: {path}")]
22    Open {
23        path: PathBuf,
24        #[source]
25        source: std::io::Error,
26    },
27
28    #[error("failed to map file. Path: {path}")]
29    MapFile {
30        path: PathBuf,
31        #[source]
32        source: std::io::Error,
33    },
34
35    #[error("failed to read metadata. Path: {path}")]
36    ReadMetaData {
37        path: PathBuf,
38        #[source]
39        source: std::io::Error,
40    },
41
42    #[error("file is empty. Path: {path}")]
43    FileIsEmpty { path: PathBuf },
44
45    #[error("parsing failed. Path: {path}")]
46    Parse {
47        path: PathBuf,
48        #[source]
49        source: nom::Err<nom::error::Error<usize>>,
50    },
51
52    #[error("offset is invalid. Path: {path}")]
53    OffsetIsInvalid { path: PathBuf },
54
55    #[error(transparent)]
56    TryFromInt(#[from] core::num::TryFromIntError),
57
58    #[error(transparent)]
59    FromBytesWithNul(#[from] core::ffi::FromBytesWithNulError),
60
61    #[error(transparent)]
62    FromBytesUntilNul(#[from] core::ffi::FromBytesUntilNulError),
63
64    #[error(transparent)]
65    Utf8(#[from] core::str::Utf8Error),
66}
67
68impl Error {
69    pub(crate) fn from_nom_parse(
70        source: nom::Err<nom::error::Error<&[u8]>>,
71        bytes: &[u8],
72        path: impl Into<PathBuf>,
73    ) -> Self {
74        Self::Parse {
75            path: path.into(),
76            source: source.map(|r| nom::error::Error {
77                input: bytes.len().saturating_sub(r.input.len()),
78                code: r.code,
79            }),
80        }
81    }
82}