summary refs log tree commit diff stats
path: root/src/args.rs
blob: 178203d69e102932b8ad885a6fb67a32e33a1829 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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"])
    }
}