summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibManual/SectionNode.h
blob: 414e03b17cdac502b027866458873539c26a6b2f (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
/*
 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/String.h>
#include <LibManual/Node.h>

namespace Manual {

class SectionNode : public Node {
public:
    virtual ~SectionNode() override = default;

    SectionNode(StringView section, StringView name)
        : m_section(MUST(String::from_utf8(section)))
        , m_name(MUST(String::from_utf8(name)))
    {
    }

    virtual NonnullRefPtrVector<Node>& children() const override
    {
        MUST(reify_if_needed());
        return m_children;
    }

    virtual Node const* parent() const override { return nullptr; }
    virtual ErrorOr<String> name() const override;
    String const& section_name() const { return m_section; }
    ErrorOr<String> path() const;

    virtual bool is_open() const override { return m_open; }
    void set_open(bool open);

    static ErrorOr<NonnullRefPtr<SectionNode>> try_create_from_number(StringView section_number);

protected:
    String m_section;
    String m_name;

private:
    ErrorOr<void> reify_if_needed() const;

    mutable NonnullRefPtrVector<Node> m_children;
    mutable bool m_reified { false };
    bool m_open { false };
};

constexpr size_t number_of_sections = 8;

extern Array<NonnullRefPtr<SectionNode>, number_of_sections> const sections;

constexpr Array<StringView, number_of_sections> const section_numbers = {
    "1"sv,
    "2"sv,
    "3"sv,
    "4"sv,
    "5"sv,
    "6"sv,
    "7"sv,
    "8"sv,
};

}