-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
55 lines (46 loc) · 1.34 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::path::{Path, PathBuf};
use napi_derive::napi;
use oxc_resolver::{ResolveOptions, Resolver};
#[napi(object)]
pub struct ResolveResult {
pub path: Option<String>,
pub error: Option<String>,
}
#[napi]
pub struct ResolverFactory {
resolver: Resolver,
}
impl Default for ResolverFactory {
fn default() -> Self {
Self::new()
}
}
#[napi]
impl ResolverFactory {
#[napi(constructor)]
pub fn new() -> Self {
Self { resolver: Resolver::new(ResolveOptions::default()) }
}
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn sync(&self, path: String, request: String) -> ResolveResult {
let path = PathBuf::from(path);
resolve(&self.resolver, &path, &request)
}
}
fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResult {
match resolver.resolve(path, request) {
Ok(resolution) => ResolveResult {
path: Some(resolution.full_path().to_string_lossy().to_string()),
error: None,
},
Err(err) => ResolveResult { path: None, error: Some(err.to_string()) },
}
}
#[allow(clippy::needless_pass_by_value)]
#[napi]
pub fn sync(path: String, request: String) -> ResolveResult {
let path = PathBuf::from(path);
let resolver = Resolver::new(ResolveOptions::default());
resolve(&resolver, &path, &request)
}