Skip to content

Commit f523e52

Browse files
committed
refactor: MORE optimization by using constants instead of dyn alloc...
1 parent 8e9f016 commit f523e52

File tree

7 files changed

+419
-155
lines changed

7 files changed

+419
-155
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ RAR_CFG_PATH = "/etc/security/rootasrole.json"
77
RAR_CFG_DATA_PATH = "/etc/security/rootasrole.json"
88
RAR_BIN_PATH = "/usr/bin"
99
RAR_CFG_IMMUTABLE = "true"
10-
RAR_TIMEOUT_TYPE = "PPID"
10+
RAR_TIMEOUT_TYPE = "ppid"
1111
RAR_TIMEOUT_DURATION = "00:05:00"
1212
RAR_TIMEOUT_MAX_USAGE = ""
1313
RAR_PATH_DEFAULT = "delete"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const_format = "0.2"
9191
hex = "0.4"
9292
bon = "3.5.1"
9393
serde_json_borrow = "0.7.1"
94+
konst = "0.3.16"
9495

9596
[dev-dependencies]
9697
log = "0.4"

rar-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ syslog = "7.0"
2828
env_logger = "0.11"
2929
bon = { version = "3.3.2", features = ["experimental-overwritable"] }
3030
cbor4ii = { version = "1.0.0", features = ["serde", "serde1", "use_std"] }
31+
konst = "0.3.16"
3132

3233
[dev-dependencies]
3334
log = "0.4"

rar-common/src/database/options.rs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::collections::HashMap;
2-
use std::env;
2+
use std::{env, result::Result};
33
use std::{borrow::Borrow, cell::RefCell, rc::Rc};
44

55
use bon::{bon, builder, Builder};
66
use chrono::Duration;
77

8+
use konst::eq_str;
89
use linked_hash_set::LinkedHashSet;
910

1011
#[cfg(feature = "pcre2")]
@@ -496,6 +497,93 @@ fn check_wildcarded(_wildcarded: &EnvKey, _s: &String) -> bool {
496497
true
497498
}
498499

500+
#[derive(Debug, PartialEq)]
501+
pub struct ConstParseError(pub &'static str);
502+
use std::fmt::{self, Display};
503+
504+
impl Display for ConstParseError {
505+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
506+
f.write_fmt(format_args!(
507+
"Failed to parse the const {} defined in .cargo/config.toml",
508+
self.0
509+
))
510+
}
511+
}
512+
513+
impl ConstParseError {
514+
const fn panic(&self) -> ! {
515+
panic!("failed to parse a const")
516+
}
517+
}
518+
519+
520+
impl PathBehavior {
521+
pub const fn try_parse(input:&str) -> std::result::Result<PathBehavior, ConstParseError> {
522+
match input {
523+
_ if eq_str(input, "delete") => Ok(PathBehavior::Delete),
524+
_ if eq_str(input, "keep_safe") => Ok(PathBehavior::KeepSafe),
525+
_ if eq_str(input, "keep_unsafe") => Ok(PathBehavior::KeepUnsafe),
526+
_ if eq_str(input, "inherit") => Ok(PathBehavior::Inherit),
527+
_ => ConstParseError("PathBehavior").panic(),
528+
}
529+
}
530+
}
531+
532+
impl EnvBehavior {
533+
pub const fn try_parse(input:&str) -> std::result::Result<EnvBehavior, ConstParseError> {
534+
match input {
535+
_ if eq_str(input, "delete") => Ok(EnvBehavior::Delete),
536+
_ if eq_str(input, "keep") => Ok(EnvBehavior::Keep),
537+
_ if eq_str(input, "inherit") => Ok(EnvBehavior::Inherit),
538+
_ => ConstParseError("EnvBehavior").panic(),
539+
}
540+
}
541+
}
542+
543+
impl SPrivileged {
544+
pub const fn try_parse(input:&str) -> std::result::Result<SPrivileged, ConstParseError> {
545+
match input {
546+
_ if eq_str(input, "user") => Ok(SPrivileged::User),
547+
_ if eq_str(input, "inherit") => Ok(SPrivileged::Inherit),
548+
_ if eq_str(input, "privileged") => Ok(SPrivileged::Privileged),
549+
_ => ConstParseError("SPrivileged").panic(),
550+
}
551+
}
552+
}
553+
554+
impl TimestampType {
555+
pub const fn try_parse(input:&str) -> std::result::Result<TimestampType, ConstParseError> {
556+
match input {
557+
_ if eq_str(input, "ppid") => Ok(TimestampType::PPID),
558+
_ if eq_str(input, "tty") => Ok(TimestampType::TTY),
559+
_ if eq_str(input, "uid") => Ok(TimestampType::UID),
560+
_ => ConstParseError("TimestampType").panic(),
561+
}
562+
}
563+
}
564+
565+
impl SBounding {
566+
pub const fn try_parse(input:&str) -> std::result::Result<SBounding, ConstParseError> {
567+
match input {
568+
_ if eq_str(input, "strict") => Ok(SBounding::Strict),
569+
_ if eq_str(input, "inherit") => Ok(SBounding::Inherit),
570+
_ if eq_str(input, "ignore") => Ok(SBounding::Ignore),
571+
_ => ConstParseError("SBounding").panic(),
572+
}
573+
}
574+
}
575+
576+
impl SAuthentication {
577+
pub const fn try_parse(input:&str) -> std::result::Result<SAuthentication, ConstParseError> {
578+
match input {
579+
_ if eq_str(input, "perform") => Ok(SAuthentication::Perform),
580+
_ if eq_str(input, "inherit") => Ok(SAuthentication::Inherit),
581+
_ if eq_str(input, "skip") => Ok(SAuthentication::Skip),
582+
_ => ConstParseError("SAuthentication").panic(),
583+
}
584+
}
585+
}
586+
499587
#[derive(Debug, Clone, Serialize, Deserialize)]
500588
pub struct OptStack {
501589
pub(crate) stack: [Option<Rc<RefCell<Opt>>>; 5],

src/sr/finder/cmd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ fn match_command_line(
119119
result
120120
}
121121

122+
#[inline(always)]
122123
pub fn evaluate_command_match(
123124
env_path: &[&str],
124125
cmd_path: &PathBuf,

src/sr/finder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl BestExecSettings {
124124
return Err("No matching role found".into());
125125
}
126126
result.env = opt_stack
127-
.calc_temp_env(&opt_stack.calc_override_behavior(), &cli.opt_filter)
127+
.calc_temp_env(opt_stack.calc_override_behavior(), &cli.opt_filter)
128128
.calc_final_env(env_vars, env_path, cred)?;
129129
result.auth = opt_stack.calc_authentication();
130130
result.bounding = opt_stack.calc_bounding();

0 commit comments

Comments
 (0)