aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/console/input/input_option.rs
blob: 7fa3d699cd3f501e0a6bcbb63436486c07e4329d (plain)
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
//! ref: composer/src/Composer/Console/Input/InputOption.php

use crate::console::input::SuggestedValues;
use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput;
use shirabe_external_packages::symfony::console::completion::completion_suggestions::CompletionSuggestions;
use shirabe_external_packages::symfony::console::input::InputOption as BaseInputOption;
use shirabe_php_shim::PhpMixed;

#[derive(Debug)]
pub struct InputOption {
    inner: BaseInputOption,
    suggested_values: SuggestedValues,
}

impl InputOption {
    pub const VALUE_NONE: i64 = 1;
    pub const VALUE_REQUIRED: i64 = 2;
    pub const VALUE_OPTIONAL: i64 = 4;
    pub const VALUE_IS_ARRAY: i64 = 8;
    pub const VALUE_NEGATABLE: i64 = 16;

    pub fn new(
        name: &str,
        shortcut: Option<PhpMixed>,
        mode: Option<i64>,
        description: &str,
        default: Option<PhpMixed>,
    ) -> anyhow::Result<Self> {
        Self::new6(
            name,
            shortcut,
            mode,
            description,
            default,
            SuggestedValues::List(Vec::new()),
        )
    }

    /// PHP's constructor with the sixth parameter, `$suggestedValues`.
    pub fn new6(
        name: &str,
        shortcut: Option<PhpMixed>,
        mode: Option<i64>,
        description: &str,
        default: Option<PhpMixed>,
        suggested_values: SuggestedValues,
    ) -> anyhow::Result<Self> {
        let shortcut = shortcut.unwrap_or(PhpMixed::Null);
        let default_mixed = default.unwrap_or(PhpMixed::Null);
        let inner =
            BaseInputOption::new(name, shortcut, mode, description.to_string(), default_mixed)?;
        // PHP throws LogicException here; suggested values on a valueless option cannot happen
        // at runtime unless a configure() is wrong, so this is a programming error.
        assert!(
            suggested_values.is_empty() || inner.accept_value(),
            "Cannot set suggested values if the option does not accept a value."
        );
        Ok(Self {
            inner,
            suggested_values,
        })
    }

    /// Adds suggestions to `suggestions` for the current completion input.
    ///
    /// PHP closures are bound to the command; `this` is the command dispatching the
    /// completion (see [`SuggestedValues`]).
    pub fn complete(
        &self,
        this: &dyn crate::command::BaseCommand,
        input: &CompletionInput,
        suggestions: &mut CompletionSuggestions,
    ) -> anyhow::Result<()> {
        self.suggested_values.complete(this, input, suggestions)
    }

    pub(crate) fn get_name(&self) -> String {
        self.inner.get_name().to_string()
    }

    /// Unwraps to the underlying Symfony `InputOption` (used when forwarding a Composer-typed
    /// definition to the Symfony command state).
    pub(crate) fn to_base(&self) -> BaseInputOption {
        self.inner.clone()
    }
}