Skip to main content

jsonette/
types.rs

1/*
2 * Copyright (c) 2026 DevEtte.
3 *
4 * This project is dual-licensed under both the MIT License and the
5 * Apache License, Version 2.0 (the "License"). You may not use this
6 * file except in compliance with one of these licenses.
7 *
8 * You may obtain a copy of the Licenses at:
9 * - MIT: https://opensource.org
10 * - Apache 2.0: http://apache.org
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19//! Public API types definitions
20
21/// A byte-offset range in the source text.
22#[derive(Debug, Clone, PartialEq, Default)]
23pub struct Span {
24    /// The starting byte offset (inclusive).
25    pub start: usize,
26    /// The ending byte offset (exclusive).
27    pub end: usize,
28}
29
30/// A single diagnostic (error or warning) from the engine.
31#[derive(Debug, Clone, PartialEq)]
32pub struct Diagnostic {
33    /// The span of the input text where this diagnostic applies.
34    pub span: Span,
35    /// The diagnostic message explaining the error or warning.
36    pub message: String,
37}
38
39/// The line ending style to use when formatting text.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
41pub enum LineEnding {
42    /// Unix line ending (Line Feed: `\n`).
43    LF,
44    /// Windows line ending (Carriage Return + Line Feed: `\r\n`).
45    CRLF,
46}
47
48/// The folding (wrapping) style to use for JSON arrays and objects.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
50pub enum FoldingStyle {
51    /// Always expand elements and key-value pairs onto new lines.
52    Expanded,
53    /// Keep elements/keys inline on a single line if they are empty or small.
54    Compact,
55}
56
57/// Options for the formatter.
58#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
59pub struct FormatOptions {
60    /// Whether to use tab characters (`\t`) for indentation
61    pub use_tabs: bool,
62    /// The number of spaces to use for indentation.
63    pub indent: u8,
64    /// The line ending character sequence to use.
65    pub line_ending: LineEnding,
66    /// The wrapping/folding strategy for objects and arrays.
67    pub folding_style: FoldingStyle,
68    /// Whether to sort objects keys alphabetically.
69    pub sort_keys: bool,
70    /// Whether to insert a space after colons (e.g. `{"key": "value"}` vs `{"key":"value"}`).
71    pub space_after_colon: bool,
72    /// Whether to insert a space after commas (e.g. `[1, 2]` vs `[1,2]`).
73    pub space_after_comma: bool,
74}
75
76impl Default for FormatOptions {
77    fn default() -> Self {
78        Self {
79            use_tabs: false,
80            indent: 2,
81            line_ending: LineEnding::LF,
82            folding_style: FoldingStyle::Expanded,
83            sort_keys: false,
84            space_after_colon: true,
85            space_after_comma: true,
86        }
87    }
88}
89
90/// Options for the parser.
91#[derive(Debug, Default, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
92pub struct ParserOptions {
93    /// Whether to allow single-line (`//`) and multi-line (`/* ... */`) comments.
94    pub allow_comments: bool,
95    /// Whether to tolerate trailing commas in arrays and objects.
96    pub allow_trailing_commas: bool,
97}
98
99/// Severity level for linting and diagnostics.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
101pub enum Severity {
102    /// Report as an error (prevents compilation/execution).
103    Error,
104    /// Report as a warning.
105    Warning,
106    /// Ignore the issue completely.
107    Ignore,
108}
109
110/// Options for diagnostics and linting.
111#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
112pub struct LintOptions {
113    /// Severity level for duplicate keys in objects.
114    pub duplicate_keys_severity: Severity,
115}
116
117impl Default for LintOptions {
118    fn default() -> Self {
119        Self {
120            duplicate_keys_severity: Severity::Warning,
121        }
122    }
123}
124
125/// The top-level settings structure for the application.
126#[derive(Debug, Default, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
127pub struct AppSettings {
128    /// Formatter preferences.
129    pub format: FormatOptions,
130    /// Parser preferences.
131    pub parser: ParserOptions,
132    /// Linting and diagnostics preferences.
133    pub lint: LintOptions,
134}
135
136/// A single autocomplete suggestion.
137#[derive(Debug, Clone, PartialEq)]
138pub struct CompletionItem {
139    /// The key/value candidate suggested for autocompletion.
140    pub key: String,
141    /// The JSONPath of the parent node context where this key applies.
142    pub path: String,
143}