serde_lsp/dap/
variables_arguments.rs

1use super::*;
2use crate::dap::VariableFilter;
3use serde::{
4    de::{Error, MapAccess, Visitor},
5    Deserializer,
6    __private::de::Content,
7};
8use std::{fmt::Formatter, num::NonZeroUsize};
9
10#[derive(Clone, Debug, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct VariablesArguments {
13    /// The variable for which to retrieve its children. The `variablesReference`
14    /// must have been obtained in the current suspended state. See 'Lifetime of
15    /// Object References' in the Overview section for details.
16    pub variables_reference: NonZeroUsize,
17    /// Filter to limit the child variables to either named or indexed. If omitted,
18    /// both types are fetched.
19    /// Values: 'indexed', 'named'
20    pub filter: Option<VariableFilter>,
21    /// The index of the first variable to return; if omitted children start at 0.
22    /// The attribute is only honored by a debug adapter if the corresponding
23    /// capability `supportsVariablePaging` is true.
24    pub start: usize,
25    /// The number of variables to return. If count is missing or 0, all variables
26    /// are returned.
27    /// The attribute is only honored by a debug adapter if the corresponding
28    /// capability `supportsVariablePaging` is true.
29    pub count: Option<NonZeroUsize>,
30    /// Specifies details on how to format the Variable values.
31    /// The attribute is only honored by a debug adapter if the corresponding
32    /// capability `supportsValueFormattingOptions` is true.
33    pub format: ValueFormat,
34}
35
36impl<'de> Deserialize<'de> for VariablesArguments {
37    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
38    where
39        D: Deserializer<'de>,
40    {
41        deserializer.deserialize_map(VariablesArgumentsVisitor::default())
42    }
43}
44
45#[derive(Default)]
46struct VariablesArgumentsVisitor {
47    variables_reference: Option<NonZeroUsize>,
48    filter: Option<VariableFilter>,
49    start: usize,
50    count: Option<NonZeroUsize>,
51}
52
53impl<'de> Visitor<'de> for VariablesArgumentsVisitor {
54    type Value = VariablesArguments;
55
56    fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
57        formatter.write_str("InspectVariableRequest")
58    }
59    fn visit_map<A>(mut self, mut map: A) -> Result<Self::Value, A::Error>
60    where
61        A: MapAccess<'de>,
62    {
63        while let Some(key) = map.next_key::<String>()? {
64            match key.as_str() {
65                "variablesReference" => self.variables_reference = Some(map.next_value()?),
66                "filter" => self.filter = Some(map.next_value()?),
67                "start" => self.start = map.next_value()?,
68                "count" => {
69                    let value = map.next_value::<usize>()?;
70                    self.count = NonZeroUsize::new(value)
71                }
72                _ => {
73                    let value = map.next_value::<Content>()?;
74                    println!("Unknown key {:?}, value {:#?}", key, value)
75                }
76            }
77        }
78        if self.variables_reference.is_none() {
79            return Err(Error::missing_field("variablesReference"));
80        }
81        Ok(VariablesArguments {
82            variables_reference: self.variables_reference.unwrap(),
83            filter: self.filter,
84            start: self.start,
85            count: self.count,
86            format: Default::default(),
87        })
88    }
89}