1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

perf(runtime): optimize PermissionState::check (#9993)

This commit is contained in:
Aaron O'Mullan 2021-04-12 13:24:41 +02:00 committed by GitHub
parent 875ac73f1e
commit 5c2a8cdbdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,6 +34,37 @@ pub enum PermissionState {
}
impl PermissionState {
#[inline(always)]
fn log_perm_access(name: &str, info: Option<&str>) {
debug!(
"{}",
colors::bold(&format!(
"{} Granted {}",
PERMISSION_EMOJI,
Self::fmt_access(name, info)
))
);
}
fn fmt_access(name: &str, info: Option<&str>) -> String {
format!(
"{} access{}",
name,
info.map_or(String::new(), |info| { format!(" to {}", info) }),
)
}
fn error(name: &str, info: Option<&str>) -> AnyError {
custom_error(
"PermissionDenied",
format!(
"Requires {}, run again with the --allow-{} flag",
Self::fmt_access(name, info),
name
),
)
}
/// Check the permission state. bool is whether a prompt was issued.
fn check(
self,
@ -41,28 +72,22 @@ impl PermissionState {
info: Option<&str>,
prompt: bool,
) -> (Result<(), AnyError>, bool) {
let access = format!(
"{} access{}",
name,
info.map_or(String::new(), |info| { format!(" to {}", info) }),
);
let result = if self == PermissionState::Granted
|| (prompt
&& self == PermissionState::Prompt
&& permission_prompt(&access))
{
log_perm_access(&access);
Ok(())
} else {
Err(custom_error(
"PermissionDenied",
format!(
"Requires {}, run again with the --allow-{} flag",
access, name
),
))
};
(result, prompt && self == PermissionState::Prompt)
match self {
PermissionState::Granted => {
Self::log_perm_access(name, info);
(Ok(()), false)
}
PermissionState::Prompt if prompt => {
let msg = Self::fmt_access(name, info);
if permission_prompt(&msg) {
Self::log_perm_access(name, info);
(Ok(()), true)
} else {
(Err(Self::error(name, info)), true)
}
}
_ => (Err(Self::error(name, info)), false),
}
}
}
@ -820,13 +845,6 @@ impl deno_websocket::WebSocketPermissions for Permissions {
}
}
fn log_perm_access(message: &str) {
debug!(
"{}",
colors::bold(&format!("{} Granted {}", PERMISSION_EMOJI, message))
);
}
fn boolean_permission_from_flag_bool(
flag: bool,
name: &'static str,