Skip to main content

ntex_error/
utils.rs

1use std::{
2    borrow::Cow, cell::RefCell, convert::Infallible, error::Error as StdError, fmt, io,
3    path::Path, path::PathBuf,
4};
5
6use ntex_bytes::ByteString;
7
8use crate::{Error, ErrorDiagnostic, ResultType};
9
10impl ErrorDiagnostic for Infallible {
11    type Kind = ResultType;
12
13    fn kind(&self) -> Self::Kind {
14        unreachable!()
15    }
16}
17
18impl ErrorDiagnostic for io::Error {
19    type Kind = ResultType;
20
21    fn kind(&self) -> Self::Kind {
22        match self.kind() {
23            io::ErrorKind::InvalidData
24            | io::ErrorKind::Unsupported
25            | io::ErrorKind::UnexpectedEof
26            | io::ErrorKind::BrokenPipe
27            | io::ErrorKind::ConnectionReset
28            | io::ErrorKind::ConnectionAborted
29            | io::ErrorKind::NotConnected
30            | io::ErrorKind::TimedOut => ResultType::ClientError,
31            _ => ResultType::ServiceError,
32        }
33    }
34}
35
36#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
37pub struct Success;
38
39impl StdError for Success {}
40
41impl ErrorDiagnostic for Success {
42    type Kind = ResultType;
43
44    fn kind(&self) -> Self::Kind {
45        ResultType::Success
46    }
47}
48
49impl fmt::Display for Success {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "Success")
52    }
53}
54
55/// Executes a future and ensures an error service is set.
56///
57/// If the error does not already have a service, the provided service is assigned.
58pub async fn with_service<F, T, E>(svc: &'static str, fut: F) -> F::Output
59where
60    F: Future<Output = Result<T, Error<E>>>,
61    E: ErrorDiagnostic + Clone,
62{
63    fut.await.map_err(|err: Error<E>| {
64        if err.service().is_none() {
65            err.set_service(svc)
66        } else {
67            err
68        }
69    })
70}
71
72/// Generates a module path from the given file path.
73pub fn module_path(file_path: &str) -> ByteString {
74    module_path_ext("", "", "::", "", file_path)
75}
76
77/// Generates a module path with a prefix from the given file path.
78pub fn module_path_prefix(prefix: &'static str, file_path: &str) -> ByteString {
79    module_path_ext(prefix, "", "::", "", file_path)
80}
81
82/// Generates a module path from a file path.
83pub fn module_path_fs(file_path: &str) -> ByteString {
84    module_path_ext("", "/src", "/", ".rs", file_path)
85}
86
87fn module_path_ext(
88    prefix: &'static str,
89    mod_sep: &str,
90    sep: &str,
91    suffix: &str,
92    file_path: &str,
93) -> ByteString {
94    type HashMap<K, V> = std::collections::HashMap<K, V, foldhash::fast::RandomState>;
95    thread_local! {
96        static CACHE: RefCell<HashMap<&'static str, HashMap<String, ByteString>>> = RefCell::new(HashMap::default());
97    }
98
99    let cached = CACHE.with(|cache| {
100        if let Some(c) = cache.borrow().get(prefix) {
101            c.get(file_path).cloned()
102        } else {
103            None
104        }
105    });
106
107    if let Some(cached) = cached {
108        cached
109    } else {
110        let normalized_file_path = normalize_file_path(file_path);
111        let (module_name, module_root) =
112            module_root_from_file(mod_sep, &normalized_file_path);
113        let module = module_path_from_file_with_root(
114            prefix,
115            sep,
116            &normalized_file_path,
117            &module_name,
118            &module_root,
119            suffix,
120        );
121
122        let _ = CACHE.with(|cache| {
123            cache
124                .borrow_mut()
125                .entry(prefix)
126                .or_default()
127                .insert(file_path.to_string(), module.clone())
128        });
129        module
130    }
131}
132
133fn normalize_file_path(file_path: &str) -> String {
134    let path = Path::new(file_path);
135    if path.is_absolute() {
136        return path.to_string_lossy().into_owned();
137    }
138
139    match std::env::current_dir() {
140        Ok(cwd) => cwd.join(path).to_string_lossy().into_owned(),
141        Err(_) => file_path.to_string(),
142    }
143}
144
145fn module_root_from_file(mod_sep: &str, file_path: &str) -> (String, PathBuf) {
146    let normalized = file_path.replace('\\', "/");
147    if let Some((root, _)) = normalized.rsplit_once("/src/") {
148        let mut root = PathBuf::from(root);
149        let mod_name = root
150            .file_name()
151            .map_or(Cow::Borrowed("crate"), |s| s.to_string_lossy());
152        let mod_name = if mod_sep.is_empty() {
153            mod_name.replace('-', "_")
154        } else {
155            mod_name.to_string()
156        };
157        root.push("src");
158        return (format!("{mod_name}{mod_sep}"), root);
159    }
160
161    let path = Path::new(file_path)
162        .parent()
163        .map_or_else(|| PathBuf::from("."), Path::to_path_buf);
164
165    let m = path
166        .parent()
167        .and_then(|p| p.file_name())
168        .map_or_else(|| Cow::Borrowed("crate"), |p| p.to_string_lossy());
169
170    (format!("{m}{mod_sep}"), path)
171}
172
173fn module_path_from_file(sep: &str, file_path: &str) -> String {
174    let normalized = file_path.replace('\\', "/");
175    let relative = normalized
176        .split_once("/src/")
177        .map_or(normalized.as_str(), |(_, tail)| tail);
178
179    if relative == "lib.rs" || relative == "main.rs" {
180        return relative.to_string();
181    }
182
183    let without_ext = relative.strip_suffix(".rs").unwrap_or(relative);
184    if without_ext.ends_with("/mod") {
185        let parent = without_ext.strip_suffix("/mod").unwrap_or(without_ext);
186        let parent = parent.trim_matches('/');
187        return parent.replace('/', sep);
188    }
189
190    let module = without_ext.trim_matches('/').replace('/', sep);
191    if module.is_empty() {
192        "crate".to_string()
193    } else {
194        module
195    }
196}
197
198fn module_path_from_file_with_root(
199    prefix: &str,
200    sep: &str,
201    file_path: &str,
202    module_name: &str,
203    module_root: &Path,
204    suffix: &str,
205) -> ByteString {
206    let normalized = file_path.replace('\\', "/");
207    let module_root_norm = module_root.to_string_lossy().replace('\\', "/");
208
209    let Some(relative) = normalized.strip_prefix(&(module_root_norm.clone() + "/")) else {
210        return format!(
211            "{prefix}{module_name}{sep}{}{suffix}",
212            module_path_from_file(sep, file_path)
213        )
214        .into();
215    };
216    if relative == "lib.rs" || relative == "main.rs" {
217        return ByteString::from(format!("{prefix}{module_name}{sep}{relative}"));
218    }
219
220    let without_ext = relative.strip_suffix(".rs").unwrap_or(relative);
221    if without_ext.ends_with("/mod") {
222        let parent = without_ext.strip_suffix("/mod").unwrap_or(without_ext);
223        let parent = parent.trim_matches('/');
224        return format!(
225            "{prefix}{module_name}{sep}{}{sep}mod{suffix}",
226            parent.replace('/', sep)
227        )
228        .into();
229    }
230
231    let module = without_ext.trim_matches('/').replace('/', sep);
232    if module.is_empty() {
233        ByteString::from(format!("{prefix}{module_name}{suffix}"))
234    } else {
235        format!("{prefix}{module_name}{sep}{module}{suffix}").into()
236    }
237}