summary refs log tree commit diff stats
path: root/src/args.rs
blob: 2b8eecfd6ebc37468469c54579464124c9d159c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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<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;
        todo!()
    }

    /// Returns examples for this argument.
    fn get_examples(&self) -> Vec<&str> {
        Vec::new()
    }
}

/// 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) -> Vec<&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) -> Vec<&str> {
        self.get_examples()
    }
}