Skip to content

Commit 7c2ffc9

Browse files
Natalie Jamesonfacebook-github-bot
authored andcommitted
Make some types work with Default
Summary: Especially in testing, it can be tedious to create a lot of objects. Add some default implmentations so that `..Default::default()` syntax works Reviewed By: bobyangyf Differential Revision: D38452374 fbshipit-source-id: 5df299cf8fe8b6c37ca156c24e4ee6a7a6986e82
1 parent ca4ee39 commit 7c2ffc9

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

starlark/src/tests/docstring.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ def f4(a: "string") -> "string":
169169
}),
170170
},
171171
],
172-
ret: Return {
173-
docs: None,
174-
typ: None,
175-
},
172+
..Default::default()
176173
}));
177174

178175
let expected_f3 = Some(DocItem::Function(Function {
@@ -288,7 +285,6 @@ fn test_module_docs_return() {
288285
use crate::values::docs::DocString;
289286
use crate::values::docs::Function;
290287
use crate::values::docs::Module;
291-
use crate::values::docs::Return;
292288

293289
let mut a = Assert::new();
294290
let mod_a = r#"
@@ -341,14 +337,7 @@ def f1():
341337
let m2_docs = m2.module_documentation();
342338
let m3_docs = m3.module_documentation();
343339

344-
let empty_function = Some(DocItem::Function(Function {
345-
docs: None,
346-
params: vec![],
347-
ret: Return {
348-
docs: None,
349-
typ: None,
350-
},
351-
}));
340+
let empty_function = Some(DocItem::Function(Function::default()));
352341

353342
let expected_m1 = ModuleDocs {
354343
module: Some(DocItem::Module(Module {
@@ -363,11 +352,7 @@ Some extra details can go here,
363352
members: hashmap! {
364353
"f1".to_owned() => Some(DocItem::Function(Function {
365354
docs: DocString::from_docstring(DocStringKind::Starlark, "This is a function summary"),
366-
params: vec![],
367-
ret: Return {
368-
docs: None,
369-
typ: None,
370-
},
355+
..Default::default()
371356
})),
372357
"f2".to_owned() => empty_function.clone(),
373358
},

starlark/src/values/docs/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn indent_trimmed(s: &str, prefix: &str) -> String {
5454
}
5555

5656
/// The documentation provided by a user for a specific module, object, function, etc.
57-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Trace)]
57+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Trace, Default)]
5858
pub struct DocString {
5959
/// The first line of a doc string. This has whitespace trimmed from it.
6060
pub summary: String,
@@ -355,7 +355,7 @@ impl DocString {
355355
}
356356

357357
/// Line / column for where in a file a symbol is.
358-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
358+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
359359
pub struct Pos {
360360
/// Line number, zero based.
361361
pub line: usize,
@@ -364,7 +364,7 @@ pub struct Pos {
364364
}
365365

366366
/// The file a symbol resides in, and if available its location within that file.
367-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
367+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
368368
pub struct Location {
369369
/// `path` is a string that can be passed into `load()` statements.
370370
pub path: String,
@@ -373,7 +373,7 @@ pub struct Location {
373373
}
374374

375375
/// The main identifier for a symbol.
376-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
376+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
377377
pub struct Identifier {
378378
/// The name of the symbol (e.g. the function name, a name or path for a module, etc).
379379
pub name: String,
@@ -382,14 +382,14 @@ pub struct Identifier {
382382
}
383383

384384
/// The type of a given parameter, field, etc.
385-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
385+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
386386
pub struct Type {
387387
/// The type string that one would find in a starlark expression.
388388
pub raw_type: String,
389389
}
390390

391391
/// Documents a full module.
392-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
392+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
393393
pub struct Module {
394394
/// In general, this should be the first statement of a loaded file, if that statement is
395395
/// a string literal.
@@ -406,7 +406,7 @@ impl Module {
406406
}
407407

408408
/// Documents a single function.
409-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
409+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
410410
pub struct Function {
411411
/// Documentation for the function. If parsed, this should generally be the first statement
412412
/// of a function's body if that statement is a string literal. Any sections like "Args:",
@@ -669,7 +669,7 @@ impl Param {
669669
}
670670

671671
/// Details about the return value of a function.
672-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
672+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
673673
pub struct Return {
674674
/// Extra semantic details around the returned value's meaning.
675675
pub docs: Option<DocString>,
@@ -684,7 +684,7 @@ impl Return {
684684
}
685685

686686
/// A single property of an object. These are explicitly not functions (see [`Member`]).
687-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
687+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
688688
pub struct Property {
689689
pub docs: Option<DocString>,
690690
#[serde(rename = "type")]
@@ -724,7 +724,7 @@ pub enum Member {
724724
}
725725

726726
/// An object with named functions/properties.
727-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
727+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
728728
pub struct Object {
729729
pub docs: Option<DocString>,
730730
/// Name and details of each member of this object.

0 commit comments

Comments
 (0)