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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
|
use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt;
use std::fs;
use std::path::Path;
pub mod archiver;
pub mod dumper;
pub mod version;
/// Package stability level.
/// Higher value = less stable.
/// Corresponds to `Composer\Package\BasePackage::STABILITY_*`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(u8)]
pub enum Stability {
#[default]
Stable = 0,
RC = 5,
Beta = 10,
Alpha = 15,
Dev = 20,
}
impl Stability {
/// Parse a stability string (case-insensitive) into a `Stability` value.
///
/// Recognizes: "stable", "RC", "beta", "alpha", "dev".
/// Defaults to `Stability::Stable` for unrecognized values.
pub fn parse(s: &str) -> Self {
match s.to_lowercase().as_str() {
"dev" => Stability::Dev,
"alpha" => Stability::Alpha,
"beta" => Stability::Beta,
"rc" => Stability::RC,
_ => Stability::Stable,
}
}
}
/// A versioned relationship between two packages.
/// Corresponds to `Composer\Package\Link`.
#[derive(Debug, Clone)]
pub struct Link {
pub source: String,
pub target: String,
pub constraint: String,
pub pretty_constraint: Option<String>,
pub description: String,
}
/// Package author metadata.
#[derive(Debug, Clone)]
pub struct Author {
pub name: Option<String>,
pub email: Option<String>,
pub homepage: Option<String>,
pub role: Option<String>,
}
/// Autoload rule sets (PSR-4, PSR-0, classmap, files).
#[derive(Debug, Clone, Default)]
pub struct AutoloadRules {
pub psr4: BTreeMap<String, Vec<String>>,
pub psr0: BTreeMap<String, Vec<String>>,
pub classmap: Vec<String>,
pub files: Vec<String>,
}
/// Support channel information.
#[derive(Debug, Clone, Default)]
pub struct Support {
pub email: Option<String>,
pub issues: Option<String>,
pub forum: Option<String>,
pub wiki: Option<String>,
pub source: Option<String>,
pub docs: Option<String>,
pub irc: Option<String>,
pub chat: Option<String>,
pub rss: Option<String>,
pub security: Option<String>,
}
/// Funding link.
#[derive(Debug, Clone)]
pub struct Funding {
pub url: Option<String>,
pub funding_type: Option<String>,
}
/// Version alias entry for root packages.
#[derive(Debug, Clone)]
pub struct VersionAlias {
pub package: String,
pub version: String,
pub alias: String,
pub alias_normalized: String,
}
/// Core package data covering `BasePackage` + `Package` fields.
/// Corresponds to `Composer\Package\Package` (implements `PackageInterface`).
#[derive(Debug, Clone)]
pub struct PackageData {
// BasePackage fields
pub name: String,
pub pretty_name: String,
// Package fields
pub version: String,
pub pretty_version: String,
pub package_type: String,
pub target_dir: Option<String>,
// source
pub source_type: Option<String>,
pub source_url: Option<String>,
pub source_reference: Option<String>,
// dist
pub dist_type: Option<String>,
pub dist_url: Option<String>,
pub dist_reference: Option<String>,
pub dist_sha1_checksum: Option<String>,
pub release_date: Option<String>,
pub extra: BTreeMap<String, serde_json::Value>,
pub binaries: Vec<String>,
pub dev: bool,
pub stability: Stability,
pub notification_url: Option<String>,
// dependency links
pub requires: BTreeMap<String, Link>,
pub conflicts: BTreeMap<String, Link>,
pub provides: BTreeMap<String, Link>,
pub replaces: BTreeMap<String, Link>,
pub dev_requires: BTreeMap<String, Link>,
pub suggests: BTreeMap<String, String>,
// autoload
pub autoload: AutoloadRules,
pub dev_autoload: AutoloadRules,
pub is_default_branch: bool,
}
/// Package with full metadata (description, authors, license, etc.).
/// Corresponds to `Composer\Package\CompletePackage`.
#[derive(Debug, Clone)]
pub struct CompletePackageData {
pub package: PackageData,
pub description: Option<String>,
pub homepage: Option<String>,
pub license: Vec<String>,
pub keywords: Vec<String>,
pub authors: Vec<Author>,
pub scripts: BTreeMap<String, Vec<String>>,
pub support: Support,
pub funding: Vec<Funding>,
pub repositories: Vec<serde_json::Value>,
/// `None` = not abandoned, `Some("")` = abandoned, `Some(pkg)` = replaced by pkg.
pub abandoned: Option<String>,
pub archive_name: Option<String>,
pub archive_excludes: Vec<String>,
}
/// The root project package with project-level configuration.
/// Corresponds to `Composer\Package\RootPackage`.
#[derive(Debug, Clone)]
pub struct RootPackageData {
pub complete: CompletePackageData,
pub minimum_stability: Stability,
pub prefer_stable: bool,
pub stability_flags: BTreeMap<String, Stability>,
pub config: BTreeMap<String, serde_json::Value>,
pub references: BTreeMap<String, String>,
pub aliases: Vec<VersionAlias>,
}
/// Accessor for `PackageData` fields.
/// Corresponds to `Composer\Package\PackageInterface`.
pub trait Package {
fn name(&self) -> &str;
fn pretty_name(&self) -> &str;
fn version(&self) -> &str;
fn pretty_version(&self) -> &str;
fn package_type(&self) -> &str;
fn target_dir(&self) -> Option<&str>;
fn source_type(&self) -> Option<&str>;
fn source_url(&self) -> Option<&str>;
fn source_reference(&self) -> Option<&str>;
fn dist_type(&self) -> Option<&str>;
fn dist_url(&self) -> Option<&str>;
fn dist_reference(&self) -> Option<&str>;
fn dist_sha1_checksum(&self) -> Option<&str>;
fn release_date(&self) -> Option<&str>;
fn extra(&self) -> &BTreeMap<String, serde_json::Value>;
fn binaries(&self) -> &[String];
fn is_dev(&self) -> bool;
fn stability(&self) -> Stability;
fn notification_url(&self) -> Option<&str>;
fn requires(&self) -> &BTreeMap<String, Link>;
fn conflicts(&self) -> &BTreeMap<String, Link>;
fn provides(&self) -> &BTreeMap<String, Link>;
fn replaces(&self) -> &BTreeMap<String, Link>;
fn dev_requires(&self) -> &BTreeMap<String, Link>;
fn suggests(&self) -> &BTreeMap<String, String>;
fn autoload(&self) -> &AutoloadRules;
fn dev_autoload(&self) -> &AutoloadRules;
fn is_default_branch(&self) -> bool;
}
/// Accessor for `CompletePackageData` fields.
/// Corresponds to `Composer\Package\CompletePackageInterface`.
pub trait CompletePackage: Package {
fn description(&self) -> Option<&str>;
fn homepage(&self) -> Option<&str>;
fn license(&self) -> &[String];
fn keywords(&self) -> &[String];
fn authors(&self) -> &[Author];
fn scripts(&self) -> &BTreeMap<String, Vec<String>>;
fn support(&self) -> &Support;
fn funding(&self) -> &[Funding];
fn repositories(&self) -> &[serde_json::Value];
fn abandoned(&self) -> Option<&str>;
fn archive_name(&self) -> Option<&str>;
fn archive_excludes(&self) -> &[String];
}
/// Accessor for `RootPackageData` fields.
/// Corresponds to `Composer\Package\RootPackageInterface`.
pub trait RootPackage: CompletePackage {
fn minimum_stability(&self) -> Stability;
fn prefer_stable(&self) -> bool;
fn stability_flags(&self) -> &BTreeMap<String, Stability>;
fn config(&self) -> &BTreeMap<String, serde_json::Value>;
fn references(&self) -> &BTreeMap<String, String>;
fn aliases(&self) -> &[VersionAlias];
}
/// Implements `Package` trait by delegating to an inner `PackageData` field.
macro_rules! delegate_package {
($type:ty => $($path:ident).+) => {
impl Package for $type {
fn name(&self) -> &str { &self.$($path).+.name }
fn pretty_name(&self) -> &str { &self.$($path).+.pretty_name }
fn version(&self) -> &str { &self.$($path).+.version }
fn pretty_version(&self) -> &str { &self.$($path).+.pretty_version }
fn package_type(&self) -> &str { &self.$($path).+.package_type }
fn target_dir(&self) -> Option<&str> { self.$($path).+.target_dir.as_deref() }
fn source_type(&self) -> Option<&str> { self.$($path).+.source_type.as_deref() }
fn source_url(&self) -> Option<&str> { self.$($path).+.source_url.as_deref() }
fn source_reference(&self) -> Option<&str> { self.$($path).+.source_reference.as_deref() }
fn dist_type(&self) -> Option<&str> { self.$($path).+.dist_type.as_deref() }
fn dist_url(&self) -> Option<&str> { self.$($path).+.dist_url.as_deref() }
fn dist_reference(&self) -> Option<&str> { self.$($path).+.dist_reference.as_deref() }
fn dist_sha1_checksum(&self) -> Option<&str> { self.$($path).+.dist_sha1_checksum.as_deref() }
fn release_date(&self) -> Option<&str> { self.$($path).+.release_date.as_deref() }
fn extra(&self) -> &BTreeMap<String, serde_json::Value> { &self.$($path).+.extra }
fn binaries(&self) -> &[String] { &self.$($path).+.binaries }
fn is_dev(&self) -> bool { self.$($path).+.dev }
fn stability(&self) -> Stability { self.$($path).+.stability }
fn notification_url(&self) -> Option<&str> { self.$($path).+.notification_url.as_deref() }
fn requires(&self) -> &BTreeMap<String, Link> { &self.$($path).+.requires }
fn conflicts(&self) -> &BTreeMap<String, Link> { &self.$($path).+.conflicts }
fn provides(&self) -> &BTreeMap<String, Link> { &self.$($path).+.provides }
fn replaces(&self) -> &BTreeMap<String, Link> { &self.$($path).+.replaces }
fn dev_requires(&self) -> &BTreeMap<String, Link> { &self.$($path).+.dev_requires }
fn suggests(&self) -> &BTreeMap<String, String> { &self.$($path).+.suggests }
fn autoload(&self) -> &AutoloadRules { &self.$($path).+.autoload }
fn dev_autoload(&self) -> &AutoloadRules { &self.$($path).+.dev_autoload }
fn is_default_branch(&self) -> bool { self.$($path).+.is_default_branch }
}
};
}
/// Implements `CompletePackage` trait by delegating to an inner `CompletePackageData` field.
macro_rules! delegate_complete_package {
($type:ty => $($path:ident).+) => {
impl CompletePackage for $type {
fn description(&self) -> Option<&str> { self.$($path).+.description.as_deref() }
fn homepage(&self) -> Option<&str> { self.$($path).+.homepage.as_deref() }
fn license(&self) -> &[String] { &self.$($path).+.license }
fn keywords(&self) -> &[String] { &self.$($path).+.keywords }
fn authors(&self) -> &[Author] { &self.$($path).+.authors }
fn scripts(&self) -> &BTreeMap<String, Vec<String>> { &self.$($path).+.scripts }
fn support(&self) -> &Support { &self.$($path).+.support }
fn funding(&self) -> &[Funding] { &self.$($path).+.funding }
fn repositories(&self) -> &[serde_json::Value] { &self.$($path).+.repositories }
fn abandoned(&self) -> Option<&str> { self.$($path).+.abandoned.as_deref() }
fn archive_name(&self) -> Option<&str> { self.$($path).+.archive_name.as_deref() }
fn archive_excludes(&self) -> &[String] { &self.$($path).+.archive_excludes }
}
};
}
impl Package for PackageData {
fn name(&self) -> &str {
&self.name
}
fn pretty_name(&self) -> &str {
&self.pretty_name
}
fn version(&self) -> &str {
&self.version
}
fn pretty_version(&self) -> &str {
&self.pretty_version
}
fn package_type(&self) -> &str {
&self.package_type
}
fn target_dir(&self) -> Option<&str> {
self.target_dir.as_deref()
}
fn source_type(&self) -> Option<&str> {
self.source_type.as_deref()
}
fn source_url(&self) -> Option<&str> {
self.source_url.as_deref()
}
fn source_reference(&self) -> Option<&str> {
self.source_reference.as_deref()
}
fn dist_type(&self) -> Option<&str> {
self.dist_type.as_deref()
}
fn dist_url(&self) -> Option<&str> {
self.dist_url.as_deref()
}
fn dist_reference(&self) -> Option<&str> {
self.dist_reference.as_deref()
}
fn dist_sha1_checksum(&self) -> Option<&str> {
self.dist_sha1_checksum.as_deref()
}
fn release_date(&self) -> Option<&str> {
self.release_date.as_deref()
}
fn extra(&self) -> &BTreeMap<String, serde_json::Value> {
&self.extra
}
fn binaries(&self) -> &[String] {
&self.binaries
}
fn is_dev(&self) -> bool {
self.dev
}
fn stability(&self) -> Stability {
self.stability
}
fn notification_url(&self) -> Option<&str> {
self.notification_url.as_deref()
}
fn requires(&self) -> &BTreeMap<String, Link> {
&self.requires
}
fn conflicts(&self) -> &BTreeMap<String, Link> {
&self.conflicts
}
fn provides(&self) -> &BTreeMap<String, Link> {
&self.provides
}
fn replaces(&self) -> &BTreeMap<String, Link> {
&self.replaces
}
fn dev_requires(&self) -> &BTreeMap<String, Link> {
&self.dev_requires
}
fn suggests(&self) -> &BTreeMap<String, String> {
&self.suggests
}
fn autoload(&self) -> &AutoloadRules {
&self.autoload
}
fn dev_autoload(&self) -> &AutoloadRules {
&self.dev_autoload
}
fn is_default_branch(&self) -> bool {
self.is_default_branch
}
}
impl CompletePackage for CompletePackageData {
fn description(&self) -> Option<&str> {
self.description.as_deref()
}
fn homepage(&self) -> Option<&str> {
self.homepage.as_deref()
}
fn license(&self) -> &[String] {
&self.license
}
fn keywords(&self) -> &[String] {
&self.keywords
}
fn authors(&self) -> &[Author] {
&self.authors
}
fn scripts(&self) -> &BTreeMap<String, Vec<String>> {
&self.scripts
}
fn support(&self) -> &Support {
&self.support
}
fn funding(&self) -> &[Funding] {
&self.funding
}
fn repositories(&self) -> &[serde_json::Value] {
&self.repositories
}
fn abandoned(&self) -> Option<&str> {
self.abandoned.as_deref()
}
fn archive_name(&self) -> Option<&str> {
self.archive_name.as_deref()
}
fn archive_excludes(&self) -> &[String] {
&self.archive_excludes
}
}
impl RootPackage for RootPackageData {
fn minimum_stability(&self) -> Stability {
self.minimum_stability
}
fn prefer_stable(&self) -> bool {
self.prefer_stable
}
fn stability_flags(&self) -> &BTreeMap<String, Stability> {
&self.stability_flags
}
fn config(&self) -> &BTreeMap<String, serde_json::Value> {
&self.config
}
fn references(&self) -> &BTreeMap<String, String> {
&self.references
}
fn aliases(&self) -> &[VersionAlias] {
&self.aliases
}
}
// CompletePackageData delegates Package → inner PackageData
delegate_package!(CompletePackageData => package);
// RootPackageData delegates Package → inner CompletePackageData → PackageData
delegate_package!(RootPackageData => complete.package);
// RootPackageData delegates CompletePackage → inner CompletePackageData
delegate_complete_package!(RootPackageData => complete);
/// Unstructured representation of a composer.json file.
/// Used by `init` and `create-project` to write a new composer.json.
/// Unlike the typed hierarchy above, all fields live at a single level
/// and map directly to the JSON keys via serde.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawPackageData {
#[serde(default = "default_root_package_name")]
pub name: String,
/// Root project's version, when explicitly set. Composer falls back to
/// `1.0.0+no-version-set` when this is missing; we keep the raw `Option`
/// here and let the resolver apply that default so the in-memory shape
/// stays close to the JSON input.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub package_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub authors: Vec<RawAuthor>,
#[serde(rename = "minimum-stability", skip_serializing_if = "Option::is_none")]
pub minimum_stability: Option<String>,
#[serde(default)]
pub require: BTreeMap<String, String>,
#[serde(
rename = "require-dev",
default,
skip_serializing_if = "BTreeMap::is_empty"
)]
pub require_dev: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub conflict: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub provide: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub replace: BTreeMap<String, String>,
/// `repositories` accepts either a JSON array or a JSON object keyed by
/// repository name. Composer iterates `foreach ($repoConfigs as ...)` in
/// `RepositoryFactory::createRepos`, so PHP transparently handles either
/// shape; mirror that here.
#[serde(
default,
deserialize_with = "deserialize_repositories",
skip_serializing_if = "Vec::is_empty"
)]
pub repositories: Vec<RawRepository>,
#[serde(skip_serializing_if = "Option::is_none")]
pub autoload: Option<RawAutoload>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub bin: Vec<String>,
#[serde(flatten)]
pub extra_fields: BTreeMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawAuthor {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawAutoload {
#[serde(rename = "psr-4")]
pub psr4: BTreeMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawRepository {
#[serde(rename = "type")]
pub repo_type: String,
/// Required for VCS / composer / artifact / path types; absent for inline
/// `package` repositories. Modeled as Option to mirror Composer's
/// per-type schema (Composer enforces presence in `RepositoryFactory`,
/// not at the JSON-parse step).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// Inline package definition(s) for `type: package` repositories. Either
/// a single object or an array of objects, mirroring
/// `Composer\Repository\PackageRepository`'s schema.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub package: Option<serde_json::Value>,
/// `only: ["name", ...]` — restrict this repository to the listed package
/// names (glob `*` allowed). Mirrors `Composer\Repository\FilterRepository`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub only: Option<Vec<String>>,
/// `exclude: ["name", ...]` — drop the listed package names from this
/// repository. Mutually exclusive with `only` per `FilterRepository`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub exclude: Option<Vec<String>>,
/// `canonical: false` — packages from this repo enter the pool but do
/// not claim authoritative ownership of their names, so lower-priority
/// repositories can still answer for the same name. Mirrors
/// `FilterRepository::loadPackages`'s `namesFound = []` reset.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub canonical: Option<bool>,
/// Inline `security-advisories` block on a repository entry. Maps package
/// name → list of advisory objects whose `affectedVersions` constraint
/// (and `advisoryId`) is read by the resolver when
/// `config.audit.block-insecure` is set: matching versions are filtered
/// out of the pool before solving, mirroring Composer's
/// `SecurityAdvisoryPoolFilter`.
#[serde(
rename = "security-advisories",
default,
skip_serializing_if = "Option::is_none"
)]
pub security_advisories: Option<serde_json::Value>,
}
/// Default root-package name when `composer.json` omits the `name` field.
/// Mirrors Composer's `RootPackageLoader` fallback.
fn default_root_package_name() -> String {
"__root__".to_string()
}
/// Deserialize `repositories` from either a JSON array or a JSON object.
/// PHP's `json_decode($x, true)` produces an associative array in either
/// case, and `RepositoryFactory::createRepos` iterates the values without
/// caring whether the key was an int (array) or a string (object). The map
/// keys are dropped — `RawRepository` doesn't carry a name field, and
/// downstream code doesn't depend on the original keying.
fn deserialize_repositories<'de, D>(deserializer: D) -> Result<Vec<RawRepository>, D::Error>
where
D: Deserializer<'de>,
{
struct RepositoriesVisitor;
impl<'de> Visitor<'de> for RepositoriesVisitor {
type Value = Vec<RawRepository>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("a sequence or map of repository definitions")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut out = Vec::with_capacity(seq.size_hint().unwrap_or(0));
while let Some(repo) = seq.next_element::<RawRepository>()? {
out.push(repo);
}
Ok(out)
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut out = Vec::with_capacity(map.size_hint().unwrap_or(0));
while let Some((_key, repo)) = map.next_entry::<String, RawRepository>()? {
out.push(repo);
}
Ok(out)
}
}
deserializer.deserialize_any(RepositoriesVisitor)
}
impl RawPackageData {
pub fn new(name: String) -> Self {
Self {
name,
version: None,
description: None,
package_type: None,
homepage: None,
license: None,
authors: Vec::new(),
minimum_stability: None,
require: BTreeMap::new(),
require_dev: BTreeMap::new(),
conflict: BTreeMap::new(),
provide: BTreeMap::new(),
replace: BTreeMap::new(),
repositories: Vec::new(),
autoload: None,
bin: Vec::new(),
extra_fields: BTreeMap::new(),
}
}
/// Reject root composer.json that requires its own package name.
///
/// Mirrors the check in Composer's
/// `Composer\Package\Loader\RootPackageLoader::load()` (RuntimeException
/// thrown for `$config['require'][$config['name']]` /
/// `$config['require-dev'][$config['name']]`).
pub fn validate_root_does_not_self_require(&self) -> anyhow::Result<()> {
if self.require.contains_key(&self.name) || self.require_dev.contains_key(&self.name) {
anyhow::bail!(
"Root package '{}' cannot require itself in its composer.json\nDid you accidentally name your root package after an external package?",
self.name
);
}
Ok(())
}
}
pub fn read_from_file(path: &Path) -> anyhow::Result<RawPackageData> {
let content = fs::read_to_string(path)?;
let data: RawPackageData = serde_json::from_str(&content)?;
Ok(data)
}
impl RootPackageData {
/// Mirrors `Composer\Package\Loader\RootPackageLoader::load()`:
/// converts the raw, untyped `RawPackageData` (the parsed-JSON form)
/// into the fully typed `RootPackageData`, applying field defaults and
/// constructing [`Link`] objects from the raw constraint strings.
pub fn from_raw(raw: RawPackageData) -> Self {
fn make_links(
source: &str,
deps: BTreeMap<String, String>,
description: &str,
) -> BTreeMap<String, Link> {
deps.into_iter()
.map(|(target, constraint)| {
let normalized = target.to_lowercase();
let link = Link {
source: source.to_string(),
target: normalized.clone(),
constraint,
pretty_constraint: None,
description: description.to_string(),
};
(normalized, link)
})
.collect()
}
let pretty_name = raw.name.clone();
let name = raw.name.to_lowercase();
let pretty_version = raw
.version
.unwrap_or_else(|| "1.0.0+no-version-set".to_string());
let version = pretty_version.clone();
let package_type = raw
.package_type
.map(|t| t.to_lowercase())
.unwrap_or_else(|| "library".to_string());
let requires = make_links(&name, raw.require, "requires");
let dev_requires = make_links(&name, raw.require_dev, "requires (for development)");
let conflicts = make_links(&name, raw.conflict, "conflicts");
let provides = make_links(&name, raw.provide, "provides");
let replaces = make_links(&name, raw.replace, "replaces");
let autoload = raw
.autoload
.map(|a| AutoloadRules {
psr4: a
.psr4
.into_iter()
.map(|(ns, path)| (ns, vec![path]))
.collect(),
..Default::default()
})
.unwrap_or_default();
let extra: BTreeMap<String, serde_json::Value> = raw
.extra_fields
.get("extra")
.and_then(|v| v.as_object())
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default();
let license = raw.license.into_iter().collect();
let authors = raw
.authors
.into_iter()
.map(|a| Author {
name: Some(a.name),
email: a.email,
homepage: None,
role: None,
})
.collect();
let repositories = raw
.repositories
.into_iter()
.filter_map(|r| serde_json::to_value(r).ok())
.collect();
let keywords = raw
.extra_fields
.get("keywords")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.map(String::from)
.collect()
})
.unwrap_or_default();
let scripts: BTreeMap<String, Vec<String>> = raw
.extra_fields
.get("scripts")
.and_then(|v| v.as_object())
.map(|obj| {
obj.iter()
.map(|(k, v)| {
let handlers = match v {
serde_json::Value::String(s) => vec![s.clone()],
serde_json::Value::Array(arr) => arr
.iter()
.filter_map(|x| x.as_str())
.map(String::from)
.collect(),
_ => vec![],
};
(k.clone(), handlers)
})
.collect()
})
.unwrap_or_default();
let support = raw
.extra_fields
.get("support")
.and_then(|v| v.as_object())
.map(|obj| {
let get_str = |key: &str| obj.get(key).and_then(|v| v.as_str()).map(String::from);
Support {
email: get_str("email"),
issues: get_str("issues"),
forum: get_str("forum"),
wiki: get_str("wiki"),
source: get_str("source"),
docs: get_str("docs"),
irc: get_str("irc"),
chat: get_str("chat"),
rss: get_str("rss"),
security: get_str("security"),
}
})
.unwrap_or_default();
let funding = raw
.extra_fields
.get("funding")
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_object())
.map(|obj| Funding {
url: obj.get("url").and_then(|v| v.as_str()).map(String::from),
funding_type: obj.get("type").and_then(|v| v.as_str()).map(String::from),
})
.collect()
})
.unwrap_or_default();
let config: BTreeMap<String, serde_json::Value> = raw
.extra_fields
.get("config")
.and_then(|v| v.as_object())
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default();
let prefer_stable = raw
.extra_fields
.get("prefer-stable")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let minimum_stability =
Stability::parse(raw.minimum_stability.as_deref().unwrap_or("stable"));
let package_data = PackageData {
name,
pretty_name,
version,
pretty_version,
package_type,
target_dir: None,
source_type: None,
source_url: None,
source_reference: None,
dist_type: None,
dist_url: None,
dist_reference: None,
dist_sha1_checksum: None,
release_date: None,
extra,
binaries: raw.bin,
dev: false,
stability: Stability::Stable,
notification_url: None,
requires,
conflicts,
provides,
replaces,
dev_requires,
suggests: BTreeMap::new(),
autoload,
dev_autoload: AutoloadRules::default(),
is_default_branch: false,
};
let complete_data = CompletePackageData {
package: package_data,
description: raw.description,
homepage: raw.homepage,
license,
keywords,
authors,
scripts,
support,
funding,
repositories,
abandoned: None,
archive_name: None,
archive_excludes: Vec::new(),
};
RootPackageData {
complete: complete_data,
minimum_stability,
prefer_stable,
stability_flags: BTreeMap::new(),
config,
references: BTreeMap::new(),
aliases: Vec::new(),
}
}
}
pub fn to_json_pretty(value: &impl Serialize) -> serde_json::Result<String> {
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut buf = Vec::new();
let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
value.serialize(&mut ser)?;
let mut json = String::from_utf8(buf).expect("serde_json produces valid UTF-8");
json.push('\n');
Ok(json)
}
pub fn write_to_file(value: &impl Serialize, path: &Path) -> anyhow::Result<()> {
let json = to_json_pretty(value)?;
fs::write(path, json)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn raw_minimal_json() {
let raw = RawPackageData::new("test/pkg".to_string());
let json = to_json_pretty(&raw).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(parsed["name"], "test/pkg");
assert!(parsed["require"].is_object());
assert!(parsed.get("description").is_none());
assert!(parsed.get("type").is_none());
assert!(parsed.get("authors").is_none());
assert!(parsed.get("require-dev").is_none());
assert!(parsed.get("autoload").is_none());
}
#[test]
fn raw_full_json() {
let mut raw = RawPackageData::new("acme/full".to_string());
raw.description = Some("A full package".to_string());
raw.package_type = Some("library".to_string());
raw.homepage = Some("https://example.com".to_string());
raw.license = Some("MIT".to_string());
raw.authors = vec![RawAuthor {
name: "Jane Doe".to_string(),
email: Some("jane@example.com".to_string()),
}];
raw.minimum_stability = Some("dev".to_string());
raw.require.insert("php".to_string(), ">=8.1".to_string());
raw.require_dev
.insert("phpunit/phpunit".to_string(), "^10.0".to_string());
raw.repositories = vec![RawRepository {
repo_type: "vcs".to_string(),
url: Some("https://github.com/acme/repo".to_string()),
package: None,
only: None,
exclude: None,
canonical: None,
security_advisories: None,
}];
let mut psr4 = BTreeMap::new();
psr4.insert("Acme\\Full\\".to_string(), "src/".to_string());
raw.autoload = Some(RawAutoload { psr4 });
let json = to_json_pretty(&raw).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
assert_eq!(parsed["name"], "acme/full");
assert_eq!(parsed["description"], "A full package");
assert_eq!(parsed["type"], "library");
assert_eq!(parsed["homepage"], "https://example.com");
assert_eq!(parsed["license"], "MIT");
assert_eq!(parsed["minimum-stability"], "dev");
assert_eq!(parsed["authors"][0]["name"], "Jane Doe");
assert_eq!(parsed["authors"][0]["email"], "jane@example.com");
assert_eq!(parsed["require"]["php"], ">=8.1");
assert_eq!(parsed["require-dev"]["phpunit/phpunit"], "^10.0");
assert_eq!(parsed["repositories"][0]["type"], "vcs");
assert_eq!(parsed["autoload"]["psr-4"]["Acme\\Full\\"], "src/");
}
#[test]
fn raw_deserialize_minimal() {
let json = r#"{"name": "test/pkg"}"#;
let raw: RawPackageData = serde_json::from_str(json).unwrap();
assert_eq!(raw.name, "test/pkg");
assert!(raw.description.is_none());
assert!(raw.require.is_empty());
assert!(raw.require_dev.is_empty());
assert!(raw.authors.is_empty());
assert!(raw.extra_fields.is_empty());
}
#[test]
fn raw_roundtrip_preserves_all_fields() {
let mut raw = RawPackageData::new("acme/roundtrip".to_string());
raw.description = Some("Test roundtrip".to_string());
raw.require.insert("php".to_string(), ">=8.1".to_string());
raw.require_dev
.insert("phpunit/phpunit".to_string(), "^10.0".to_string());
let json1 = to_json_pretty(&raw).unwrap();
let deserialized: RawPackageData = serde_json::from_str(&json1).unwrap();
let json2 = to_json_pretty(&deserialized).unwrap();
assert_eq!(json1, json2);
}
#[test]
fn raw_extra_fields_preserved() {
let json = r#"{
"name": "test/extra",
"require": {},
"scripts": {"post-install-cmd": ["echo hello"]},
"config": {"sort-packages": true},
"extra": {"custom-key": "custom-value"}
}"#;
let raw: RawPackageData = serde_json::from_str(json).unwrap();
assert_eq!(raw.name, "test/extra");
assert!(raw.extra_fields.contains_key("scripts"));
assert!(raw.extra_fields.contains_key("config"));
assert!(raw.extra_fields.contains_key("extra"));
// Roundtrip: extra fields should be preserved in output
let output = to_json_pretty(&raw).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
assert!(parsed["scripts"].is_object());
assert!(parsed["config"].is_object());
assert!(parsed["extra"].is_object());
}
#[test]
fn raw_read_from_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("composer.json");
let content = r#"{"name": "test/file", "require": {"php": ">=8.0"}}"#;
std::fs::write(&path, content).unwrap();
let raw = read_from_file(&path).unwrap();
assert_eq!(raw.name, "test/file");
assert_eq!(raw.require.get("php").unwrap(), ">=8.0");
}
#[test]
fn raw_none_fields_omitted() {
let raw = RawPackageData::new("test/empty".to_string());
let json = to_json_pretty(&raw).unwrap();
assert!(!json.contains("\"description\""));
assert!(!json.contains("\"type\""));
assert!(!json.contains("\"homepage\""));
assert!(!json.contains("\"license\""));
assert!(!json.contains("\"authors\""));
assert!(!json.contains("\"minimum-stability\""));
assert!(!json.contains("\"require-dev\""));
assert!(!json.contains("\"repositories\""));
assert!(!json.contains("\"autoload\""));
}
#[test]
fn validate_root_self_require_rejects_self_in_require() {
let mut raw = RawPackageData::new("foo/bar".to_string());
raw.require
.insert("foo/bar".to_string(), "@dev".to_string());
let err = raw.validate_root_does_not_self_require().unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("Root package 'foo/bar' cannot require itself"),
"{msg}"
);
assert!(msg.contains("accidentally name your root package"), "{msg}");
}
#[test]
fn validate_root_self_require_rejects_self_in_require_dev() {
let mut raw = RawPackageData::new("foo/bar".to_string());
raw.require_dev
.insert("foo/bar".to_string(), "@dev".to_string());
assert!(raw.validate_root_does_not_self_require().is_err());
}
#[test]
fn validate_root_self_require_accepts_other_packages() {
let mut raw = RawPackageData::new("foo/bar".to_string());
raw.require
.insert("vendor/lib".to_string(), "^1.0".to_string());
raw.require_dev
.insert("vendor/dev-tool".to_string(), "^2.0".to_string());
assert!(raw.validate_root_does_not_self_require().is_ok());
}
#[test]
fn validate_root_self_require_accepts_empty_requires() {
let raw = RawPackageData::new("foo/bar".to_string());
assert!(raw.validate_root_does_not_self_require().is_ok());
}
#[test]
fn raw_repositories_array_form() {
let json = r#"{
"name": "test/array",
"repositories": [
{"type": "vcs", "url": "https://example.com/a"},
{"type": "vcs", "url": "https://example.com/b"}
]
}"#;
let raw: RawPackageData = serde_json::from_str(json).unwrap();
assert_eq!(raw.repositories.len(), 2);
assert_eq!(
raw.repositories[0].url.as_deref(),
Some("https://example.com/a")
);
assert_eq!(
raw.repositories[1].url.as_deref(),
Some("https://example.com/b")
);
}
#[test]
fn raw_repositories_object_form() {
let json = r#"{
"name": "test/object",
"repositories": {
"first": {"type": "vcs", "url": "https://example.com/a"},
"second": {"type": "package", "package": {"name": "x/y", "version": "1.0.0"}}
}
}"#;
let raw: RawPackageData = serde_json::from_str(json).unwrap();
assert_eq!(raw.repositories.len(), 2);
assert_eq!(raw.repositories[0].repo_type, "vcs");
assert_eq!(
raw.repositories[0].url.as_deref(),
Some("https://example.com/a")
);
assert_eq!(raw.repositories[1].repo_type, "package");
assert!(raw.repositories[1].package.is_some());
}
}
|