Skip to content

Commit 5c251ea

Browse files
Bahex132iklcptpiepmatz
authored
Mark Value variants as #[non_exhaustive] (nushell#16820)
`Value` can no longer be constructed directly using struct expression syntax outside of the defining crate (`nu-protocol`) Now it can only be constructed using the constructor methods. Co-authored-by: rose <[email protected]> Co-authored-by: Piepmatz <[email protected]>
1 parent c4eca64 commit 5c251ea

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

crates/nu-cli/tests/completions/support/completions_helpers.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@ pub fn new_external_engine() -> EngineState {
7676
let mut engine = create_default_context();
7777
let dir = fs::fixtures().join("external_completions").join("path");
7878
let dir_str = dir.to_string_lossy().to_string();
79-
let internal_span = nu_protocol::Span::new(0, dir_str.len());
79+
let span = nu_protocol::Span::new(0, dir_str.len());
8080
engine.add_env_var(
8181
"PATH".to_string(),
82-
Value::List {
83-
vals: vec![Value::String {
84-
val: dir_str,
85-
internal_span,
86-
}],
87-
internal_span,
88-
},
82+
Value::list(vec![Value::string(dir_str, span)], span),
8983
);
9084
engine
9185
}

crates/nu-protocol/src/value/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,45 @@ use std::{
4444
/// Core structured values that pass through the pipeline in Nushell.
4545
// NOTE: Please do not reorder these enum cases without thinking through the
4646
// impact on the PartialOrd implementation and the global sort order
47+
// NOTE: All variants are marked as `non_exhaustive` to prevent them
48+
// from being constructed (outside of this crate) with the struct
49+
// expression syntax. This makes using the constructor methods the
50+
// only way to construct `Value`'s
4751
#[derive(Debug, Serialize, Deserialize)]
4852
pub enum Value {
53+
#[non_exhaustive]
4954
Bool {
5055
val: bool,
5156
/// note: spans are being refactored out of Value
5257
/// please use .span() instead of matching this span value
5358
#[serde(rename = "span")]
5459
internal_span: Span,
5560
},
61+
#[non_exhaustive]
5662
Int {
5763
val: i64,
5864
/// note: spans are being refactored out of Value
5965
/// please use .span() instead of matching this span value
6066
#[serde(rename = "span")]
6167
internal_span: Span,
6268
},
69+
#[non_exhaustive]
6370
Float {
6471
val: f64,
6572
/// note: spans are being refactored out of Value
6673
/// please use .span() instead of matching this span value
6774
#[serde(rename = "span")]
6875
internal_span: Span,
6976
},
77+
#[non_exhaustive]
7078
String {
7179
val: String,
7280
/// note: spans are being refactored out of Value
7381
/// please use .span() instead of matching this span value
7482
#[serde(rename = "span")]
7583
internal_span: Span,
7684
},
85+
#[non_exhaustive]
7786
Glob {
7887
val: String,
7988
no_expand: bool,
@@ -82,13 +91,15 @@ pub enum Value {
8291
#[serde(rename = "span")]
8392
internal_span: Span,
8493
},
94+
#[non_exhaustive]
8595
Filesize {
8696
val: Filesize,
8797
/// note: spans are being refactored out of Value
8898
/// please use .span() instead of matching this span value
8999
#[serde(rename = "span")]
90100
internal_span: Span,
91101
},
102+
#[non_exhaustive]
92103
Duration {
93104
/// The duration in nanoseconds.
94105
val: i64,
@@ -97,69 +108,79 @@ pub enum Value {
97108
#[serde(rename = "span")]
98109
internal_span: Span,
99110
},
111+
#[non_exhaustive]
100112
Date {
101113
val: DateTime<FixedOffset>,
102114
/// note: spans are being refactored out of Value
103115
/// please use .span() instead of matching this span value
104116
#[serde(rename = "span")]
105117
internal_span: Span,
106118
},
119+
#[non_exhaustive]
107120
Range {
108121
val: Box<Range>,
109122
/// note: spans are being refactored out of Value
110123
/// please use .span() instead of matching this span value
111124
#[serde(rename = "span")]
112125
internal_span: Span,
113126
},
127+
#[non_exhaustive]
114128
Record {
115129
val: SharedCow<Record>,
116130
/// note: spans are being refactored out of Value
117131
/// please use .span() instead of matching this span value
118132
#[serde(rename = "span")]
119133
internal_span: Span,
120134
},
135+
#[non_exhaustive]
121136
List {
122137
vals: Vec<Value>,
123138
/// note: spans are being refactored out of Value
124139
/// please use .span() instead of matching this span value
125140
#[serde(rename = "span")]
126141
internal_span: Span,
127142
},
143+
#[non_exhaustive]
128144
Closure {
129145
val: Box<Closure>,
130146
/// note: spans are being refactored out of Value
131147
/// please use .span() instead of matching this span value
132148
#[serde(rename = "span")]
133149
internal_span: Span,
134150
},
151+
#[non_exhaustive]
135152
Error {
136153
error: Box<ShellError>,
137154
/// note: spans are being refactored out of Value
138155
/// please use .span() instead of matching this span value
139156
#[serde(rename = "span")]
140157
internal_span: Span,
141158
},
159+
#[non_exhaustive]
142160
Binary {
143161
val: Vec<u8>,
144162
/// note: spans are being refactored out of Value
145163
/// please use .span() instead of matching this span value
146164
#[serde(rename = "span")]
147165
internal_span: Span,
148166
},
167+
#[non_exhaustive]
149168
CellPath {
150169
val: CellPath,
151170
/// note: spans are being refactored out of Value
152171
/// please use .span() instead of matching this span value
153172
#[serde(rename = "span")]
154173
internal_span: Span,
155174
},
175+
#[non_exhaustive]
156176
Custom {
157177
val: Box<dyn CustomValue>,
158178
/// note: spans are being refactored out of Value
159179
/// please use .span() instead of matching this span value
160180
#[serde(rename = "span")]
161181
internal_span: Span,
162182
},
183+
#[non_exhaustive]
163184
Nothing {
164185
/// note: spans are being refactored out of Value
165186
/// please use .span() instead of matching this span value

crates/nu_plugin_polars/src/dataframe/values/nu_dtype/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ impl NuDataType {
3838

3939
impl From<NuDataType> for Value {
4040
fn from(nu_dtype: NuDataType) -> Self {
41-
Value::String {
42-
val: nu_dtype.dtype.to_string(),
43-
internal_span: Span::unknown(),
44-
}
41+
Value::string(nu_dtype.dtype.to_string(), Span::unknown())
4542
}
4643
}
4744

0 commit comments

Comments
 (0)