|
1 | 1 | use std::collections::HashMap; |
2 | | -use std::env; |
| 2 | +use std::{env, result::Result}; |
3 | 3 | use std::{borrow::Borrow, cell::RefCell, rc::Rc}; |
4 | 4 |
|
5 | 5 | use bon::{bon, builder, Builder}; |
6 | 6 | use chrono::Duration; |
7 | 7 |
|
| 8 | +use konst::eq_str; |
8 | 9 | use linked_hash_set::LinkedHashSet; |
9 | 10 |
|
10 | 11 | #[cfg(feature = "pcre2")] |
@@ -496,6 +497,93 @@ fn check_wildcarded(_wildcarded: &EnvKey, _s: &String) -> bool { |
496 | 497 | true |
497 | 498 | } |
498 | 499 |
|
| 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 | + |
499 | 587 | #[derive(Debug, Clone, Serialize, Deserialize)] |
500 | 588 | pub struct OptStack { |
501 | 589 | pub(crate) stack: [Option<Rc<RefCell<Opt>>>; 5], |
|
0 commit comments