1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-12 17:09:00 -05:00

refactor(ext/node): remove unwraps (#16559)

This commit is contained in:
Bartek Iwańczuk 2022-11-08 03:16:24 +01:00 committed by GitHub
parent 3019c45f87
commit d0212198b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::include_js_files;
use deno_core::normalize_path;
@ -431,17 +432,31 @@ fn op_require_path_resolve(state: &mut OpState, parts: Vec<String>) -> String {
}
#[op]
fn op_require_path_dirname(state: &mut OpState, request: String) -> String {
fn op_require_path_dirname(
state: &mut OpState,
request: String,
) -> Result<String, AnyError> {
check_unstable(state);
let p = PathBuf::from(request);
p.parent().unwrap().to_string_lossy().to_string()
if let Some(parent) = p.parent() {
Ok(parent.to_string_lossy().to_string())
} else {
Err(generic_error("Path doesn't have a parent"))
}
}
#[op]
fn op_require_path_basename(state: &mut OpState, request: String) -> String {
fn op_require_path_basename(
state: &mut OpState,
request: String,
) -> Result<String, AnyError> {
check_unstable(state);
let p = PathBuf::from(request);
p.file_name().unwrap().to_string_lossy().to_string()
if let Some(path) = p.file_name() {
Ok(path.to_string_lossy().to_string())
} else {
Err(generic_error("Path doesn't have a file name"))
}
}
#[op]
@ -551,10 +566,14 @@ pub fn op_require_as_file_path(
file_or_url: String,
) -> String {
check_unstable(state);
match Url::parse(&file_or_url) {
Ok(url) => url.to_file_path().unwrap().to_string_lossy().to_string(),
Err(_) => file_or_url,
if let Ok(url) = Url::parse(&file_or_url) {
if let Ok(p) = url.to_file_path() {
return p.to_string_lossy().to_string();
}
}
file_or_url
}
#[op]