summary refs log blame commit diff stats
path: root/src/args.rs
blob: 178203d69e102932b8ad885a6fb67a32e33a1829 (plain) (tree)
1
2
3
4
5
6
7
8
9






                                                            
                    
                       
                          
                      

                    
                                                



                                          

                                                                                 




                       







                                                          

                          




                                                         


                                                      
                           



                                          



                              
                              
                                        
                                       

                                                                       



                                             

                                              
                            
              
                                           



                                                               
                            


                                           

                                                    

     
















                                                                       
                                                   

















                                                               
                                                    


                           









































                                                               
// 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::borrow::Cow;
use ::std::future::Future;
use ::std::io::Cursor;
use ::std::pin::Pin;

use crate::strcursor::{StringReader, ReadError};
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
///
/// - `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<S, E> ArgumentType<S, E> for BoolArgumentType
/// where for<'i> E: ReadError<'i, Cursor<&'i str>>
/// {
///     type Result = bool;
///     fn parse<'i>(
///         &self,
///         reader: &mut Cursor<&'i str>,
///     ) -> Result<bool, E> where E: 'i {
///         reader.read_bool()
///     }
/// }
/// ```
pub trait ArgumentType<S, E> {
    /// 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<Self::Result, E> where E: 'i;

    /// Creates suggestions for this argument.
    fn list_suggestions<'i>(
        &self,
        context: &CommandContext<'i, S, E>,
        builder: SuggestionsBuilder<'i>,
    ) -> Pin<Box<dyn Future<Output=Suggestions> + Send + 'i>> {
        let _ = context;
        let _ = builder;
        Suggestions::empty()
    }

    /// Returns examples for this argument.
    fn get_examples(&self) -> Cow<'static, [&str]> {
        Cow::Borrowed(&[])
    }
}

/// Wrapper around `ArgumentType`, but with `Any`.
pub(crate) trait ArgumentTypeAny<S, E> {
    /// Parses an argument of this type, returning the parsed argument.
    fn parse<'i>(
        &self,
        reader: &mut Cursor<&'i str>,
    ) -> Result<Box<dyn Any>, E> where E: 'i;

    /// Creates suggestions for this argument.
    fn list_suggestions<'i>(
        &self,
        context: &CommandContext<'i, S, E>,
        builder: SuggestionsBuilder<'i>,
    ) -> Pin<Box<dyn Future<Output=Suggestions> + Send + 'i>>;

    /// Returns examples for this argument.
    fn get_examples(&self) -> Cow<'static, [&str]>;
}

impl<T: ArgumentType<S, E>, S, E> ArgumentTypeAny<S, E> for T {
    fn parse<'i>(
        &self,
        reader: &mut Cursor<&'i str>,
    ) -> Result<Box<dyn Any>, 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<Box<dyn Future<Output=Suggestions> + Send + 'i>> {
        self.list_suggestions(context, builder)
    }

    fn get_examples(&self) -> Cow<'static, [&str]> {
        self.get_examples()
    }
}

/// A boolean argument.
// FIXME add examples/expand docs
// FIXME add tests
pub struct BoolArgumentType;

/// An `ArgumentType` for `bool`.
impl<S, E> ArgumentType<S, E> for BoolArgumentType
where for<'i> E: ReadError<'i, Cursor<&'i str>>
{
    /// A `BoolArgumentType` parses a `bool`.
    type Result = bool;

    /// Attempts to parse a `bool` from the `reader`.
    fn parse<'i>(
        &self,
        reader: &mut Cursor<&'i str>,
    ) -> Result<bool, E> where E: 'i {
        reader.read_bool()
    }

    /// Suggests completions for inputting a boolean argument.
    fn list_suggestions<'i>(
        &self,
        context: &CommandContext<'i, S, E>,
        mut builder: SuggestionsBuilder<'i>,
    ) -> Pin<Box<dyn Future<Output=Suggestions> + Send + 'i>> {
        let _ = context;
        if "true".starts_with(builder.get_remaining()) {
            builder.suggest("true".into());
        }
        if "false".starts_with(builder.get_remaining()) {
            builder.suggest("false".into());
        }
        builder.drain_build_future()
    }

    /// Returns examples
    fn get_examples(&self) -> Cow<'static, [&str]> {
        Cow::Borrowed(&["true", "false"])
    }
}