Skip to main content

nifty_check/
methods.rs

1//! The built-in methods on lists and strings. The checker dispatches on [`Method::kind`],
2//! and tools such as the language server read the same table.
3
4/// A built-in method such as `items.push(item)`.
5#[derive(Debug)]
6pub struct Method {
7    pub name: &'static str,
8    pub kind: MethodKind,
9    /// The names of its arguments, which are always given by position.
10    pub params: &'static [&'static str],
11    pub summary: &'static str,
12}
13
14/// Which method a table entry is, for the checker to dispatch on.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum MethodKind {
17    Len,
18    IsEmpty,
19    Push,
20    Remove,
21}
22
23pub const METHODS: &[Method] = &[
24    Method {
25        name: "len",
26        kind: MethodKind::Len,
27        params: &[],
28        summary: "How many items a list has, or characters a string has.",
29    },
30    Method {
31        name: "is_empty",
32        kind: MethodKind::IsEmpty,
33        params: &[],
34        summary: "Whether a list or string has nothing in it.",
35    },
36    Method {
37        name: "push",
38        kind: MethodKind::Push,
39        params: &["item"],
40        summary: "Adds an item to the end of a list.",
41    },
42    Method {
43        name: "remove",
44        kind: MethodKind::Remove,
45        params: &["index"],
46        summary: "Removes the item at a position in a list.",
47    },
48];
49
50/// The method called `name`, if it is built in.
51#[must_use]
52pub fn method(name: &str) -> Option<&'static Method> {
53    METHODS.iter().find(|method| method.name == name)
54}