From b54b74c158e86bdd8e5232a7bbf679a9db53ed33 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 26 Dec 2020 17:31:50 +0000 Subject: [PATCH] subst more Signed-off-by: Ian Jackson --- wdriver.rs | 79 ++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/wdriver.rs b/wdriver.rs index 8525dc7e..983d4dc9 100644 --- a/wdriver.rs +++ b/wdriver.rs @@ -50,7 +50,7 @@ pub enum StaticUser { use StaticUser::*; -const FIXED_TOKENS : (&str, &str) = [ +const FIXED_TOKENS : &[(StaticUser, &str)] = &[ (Alice, "kmqAKPwK4TfReFjMor8MJhdRPBcwIBpe"), (Bob , "ccg9kzoTh758QrVE1xMY7BQWB36dNJTx"), ]; @@ -123,32 +123,36 @@ struct RawSubst(HashMap); struct ExtendedSubst<'b, B: Subst, X: Subst>(&'b B, X); -impl, - U: AsRef, - L: Iterator> - From<&L> for RawSubst +impl, + U: ToOwned, + L: IntoIterator> + From for RawSubst { - fn from(l: &L) -> RawSubst { - let map = l.map(|(k,v)| (k.to_owned(), v.to_owned())).collect(); + fn from(l: L) -> RawSubst { + let map = l.into_iter() + .map(|(k,v)| (k.to_owned(), v.to_owned())).collect(); RawSubst(map) } } trait Subst : Sized { fn get(&self, kw: &str) -> Option; - fn also>(&self, xl: &L) -> ExtendedSubst { - ExtendedSubst(self, Box::new(xl.into())); + fn also>(&self, xl: L) -> ExtendedSubst { + ExtendedSubst(self, xl.into()) } - fn subst(&self, s: &dyn AsRef) -> Result { + + #[throws(AE)] + fn subst(&self, s: &dyn AsRef) -> String { let s = s.as_ref(); let re = Regex::new(r"@(\w+)@").expect("bad re!"); let mut errs = vec![]; let out = re.replace_all(s, |caps: ®ex::Captures| { - let n = caps.get(1).expect("$1 missing!").as_str(); - if n == "" { return &"" } - self.lookup(n).unwrap_or_else(||{ - errs.push(n.to_owned()); - &"" + let kw = caps.get(1).expect("$1 missing!").as_str(); + if kw == "" { return "".to_owned() } + let v = self.get(kw); + v.unwrap_or_else(||{ + errs.push(kw.to_owned()); + "".to_owned() }) }); if ! errs.is_empty() { @@ -157,24 +161,31 @@ trait Subst : Sized { } out.into() } + + #[throws(AE)] + fn ss(&self, s: &str) -> Vec { + self.subst(&s)? + .trim() + .split(' ') + .filter(|s| !s.is_empty()) + .map(str::to_string) + .collect() + } } impl Subst for RawSubst { - #[throws(AE)] - fn get(&self, kw: &str) -> String { - self.0.get(kw).to_owned() + fn get(&self, kw: &str) -> Option { + self.0.get(kw).map(String::clone) } } impl<'b, B:Subst, X:Subst> Subst for ExtendedSubst<'b, B, X> { - #[throws(AE)] - fn get(&self, kw: &str) -> Option<&str> { + fn get(&self, kw: &str) -> Option { self.1.get(kw).or_else(|| self.0.get(kw)) } } impl Subst for DirSubst { - #[throws(AE)] fn get(&self, kw: &str) -> Option { Some(match kw { "src" => self.src.clone(), @@ -186,26 +197,6 @@ impl Subst for DirSubst { } } -impl DirSubst { - #[throws(AE)] - fn subst>(&self, s:S) -> String { - - - - - } - - #[throws(AE)] - fn ss(&self, s: &str) -> Vec { - self.subst(s)? - .trim() - .split(' ') - .filter(|s| !s.is_empty()) - .map(str::to_string) - .collect() - } -} - mod cleanup_notify { use anyhow::Context; use fehler::{throw, throws}; @@ -482,7 +473,7 @@ fn prepare_xserver(cln: &cleanup_notify::Handle, ds: &DirSubst) { #[throws(AE)] fn prepare_gameserver(cln: &cleanup_notify::Handle, ds: &DirSubst) { - let config = ds.subst(r##" + let config = ds.subst(&r##" base_dir = "@build@" public_url = "@url" @@ -510,7 +501,7 @@ _ = "error" # rocket fs::write(CONFIG, &config) .context(CONFIG).context("create server config")?; - let server_exe = ds.subst("@target@/debug/daemon-otter")?; + let server_exe = ds.subst(&"@target@/debug/daemon-otter")?; let mut cmd = Command::new(&server_exe); cmd .arg("--report-startup") @@ -531,7 +522,7 @@ impl DirSubst { #[throws(AE)] fn otter>(&self, args: &[S]) { let ds = self; - let exe = ds.subst("@target@/debug/otter")?; + let exe = ds.subst(&"@target@/debug/otter")?; (||{ let mut cmd = Command::new(&exe); cmd -- 2.30.2