@@ -2,81 +2,156 @@ use serde::{ser::SerializeTuple, Serialize};
2
2
3
3
use super :: PackageEntry ;
4
4
5
+ // Comment explaining entry schemas taken from bun.lock.zig
6
+ // first index is resolution for each type of package
7
+ // npm -> [
8
+ // "name@version",
9
+ // registry (TODO: remove if default),
10
+ // INFO,
11
+ // integrity
12
+ // ]
13
+ // symlink -> [ "name@link:path", INFO ]
14
+ // folder -> [ "name@file:path", INFO ]
15
+ // workspace -> [ "name@workspace:path", INFO ]
16
+ // tarball -> [ "name@tarball", INFO ]
17
+ // root -> [ "name@root:", { bin, binDir } ]
18
+ // git -> [ "name@git+repo", INFO, .bun-tag string (TODO: remove this) ]
19
+ // github -> [ "name@github:user/repo", INFO, .bun-tag string (TODO: remove
20
+ // this) ]
5
21
impl Serialize for PackageEntry {
6
22
fn serialize < S : serde:: Serializer > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error > {
7
- let mut tuple = serializer. serialize_tuple ( 5 ) ?;
23
+ let mut tuple = serializer. serialize_tuple ( 4 ) ?;
8
24
tuple. serialize_element ( & self . ident ) ?;
9
25
26
+ // For root packages, only thing left to serialize is the root info
27
+ if let Some ( root) = & self . root {
28
+ tuple. serialize_element ( root) ?;
29
+ return tuple. end ( ) ;
30
+ }
31
+
32
+ // npm packages have a registry
33
+ if let Some ( registry) = & self . registry {
34
+ tuple. serialize_element ( registry) ?;
35
+ }
36
+
37
+ // All packages have info in the next slot
10
38
if let Some ( info) = & self . info {
11
- tuple. serialize_element ( & self . registry . as_deref ( ) . unwrap_or ( "" ) ) ?;
12
39
tuple. serialize_element ( info) ?;
13
- tuple. serialize_element ( & self . checksum ) ?;
14
- if let Some ( root) = & self . root {
15
- tuple. serialize_element ( root) ?;
16
- }
17
40
} ;
18
41
42
+ // npm packages, git, and github have a checksum/integrity
43
+ if let Some ( checksum) = & self . checksum {
44
+ tuple. serialize_element ( checksum) ?;
45
+ }
46
+
19
47
tuple. end ( )
20
48
}
21
49
}
22
50
23
51
#[ cfg( test) ]
24
52
mod test {
53
+ use std:: { str:: FromStr , sync:: OnceLock } ;
54
+
25
55
use serde_json:: json;
56
+ use test_case:: test_case;
26
57
27
58
use super :: * ;
28
- use crate :: bun:: PackageInfo ;
59
+ use crate :: {
60
+ bun:: { PackageInfo , RootInfo , WorkspaceEntry } ,
61
+ BunLockfile ,
62
+ } ;
63
+
64
+ macro_rules! fixture {
65
+ ( $name: ident, $kind: ty, $cons: expr) => {
66
+ fn $name( ) -> & ' static $kind {
67
+ static ONCE : OnceLock <$kind> = OnceLock :: new( ) ;
68
+ ONCE . get_or_init( || $cons)
69
+ }
70
+ } ;
71
+ }
72
+
73
+ fixture ! (
74
+ basic_workspace,
75
+ WorkspaceEntry ,
76
+ WorkspaceEntry {
77
+ name: "bun-test" . into( ) ,
78
+ dev_dependencies: Some (
79
+ Some ( ( "turbo" . to_string( ) , "^2.3.3" . to_string( ) ) )
80
+ . into_iter( )
81
+ . collect( )
82
+ ) ,
83
+ ..Default :: default ( )
84
+ }
85
+ ) ;
86
+
87
+ fixture ! (
88
+ workspace_with_version,
89
+ WorkspaceEntry ,
90
+ WorkspaceEntry {
91
+ name: "docs" . into( ) ,
92
+ version: Some ( "0.1.0" . into( ) ) ,
93
+ ..Default :: default ( )
94
+ }
95
+ ) ;
29
96
30
- #[ test]
31
- fn test_serialize_registry_package ( ) {
32
- let package = PackageEntry {
97
+ fixture ! (
98
+ registry_pkg,
99
+ PackageEntry ,
100
+ PackageEntry {
33
101
ident
: "[email protected] " . into
( ) ,
34
- registry : Some ( "registry " . into ( ) ) ,
102
+ registry: Some ( "" . into( ) ) ,
35
103
info: Some ( PackageInfo {
36
- dependencies : [ ( "is-number" . into ( ) , "^6.0.0" . into ( ) ) ]
104
+ dependencies: Some ( ( "is-number" . into( ) , "^6.0.0" . into( ) ) )
37
105
. into_iter( )
38
106
. collect( ) ,
39
107
..Default :: default ( )
40
108
} ) ,
41
109
checksum: Some ( "sha" . into( ) ) ,
42
110
root: None ,
43
- } ;
44
-
45
- let expected =
46
- json ! ( [ "[email protected] " , "registry" , { "dependencies" : { "is-number" : "^6.0.0" } } , "sha" ] ) ;
47
- let actual = serde_json:: to_value ( & package) . unwrap ( ) ;
48
- assert_eq ! ( actual, expected) ;
49
- }
111
+ }
112
+ ) ;
50
113
51
- # [ test ]
52
- fn test_serialize_registry_package_no_deps ( ) {
53
- let package = PackageEntry {
54
- ident : "[email protected] " . into ( ) ,
55
- registry : Some ( "registry ". into ( ) ) ,
114
+ fixture ! (
115
+ workspace_pkg ,
116
+ PackageEntry ,
117
+ PackageEntry {
118
+ ident : "docs ". into( ) ,
56
119
info: Some ( PackageInfo {
120
+ dependencies: Some ( ( "is-odd" . into( ) , "3.0.1" . into( ) ) )
121
+ . into_iter( )
122
+ . collect( ) ,
57
123
..Default :: default ( )
58
124
} ) ,
59
- checksum : Some ( "sha" . into ( ) ) ,
60
- root : None ,
61
- } ;
62
-
63
- let expected =
json ! ( [ "[email protected] " , "registry" , { } , "sha" ] ) ;
64
- let actual = serde_json:: to_value ( & package) . unwrap ( ) ;
65
- assert_eq ! ( actual, expected) ;
66
- }
67
-
68
- #[ test]
69
- fn test_serialize_workspace_package ( ) {
70
- let package = PackageEntry {
71
- ident : "@workspace/package" . into ( ) ,
72
125
registry: None ,
73
- info : None ,
74
126
checksum: None ,
75
127
root: None ,
76
- } ;
128
+ }
129
+ ) ;
77
130
78
- let expected = json ! ( [ "@workspace/package" ] ) ;
79
- let actual = serde_json:: to_value ( & package) . unwrap ( ) ;
131
+ fixture ! (
132
+ root_pkg,
133
+ PackageEntry ,
134
+ PackageEntry {
135
+ ident: "some-package@root:" . into( ) ,
136
+ root: Some ( RootInfo {
137
+ bin: Some ( "bin" . into( ) ) ,
138
+ bin_dir: Some ( "binDir" . into( ) ) ,
139
+ } ) ,
140
+ info: None ,
141
+ registry: None ,
142
+ checksum: None ,
143
+ }
144
+ ) ;
145
+ #[ test_case( json!( { "name" : "bun-test" , "devDependencies" : { "turbo" : "^2.3.3" } } ) , basic_workspace( ) ; "basic" ) ]
146
+ #[ test_case( json!( { "name" : "docs" , "version" : "0.1.0" } ) , workspace_with_version( ) ; "with version" ) ]
147
+ #[ test_case( json!( [ "[email protected] " , "" , { "dependencies" : { "is-number" : "^6.0.0" } } , "sha" ] ) , registry_pkg( ) ; "registry package" ) ]
148
+ #[ test_case( json!( [ "docs" , { "dependencies" : { "is-odd" : "3.0.1" } } ] ) , workspace_pkg( ) ; "workspace package" ) ]
149
+ #[ test_case( json!( [ "some-package@root:" , { "bin" : "bin" , "binDir" : "binDir" } ] ) , root_pkg( ) ; "root package" ) ]
150
+ fn test_serialization < T : for < ' a > Serialize + PartialEq + std:: fmt:: Debug > (
151
+ expected : serde_json:: Value ,
152
+ input : & T ,
153
+ ) {
154
+ let actual = serde_json:: to_value ( input) . unwrap ( ) ;
80
155
assert_eq ! ( actual, expected) ;
81
156
}
82
157
}
0 commit comments