summary refs log tree commit diff stats
path: root/src/tree.rs
blob: 71243bebbc13ebbb7976abd2bd27eb131ce84143 (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
// 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.

//! Command syntax tree.

use ::std::borrow::Cow;

use crate::Command;
use crate::args::{ArgumentType, ArgumentTypeAny};
use crate::strcursor::StringReader;
// FIXME
use crate::args::CommandContext;

/// The kind of node.
enum NodeKind<S, E> {
    /// Root node.
    Root,
    /// Literal node.
    Literal {
        /// The literal of the node.
        literal: Cow<'static, str>,
    },
    /// Argument node.
    Argument {
        /// The label of the node.
        label: Cow<'static, str>,
        /// The argument type.
        arg: Box<dyn ArgumentTypeAny<S, E>>,
    },
}

/// A node in the syntax tree.
pub struct CommandNode<T, S, E> {
    /// The command to run for this node.
    command: Option<Command<T, S, E>>,
    /// Nodes.
    nodes: (),
    /// Literal nodes.
    literals: (),
    /// Argument nodes.
    arguments: (),
    /// The kind of node.
    kind: NodeKind<S, E>,
}

impl<T, S, E> CommandNode<T, S, E> {
    /// Creates a new root node.
    pub fn root() -> Self {
        CommandNode {
            command: None,
            nodes: (),
            literals: (),
            arguments: (),
            kind: NodeKind::Root,
        }
    }

    /// Creates a new literal node.
    pub fn literal(
        word: Cow<'static, str>,
        command: Option<Command<T, S, E>>,
    ) -> Self {
        CommandNode {
            command: command,
            nodes: (),
            literals: (),
            arguments: (),
            kind: NodeKind::Literal {
                literal: word,
            },
        }
    }

    /// Creates a new argument node.
    pub fn argument<A: ArgumentType<S, E> + 'static + Send + Sync>(
        name: Cow<'static, str>,
        command: Option<Command<T, S, E>>,
        argument: A,
    ) -> Self {
        CommandNode {
            command: command,
            nodes: (),
            literals: (),
            arguments: (),
            kind: NodeKind::Argument {
                label: name,
                arg: Box::new(argument),
            },
        }
    }

    /// Returns the name of this node.
    ///
    /// For literal nodes, this is the literal itself. For argument nodes, this
    /// is the name of the argument.
    pub fn get_name(&self) -> &str {
        match self.kind {
            NodeKind::Root => "",
            NodeKind::Literal { ref literal, .. } => literal,
            NodeKind::Argument { ref label, .. } => label,
        }
    }
}