From: Ian Jackson Date: Fri, 7 Aug 2020 21:46:27 +0000 (+0100) Subject: wip lifetime mess ? X-Git-Tag: otter-0.2.0~1172 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=ff19701bf3172b861e80cb1d94952db595d69191;p=otter.git wip lifetime mess ? --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 5c7cc17c..537300ad 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -1,5 +1,7 @@ // +#![allow(unused_imports)] + use game::imports::*; use argparse::{self,ArgumentParser,action::{TypedAction,ParseResult}}; use argparse::action::{Action,IFlagAction,IArgAction}; @@ -47,35 +49,37 @@ impl<'a,T> Deref for CellRef<'a,T> { } */ -struct CallAction(F, PhantomData); - -struct CallFlag; +#[derive(Clone)] +struct MapStore Result > ( + F +); -impl IFlagAction for CallAction>> -where F : FnMut() -> ParseResult { - fn parse_flag(&self) -> ParseResult { - self.0.borrow_mut()() - } +struct BoundMapStore<'r, T, F: FnMut(&str) -> Result> { + f: Rc>, + r: Rc>, } -impl ParseResult> TypedAction for CallFlag { - fn bind<'x>(&self, f: Rc>) -> Action<'x> { - Action::Flag(Box::new(CallAction(f.clone(), PhantomData::))) +impl<'f,T,F> TypedAction for MapStore +where F : 'static + Clone + FnMut(&str) -> Result { + fn bind<'x>(&self, r: Rc>) -> Action<'x> + { + Action::Single(Box::new(BoundMapStore { + f: Rc::new(RefCell::new(self.0.clone())), + r + })) } } -struct CallArg; - -impl IArgAction for CallAction>> -where F : FnMut(&str) -> ParseResult { +impl<'x, T, F: FnMut(&str) -> Result> + IArgAction for BoundMapStore<'x, T, F> +{ fn parse_arg(&self, arg: &str) -> ParseResult { - self.0.borrow_mut()(arg) - } -} - -impl ParseResult> TypedAction for CallArg { - fn bind<'x>(&self, f: Rc>) -> Action<'x> { - Action::Single(Box::new(CallAction(f.clone(), PhantomData::))) + let v = match self.f.borrow_mut()(arg) { + Ok(r) => r, + Err(e) => return ParseResult::Error(e), + }; + *self.r.borrow_mut(arg) = v; + ParseResult::Parsed } } @@ -95,15 +99,15 @@ enum Subcommand { fn main() { let mut mainopts : MainOpts = Default::default(); { + use argparse::*; let mut ap = ArgumentParser::new(); - let scope = Cell::from_mut(&mut mainopts.scope); - - Call(||{ scope.set(Some(ManagementScope::Server)); Parsed }) - . - let mut set_scope_server = - ap.refer(&mut set_scope_server) - .add_option(&["--scope-server"], CallFlag, - "use Server scope for game names"); + let scope = ap.refer(&mut mainopts.scope); + scope.add_option(&["--scope-server"], + Store(Some(ManagementScope::Server)), + "use Server scope"); + scope.add_option(&["--scope-unix-user"], + MapStore(|user| ManagementScope::Unix { user }), + "use specified unix user scope"); let r = ap.parse_args(); mem::drop(ap); r