// Copyright (c) 2021 Soni L. // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. // Documentation and comments licensed under CC BY-SA 4.0. //! Argument processing. use ::std::any::Any; use ::std::future::Future; use ::std::io::Cursor; use ::std::pin::Pin; use crate::strcursor::StringReader; use crate::suggestion::Suggestions; use crate::suggestion::SuggestionsBuilder; // FIXME delete when implemented /// The parsing context of a command. pub struct CommandContext<'i, S, E>(::std::marker::PhantomData<(&'i str, S, E)>); /// An argument parser. /// /// # Type params /// /// - `'i`: Lifetime of the input. /// - `R`: The reader accepted by this argument type. /// - `S`: The source type accepted by this argument type. /// - `E`: The error type accepted by this argument type. /// /// # Examples /// /// A very basic `bool` argument type: /// /// ``` /// use ::std::io::Cursor; /// /// use ::iosonism::args::ArgumentType; /// use ::iosonism::strcursor::{ReadError, StringReader}; /// /// struct BoolArgumentType; /// /// impl ArgumentType for BoolArgumentType /// where for<'i> E: ReadError<'i, Cursor<&'i str>> /// { /// type Result = bool; /// fn parse<'i>( /// &self, /// reader: &mut Cursor<&'i str>, /// ) -> Result where E: 'i { /// reader.read_bool() /// } /// } /// ``` pub trait ArgumentType { /// The parsed type of the argument. type Result: Sized + 'static + Any; /// Parses an argument of this type, returning the parsed argument. fn parse<'i>( &self, reader: &mut Cursor<&'i str>, ) -> Result where E: 'i; /// Creates suggestions for this argument. fn list_suggestions<'i>( &self, context: &CommandContext<'i, S, E>, builder: SuggestionsBuilder<'i>, ) -> Pin + Send + 'i>> { let _ = context; let _ = builder; todo!() } /// Returns examples for this argument. fn get_examples(&self) -> Vec<&str> { Vec::new() } } /// Wrapper around `ArgumentType`, but with `Any`. pub(crate) trait ArgumentTypeAny { /// Parses an argument of this type, returning the parsed argument. fn parse<'i>( &self, reader: &mut Cursor<&'i str>, ) -> Result, E> where E: 'i; /// Creates suggestions for this argument. fn list_suggestions<'i>( &self, context: &CommandContext<'i, S, E>, builder: SuggestionsBuilder<'i>, ) -> Pin + Send + 'i>>; /// Returns examples for this argument. fn get_examples(&self) -> Vec<&str>; } impl, S, E> ArgumentTypeAny for T { fn parse<'i>( &self, reader: &mut Cursor<&'i str>, ) -> Result, E> where E: 'i { self.parse(reader).map(|x| Box::new(x) as _) } fn list_suggestions<'i>( &self, context: &CommandContext<'i, S, E>, builder: SuggestionsBuilder<'i>, ) -> Pin + Send + 'i>> { self.list_suggestions(context, builder) } fn get_examples(&self) -> Vec<&str> { self.get_examples() } }