forc_pkg/manifest/
mod.rs

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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
pub mod build_profile;

use crate::pkg::{manifest_file_missing, parsing_failed, wrong_program_type};
use anyhow::{anyhow, bail, Context, Result};
use forc_tracing::println_warning;
use forc_util::{validate_name, validate_project_name};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::{
    collections::{BTreeMap, HashMap},
    fmt::Display,
    path::{Path, PathBuf},
    str::FromStr,
    sync::Arc,
};
use sway_core::{fuel_prelude::fuel_tx, language::parsed::TreeType, parse_tree_type, BuildTarget};
use sway_error::handler::Handler;
use sway_utils::{
    constants, find_nested_manifest_dir, find_parent_manifest_dir,
    find_parent_manifest_dir_with_check,
};

use self::build_profile::BuildProfile;

/// The name of a workspace member package.
pub type MemberName = String;
/// A manifest for each workspace member, or just one manifest if working with a single package
pub type MemberManifestFiles = BTreeMap<MemberName, PackageManifestFile>;

pub trait GenericManifestFile {
    fn from_file<P: AsRef<Path>>(path: P) -> Result<Self>
    where
        Self: Sized;
    fn from_dir<P: AsRef<Path>>(dir: P) -> Result<Self>
    where
        Self: Sized;

    /// The path to the `Forc.toml` from which this manifest was loaded.
    ///
    /// This will always be a canonical path.
    fn path(&self) -> &Path;

    /// The path to the directory containing the `Forc.toml` from which this manifest was loaded.
    ///
    /// This will always be a canonical path.
    fn dir(&self) -> &Path {
        self.path()
            .parent()
            .expect("failed to retrieve manifest directory")
    }

    /// Returns the path of the `Forc.lock` file.
    fn lock_path(&self) -> Result<PathBuf>;

    /// Returns a mapping of member member names to package manifest files.
    fn member_manifests(&self) -> Result<MemberManifestFiles>;
}

#[derive(Clone, Debug)]
pub enum ManifestFile {
    Package(Box<PackageManifestFile>),
    Workspace(WorkspaceManifestFile),
}

impl GenericManifestFile for ManifestFile {
    /// Returns a `PackageManifestFile` if the path is within a package directory, otherwise
    /// returns a `WorkspaceManifestFile` if within a workspace directory.
    fn from_dir<P: AsRef<Path>>(path: P) -> Result<Self> {
        let maybe_pkg_manifest = PackageManifestFile::from_dir(path.as_ref());
        let manifest_file = if let Err(e) = maybe_pkg_manifest {
            if e.to_string().contains("missing field `project`") {
                // This might be a workspace manifest file
                let workspace_manifest_file = WorkspaceManifestFile::from_dir(path.as_ref())?;
                ManifestFile::Workspace(workspace_manifest_file)
            } else {
                bail!("{}", e)
            }
        } else if let Ok(pkg_manifest) = maybe_pkg_manifest {
            ManifestFile::Package(Box::new(pkg_manifest))
        } else {
            bail!(
                "Cannot find a valid `Forc.toml` at {}",
                path.as_ref().to_string_lossy()
            )
        };
        Ok(manifest_file)
    }

    /// Returns a `PackageManifestFile` if the path is pointing to package manifest, otherwise
    /// returns a `WorkspaceManifestFile` if it is pointing to a workspace manifest.
    fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let maybe_pkg_manifest = PackageManifestFile::from_file(path.as_ref());
        let manifest_file = if let Err(e) = maybe_pkg_manifest {
            if e.to_string().contains("missing field `project`") {
                // This might be a workspace manifest file
                let workspace_manifest_file = WorkspaceManifestFile::from_file(path.as_ref())?;
                ManifestFile::Workspace(workspace_manifest_file)
            } else {
                bail!("{}", e)
            }
        } else if let Ok(pkg_manifest) = maybe_pkg_manifest {
            ManifestFile::Package(Box::new(pkg_manifest))
        } else {
            bail!(
                "Cannot find a valid `Forc.toml` at {}",
                path.as_ref().to_string_lossy()
            )
        };
        Ok(manifest_file)
    }

    /// The path to the `Forc.toml` from which this manifest was loaded.
    ///
    /// This will always be a canonical path.
    fn path(&self) -> &Path {
        match self {
            ManifestFile::Package(pkg_manifest_file) => pkg_manifest_file.path(),
            ManifestFile::Workspace(workspace_manifest_file) => workspace_manifest_file.path(),
        }
    }

    fn member_manifests(&self) -> Result<MemberManifestFiles> {
        match self {
            ManifestFile::Package(pkg_manifest_file) => pkg_manifest_file.member_manifests(),
            ManifestFile::Workspace(workspace_manifest_file) => {
                workspace_manifest_file.member_manifests()
            }
        }
    }

    /// Returns the path of the lock file for the given ManifestFile
    fn lock_path(&self) -> Result<PathBuf> {
        match self {
            ManifestFile::Package(pkg_manifest) => pkg_manifest.lock_path(),
            ManifestFile::Workspace(workspace_manifest) => workspace_manifest.lock_path(),
        }
    }
}

impl TryInto<PackageManifestFile> for ManifestFile {
    type Error = anyhow::Error;

    fn try_into(self) -> Result<PackageManifestFile> {
        match self {
            ManifestFile::Package(pkg_manifest_file) => Ok(*pkg_manifest_file),
            ManifestFile::Workspace(_) => {
                bail!("Cannot convert workspace manifest to package manifest")
            }
        }
    }
}

impl TryInto<WorkspaceManifestFile> for ManifestFile {
    type Error = anyhow::Error;

    fn try_into(self) -> Result<WorkspaceManifestFile> {
        match self {
            ManifestFile::Package(_) => {
                bail!("Cannot convert package manifest to workspace manifest")
            }
            ManifestFile::Workspace(workspace_manifest_file) => Ok(workspace_manifest_file),
        }
    }
}

type PatchMap = BTreeMap<String, Dependency>;

/// A [PackageManifest] that was deserialized from a file at a particular path.
#[derive(Clone, Debug, PartialEq)]
pub struct PackageManifestFile {
    /// The deserialized `Forc.toml`.
    manifest: PackageManifest,
    /// The path from which the `Forc.toml` file was read.
    path: PathBuf,
}

/// A direct mapping to a `Forc.toml`.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct PackageManifest {
    pub project: Project,
    pub network: Option<Network>,
    pub dependencies: Option<BTreeMap<String, Dependency>>,
    pub patch: Option<BTreeMap<String, PatchMap>>,
    /// A list of [configuration-time constants](https://github.com/FuelLabs/sway/issues/1498).
    pub build_target: Option<BTreeMap<String, BuildTarget>>,
    build_profile: Option<BTreeMap<String, BuildProfile>>,
    pub contract_dependencies: Option<BTreeMap<String, ContractDependency>>,
    pub proxy: Option<Proxy>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub struct Project {
    pub authors: Option<Vec<String>>,
    pub name: String,
    pub organization: Option<String>,
    pub license: String,
    #[serde(default = "default_entry")]
    pub entry: String,
    pub implicit_std: Option<bool>,
    pub forc_version: Option<semver::Version>,
    #[serde(default)]
    pub experimental: HashMap<String, bool>,
    pub metadata: Option<toml::Value>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Network {
    #[serde(default = "default_url")]
    pub url: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct HexSalt(pub fuel_tx::Salt);

impl FromStr for HexSalt {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // cut 0x from start.
        let normalized = s
            .strip_prefix("0x")
            .ok_or_else(|| anyhow::anyhow!("hex salt declaration needs to start with 0x"))?;
        let salt: fuel_tx::Salt =
            fuel_tx::Salt::from_str(normalized).map_err(|e| anyhow::anyhow!("{e}"))?;
        let hex_salt = Self(salt);
        Ok(hex_salt)
    }
}

impl Display for HexSalt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let salt = self.0;
        write!(f, "{}", salt)
    }
}

fn default_hex_salt() -> HexSalt {
    HexSalt(fuel_tx::Salt::default())
}

#[serde_as]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct ContractDependency {
    #[serde(flatten)]
    pub dependency: Dependency,
    #[serde_as(as = "DisplayFromStr")]
    #[serde(default = "default_hex_salt")]
    pub salt: HexSalt,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum Dependency {
    /// In the simple format, only a version is specified, eg.
    /// `package = "<version>"`
    Simple(String),
    /// The simple format is equivalent to a detailed dependency
    /// specifying only a version, eg.
    /// `package = { version = "<version>" }`
    Detailed(DependencyDetails),
}

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct DependencyDetails {
    pub(crate) version: Option<String>,
    pub path: Option<String>,
    pub(crate) git: Option<String>,
    pub(crate) branch: Option<String>,
    pub(crate) tag: Option<String>,
    pub(crate) package: Option<String>,
    pub(crate) rev: Option<String>,
    pub(crate) ipfs: Option<String>,
}

/// Describes the details around proxy contract.
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Proxy {
    pub enabled: bool,
    /// Points to the proxy contract to be updated with the new contract id.
    /// If there is a value for this field, forc will try to update the proxy contract's storage
    /// field such that it points to current contract's deployed instance.
    pub address: Option<String>,
}

impl DependencyDetails {
    /// Checks if dependency details reserved for a specific dependency type used without the main
    /// detail for that type.
    ///
    /// Following dependency details sets are considered to be invalid:
    /// 1. A set of dependency details which declares `branch`, `tag` or `rev` without `git`.
    pub fn validate(&self) -> anyhow::Result<()> {
        let DependencyDetails {
            git,
            branch,
            tag,
            rev,
            ..
        } = self;

        if git.is_none() && (branch.is_some() || tag.is_some() || rev.is_some()) {
            bail!("Details reserved for git sources used without a git field");
        }
        Ok(())
    }
}

impl Dependency {
    /// The string of the `package` field if specified.
    pub fn package(&self) -> Option<&str> {
        match *self {
            Self::Simple(_) => None,
            Self::Detailed(ref det) => det.package.as_deref(),
        }
    }
}

impl PackageManifestFile {
    /// Returns an iterator over patches defined in underlying `PackageManifest` if this is a
    /// standalone package.
    ///
    /// If this package is a member of a workspace, patches are fetched from
    /// the workspace manifest file, ignoring any patch defined in the package
    /// manifest file, even if a patch section is not defined in the namespace.
    fn resolve_patches(&self) -> Result<impl Iterator<Item = (String, PatchMap)>> {
        if let Some(workspace) = self.workspace().ok().flatten() {
            // If workspace is defined, passing a local patch is a warning, but the global patch is used
            if self.patch.is_some() {
                println_warning("Patch for the non root package will be ignored.");
                println_warning(&format!(
                    "Specify patch at the workspace root: {}",
                    workspace.path().to_str().unwrap_or_default()
                ));
            }
            Ok(workspace
                .patch
                .as_ref()
                .cloned()
                .unwrap_or_default()
                .into_iter())
        } else {
            Ok(self.patch.as_ref().cloned().unwrap_or_default().into_iter())
        }
    }

    /// Retrieve the listed patches for the given name from underlying `PackageManifest` if this is
    /// a standalone package.
    ///
    /// If this package is a member of a workspace, patch is fetched from
    /// the workspace manifest file.
    pub fn resolve_patch(&self, patch_name: &str) -> Result<Option<PatchMap>> {
        Ok(self
            .resolve_patches()?
            .find(|(p_name, _)| patch_name == p_name.as_str())
            .map(|(_, patch)| patch))
    }

    /// Given the directory in which the file associated with this `PackageManifest` resides, produce the
    /// path to the entry file as specified in the manifest.
    ///
    /// This will always be a canonical path.
    pub fn entry_path(&self) -> PathBuf {
        self.dir()
            .join(constants::SRC_DIR)
            .join(&self.project.entry)
    }

    /// Produces the string of the entry point file.
    pub fn entry_string(&self) -> Result<Arc<str>> {
        let entry_path = self.entry_path();
        let entry_string = std::fs::read_to_string(entry_path)?;
        Ok(Arc::from(entry_string))
    }

    /// Parse and return the associated project's program type.
    pub fn program_type(&self) -> Result<TreeType> {
        let entry_string = self.entry_string()?;
        let handler = Handler::default();
        let parse_res = parse_tree_type(&handler, entry_string);

        parse_res.map_err(|_| {
            let (errors, _warnings) = handler.consume();
            parsing_failed(&self.project.name, &errors)
        })
    }

    /// Given the current directory and expected program type,
    /// determines whether the correct program type is present.
    pub fn check_program_type(&self, expected_types: &[TreeType]) -> Result<()> {
        let parsed_type = self.program_type()?;
        if !expected_types.contains(&parsed_type) {
            bail!(wrong_program_type(
                &self.project.name,
                expected_types,
                parsed_type
            ));
        } else {
            Ok(())
        }
    }

    /// Access the build profile associated with the given profile name.
    pub fn build_profile(&self, profile_name: &str) -> Option<&BuildProfile> {
        self.build_profile
            .as_ref()
            .and_then(|profiles| profiles.get(profile_name))
    }

    /// Given the name of a `path` dependency, returns the full canonical `Path` to the dependency.
    pub fn dep_path(&self, dep_name: &str) -> Option<PathBuf> {
        let dir = self.dir();
        let details = self.dep_detailed(dep_name)?;
        details.path.as_ref().and_then(|path_str| {
            let path = Path::new(path_str);
            match path.is_absolute() {
                true => Some(path.to_owned()),
                false => dir.join(path).canonicalize().ok(),
            }
        })
    }

    /// Returns the workspace manifest file if this `PackageManifestFile` is one of the members.
    pub fn workspace(&self) -> Result<Option<WorkspaceManifestFile>> {
        let parent_dir = match self.dir().parent() {
            None => return Ok(None),
            Some(dir) => dir,
        };
        let ws_manifest = match WorkspaceManifestFile::from_dir(parent_dir) {
            Ok(manifest) => manifest,
            Err(e) => {
                // Check if the error is missing workspace manifest file. Do not return that error if that
                // is the case as we do not want to return error if this is a single project
                // without a workspace.
                if e.to_string().contains("could not find") {
                    return Ok(None);
                } else {
                    return Err(e);
                }
            }
        };
        if ws_manifest.is_member_path(self.dir())? {
            Ok(Some(ws_manifest))
        } else {
            Ok(None)
        }
    }

    /// Returns an immutable reference to the project name that this manifest file describes.
    pub fn project_name(&self) -> &str {
        &self.project.name
    }

    /// Validate the `PackageManifestFile`.
    ///
    /// This checks:
    /// 1. Validity of the underlying `PackageManifest`.
    /// 2. Existence of the entry file.
    pub fn validate(&self) -> Result<()> {
        self.manifest.validate()?;
        let mut entry_path = self.path.clone();
        entry_path.pop();
        let entry_path = entry_path
            .join(constants::SRC_DIR)
            .join(&self.project.entry);
        if !entry_path.exists() {
            bail!(
                "failed to validate path from entry field {:?} in Forc manifest file.",
                self.project.entry
            )
        }

        // Check for nested packages.
        //
        // `path` is the path to manifest file. To start nested package search we need to start
        // from manifest's directory. So, last part of the path (the filename, "/forc.toml") needs
        // to be removed.
        let mut pkg_dir = self.path.to_path_buf();
        pkg_dir.pop();
        if let Some(nested_package) = find_nested_manifest_dir(&pkg_dir) {
            // remove file name from nested_package_manifest
            bail!("Nested packages are not supported, please consider separating the nested package at {} from the package at {}, or if it makes sense consider creating a workspace.", nested_package.display(), pkg_dir.display())
        }
        Ok(())
    }
}

impl GenericManifestFile for PackageManifestFile {
    /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest`.
    ///
    /// This also `validate`s the manifest, returning an `Err` in the case that invalid names,
    /// fields were used.
    ///
    /// If `core` and `std` are unspecified, `std` will be added to the `dependencies` table
    /// implicitly. In this case, the git tag associated with the version of this crate is used to
    /// specify the pinned commit at which we fetch `std`.
    fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref().canonicalize()?;
        let manifest = PackageManifest::from_file(&path)?;
        let manifest_file = Self { manifest, path };
        manifest_file.validate()?;
        Ok(manifest_file)
    }

    /// Read the manifest from the `Forc.toml` in the directory specified by the given `path` or
    /// any of its parent directories.
    ///
    /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the
    /// file.
    fn from_dir<P: AsRef<Path>>(manifest_dir: P) -> Result<Self> {
        let manifest_dir = manifest_dir.as_ref();
        let dir = find_parent_manifest_dir(manifest_dir)
            .ok_or_else(|| manifest_file_missing(manifest_dir))?;
        let path = dir.join(constants::MANIFEST_FILE_NAME);
        Self::from_file(path)
    }

    fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the location of the lock file for `PackageManifestFile`.
    /// Checks if this PackageManifestFile corresponds to a workspace member and if that is the case
    /// returns the workspace level lock file's location.
    ///
    /// This will always be a canonical path.
    fn lock_path(&self) -> Result<PathBuf> {
        // Check if this package is in a workspace
        let workspace_manifest = self.workspace()?;
        if let Some(workspace_manifest) = workspace_manifest {
            workspace_manifest.lock_path()
        } else {
            Ok(self.dir().to_path_buf().join(constants::LOCK_FILE_NAME))
        }
    }

    fn member_manifests(&self) -> Result<MemberManifestFiles> {
        let mut member_manifest_files = BTreeMap::new();
        // Check if this package is in a workspace, in that case insert all member manifests
        if let Some(workspace_manifest_file) = self.workspace()? {
            for member_manifest in workspace_manifest_file.member_pkg_manifests()? {
                let member_manifest = member_manifest.with_context(|| "Invalid member manifest")?;
                member_manifest_files.insert(member_manifest.project.name.clone(), member_manifest);
            }
        } else {
            let member_name = &self.project.name;
            member_manifest_files.insert(member_name.clone(), self.clone());
        }

        Ok(member_manifest_files)
    }
}

impl PackageManifest {
    pub const DEFAULT_ENTRY_FILE_NAME: &'static str = "main.sw";

    /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest`.
    ///
    /// This also `validate`s the manifest, returning an `Err` in the case that invalid names,
    /// fields were used.
    ///
    /// If `core` and `std` are unspecified, `std` will be added to the `dependencies` table
    /// implicitly. In this case, the git tag associated with the version of this crate is used to
    /// specify the pinned commit at which we fetch `std`.
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        // While creating a `ManifestFile` we need to check if the given path corresponds to a
        // package or a workspace. While doing so, we should be printing the warnings if the given
        // file parses so that we only see warnings for the correct type of manifest.
        let path = path.as_ref();
        let mut warnings = vec![];
        let manifest_str = std::fs::read_to_string(path)
            .map_err(|e| anyhow!("failed to read manifest at {:?}: {}", path, e))?;
        let toml_de = toml::de::Deserializer::new(&manifest_str);
        let mut manifest: Self = serde_ignored::deserialize(toml_de, |path| {
            let warning = format!("unused manifest key: {path}");
            warnings.push(warning);
        })
        .map_err(|e| anyhow!("failed to parse manifest: {}.", e))?;
        for warning in warnings {
            println_warning(&warning);
        }
        manifest.implicitly_include_std_if_missing();
        manifest.implicitly_include_default_build_profiles_if_missing();
        manifest.validate()?;
        Ok(manifest)
    }

    /// Validate the `PackageManifest`.
    ///
    /// This checks:
    /// 1. The project and organization names against a set of reserved/restricted keywords and patterns.
    /// 2. The validity of the details provided. Makes sure that there are no mismatching detail
    ///    declarations (to prevent mixing details specific to certain types).
    pub fn validate(&self) -> Result<()> {
        validate_project_name(&self.project.name)?;
        if let Some(ref org) = self.project.organization {
            validate_name(org, "organization name")?;
        }
        for (_, dependency_details) in self.deps_detailed() {
            dependency_details.validate()?;
        }
        Ok(())
    }

    /// Given a directory to a forc project containing a `Forc.toml`, read the manifest.
    ///
    /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the
    /// file.
    pub fn from_dir<P: AsRef<Path>>(dir: P) -> Result<Self> {
        let dir = dir.as_ref();
        let manifest_dir =
            find_parent_manifest_dir(dir).ok_or_else(|| manifest_file_missing(dir))?;
        let file_path = manifest_dir.join(constants::MANIFEST_FILE_NAME);
        Self::from_file(file_path)
    }

    /// Produce an iterator yielding all listed dependencies.
    pub fn deps(&self) -> impl Iterator<Item = (&String, &Dependency)> {
        self.dependencies
            .as_ref()
            .into_iter()
            .flat_map(|deps| deps.iter())
    }

    /// Produce an iterator yielding all listed build profiles.
    pub fn build_profiles(&self) -> impl Iterator<Item = (&String, &BuildProfile)> {
        self.build_profile
            .as_ref()
            .into_iter()
            .flat_map(|deps| deps.iter())
    }

    /// Produce an iterator yielding all listed contract dependencies
    pub fn contract_deps(&self) -> impl Iterator<Item = (&String, &ContractDependency)> {
        self.contract_dependencies
            .as_ref()
            .into_iter()
            .flat_map(|deps| deps.iter())
    }

    /// Produce an iterator yielding all `Detailed` dependencies.
    pub fn deps_detailed(&self) -> impl Iterator<Item = (&String, &DependencyDetails)> {
        self.deps().filter_map(|(name, dep)| match dep {
            Dependency::Detailed(ref det) => Some((name, det)),
            Dependency::Simple(_) => None,
        })
    }

    /// Produce an iterator yielding all listed patches.
    pub fn patches(&self) -> impl Iterator<Item = (&String, &PatchMap)> {
        self.patch
            .as_ref()
            .into_iter()
            .flat_map(|patches| patches.iter())
    }

    /// Retrieve the listed patches for the given name.
    pub fn patch(&self, patch_name: &str) -> Option<&PatchMap> {
        self.patch
            .as_ref()
            .and_then(|patches| patches.get(patch_name))
    }

    /// Retrieve the proxy table for the package.
    pub fn proxy(&self) -> Option<&Proxy> {
        self.proxy.as_ref()
    }

    /// Check for the `core` and `std` packages under `[dependencies]`. If both are missing, add
    /// `std` implicitly.
    ///
    /// This makes the common case of depending on `std` a lot smoother for most users, while still
    /// allowing for the uncommon case of custom `core`/`std` deps.
    ///
    /// Note: If only `core` is specified, we are unable to implicitly add `std` as we cannot
    /// guarantee that the user's `core` is compatible with the implicit `std`.
    fn implicitly_include_std_if_missing(&mut self) {
        use sway_types::constants::{CORE, STD};
        // Don't include `std` if:
        // - this *is* `core` or `std`.
        // - either `core` or `std` packages are already specified.
        // - a dependency already exists with the name "std".
        if self.project.name == CORE
            || self.project.name == STD
            || self.pkg_dep(CORE).is_some()
            || self.pkg_dep(STD).is_some()
            || self.dep(STD).is_some()
            || !self.project.implicit_std.unwrap_or(true)
        {
            return;
        }
        // Add a `[dependencies]` table if there isn't one.
        let deps = self.dependencies.get_or_insert_with(Default::default);
        // Add the missing dependency.
        let std_dep = implicit_std_dep();
        deps.insert(STD.to_string(), std_dep);
    }

    /// Check for the `debug` and `release` packages under `[build-profile]`. If they are missing add them.
    /// If they are provided, use the provided `debug` or `release` so that they override the default `debug`
    /// and `release`.
    fn implicitly_include_default_build_profiles_if_missing(&mut self) {
        let build_profiles = self.build_profile.get_or_insert_with(Default::default);

        if build_profiles.get(BuildProfile::DEBUG).is_none() {
            build_profiles.insert(BuildProfile::DEBUG.into(), BuildProfile::debug());
        }
        if build_profiles.get(BuildProfile::RELEASE).is_none() {
            build_profiles.insert(BuildProfile::RELEASE.into(), BuildProfile::release());
        }
    }

    /// Retrieve a reference to the dependency with the given name.
    pub fn dep(&self, dep_name: &str) -> Option<&Dependency> {
        self.dependencies
            .as_ref()
            .and_then(|deps| deps.get(dep_name))
    }

    /// Retrieve a reference to the dependency with the given name.
    pub fn dep_detailed(&self, dep_name: &str) -> Option<&DependencyDetails> {
        self.dep(dep_name).and_then(|dep| match dep {
            Dependency::Simple(_) => None,
            Dependency::Detailed(detailed) => Some(detailed),
        })
    }

    /// Retrieve a reference to the contract dependency with the given name.
    pub fn contract_dep(&self, contract_dep_name: &str) -> Option<&ContractDependency> {
        self.contract_dependencies
            .as_ref()
            .and_then(|contract_dependencies| contract_dependencies.get(contract_dep_name))
    }

    /// Retrieve a reference to the contract dependency with the given name.
    pub fn contract_dependency_detailed(
        &self,
        contract_dep_name: &str,
    ) -> Option<&DependencyDetails> {
        self.contract_dep(contract_dep_name)
            .and_then(|contract_dep| match &contract_dep.dependency {
                Dependency::Simple(_) => None,
                Dependency::Detailed(detailed) => Some(detailed),
            })
    }

    /// Finds and returns the name of the dependency associated with a package of the specified
    /// name if there is one.
    ///
    /// Returns `None` in the case that no dependencies associate with a package of the given name.
    fn pkg_dep<'a>(&'a self, pkg_name: &str) -> Option<&'a str> {
        for (dep_name, dep) in self.deps() {
            if dep.package().unwrap_or(dep_name) == pkg_name {
                return Some(dep_name);
            }
        }
        None
    }
}

impl std::ops::Deref for PackageManifestFile {
    type Target = PackageManifest;
    fn deref(&self) -> &Self::Target {
        &self.manifest
    }
}

/// The definition for the implicit `std` dependency.
///
/// This can be configured using environment variables:
/// - use `FORC_IMPLICIT_STD_PATH` for the path for the std-lib;
/// - use `FORC_IMPLICIT_STD_GIT`, `FORC_IMPLICIT_STD_GIT_TAG` and/or `FORC_IMPLICIT_STD_GIT_BRANCH` to configure
///   the git repo of the std-lib.
fn implicit_std_dep() -> Dependency {
    if let Ok(path) = std::env::var("FORC_IMPLICIT_STD_PATH") {
        return Dependency::Detailed(DependencyDetails {
            path: Some(path),
            ..Default::default()
        });
    }

    // Here, we use the `forc-pkg` crate version formatted with the `v` prefix (e.g. "v1.2.3"),
    // or the revision commit hash (e.g. "abcdefg").
    //
    // This git tag or revision is used during `PackageManifest` construction to pin the version of the
    // implicit `std` dependency to the `forc-pkg` version.
    //
    // This is important to ensure that the version of `sway-core` that is baked into `forc-pkg` is
    // compatible with the version of the `std` lib.
    let tag = std::env::var("FORC_IMPLICIT_STD_GIT_TAG")
        .ok()
        .unwrap_or_else(|| format!("v{}", env!("CARGO_PKG_VERSION")));
    const SWAY_GIT_REPO_URL: &str = "https://github.com/fuellabs/sway";

    // only use tag/rev if the branch is None
    let branch = std::env::var("FORC_IMPLICIT_STD_GIT_BRANCH").ok();
    let tag = branch.as_ref().map_or_else(|| Some(tag), |_| None);

    let mut det = DependencyDetails {
        git: std::env::var("FORC_IMPLICIT_STD_GIT")
            .ok()
            .or_else(|| Some(SWAY_GIT_REPO_URL.to_string())),
        tag,
        branch,
        ..Default::default()
    };

    if let Some((_, build_metadata)) = det.tag.as_ref().and_then(|tag| tag.split_once('+')) {
        // Nightlies are in the format v<version>+nightly.<date>.<hash>
        let rev = build_metadata.split('.').last().map(|r| r.to_string());

        // If some revision is available and parsed from the 'nightly' build metadata,
        // we always prefer the revision over the tag.
        det.tag = None;
        det.rev = rev;
    };

    Dependency::Detailed(det)
}

fn default_entry() -> String {
    PackageManifest::DEFAULT_ENTRY_FILE_NAME.to_string()
}

fn default_url() -> String {
    constants::DEFAULT_NODE_URL.into()
}

/// A [WorkspaceManifest] that was deserialized from a file at a particular path.
#[derive(Clone, Debug)]
pub struct WorkspaceManifestFile {
    /// The derserialized `Forc.toml`
    manifest: WorkspaceManifest,
    /// The path from which the `Forc.toml` file was read.
    path: PathBuf,
}

/// A direct mapping to `Forc.toml` if it is a WorkspaceManifest
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct WorkspaceManifest {
    workspace: Workspace,
    patch: Option<BTreeMap<String, PatchMap>>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct Workspace {
    pub members: Vec<PathBuf>,
    pub metadata: Option<toml::Value>,
}

impl WorkspaceManifestFile {
    /// Produce an iterator yielding all listed patches.
    pub fn patches(&self) -> impl Iterator<Item = (&String, &PatchMap)> {
        self.patch
            .as_ref()
            .into_iter()
            .flat_map(|patches| patches.iter())
    }

    /// Returns an iterator over relative paths of workspace members.
    pub fn members(&self) -> impl Iterator<Item = &PathBuf> + '_ {
        self.workspace.members.iter()
    }

    /// Returns an iterator over workspace member root directories.
    ///
    /// This will always return canonical paths.
    pub fn member_paths(&self) -> Result<impl Iterator<Item = PathBuf> + '_> {
        Ok(self
            .workspace
            .members
            .iter()
            .map(|member| self.dir().join(member)))
    }

    /// Returns an iterator over workspace member package manifests.
    pub fn member_pkg_manifests(
        &self,
    ) -> Result<impl Iterator<Item = Result<PackageManifestFile>> + '_> {
        let member_paths = self.member_paths()?;
        let member_pkg_manifests = member_paths.map(PackageManifestFile::from_dir);
        Ok(member_pkg_manifests)
    }

    /// Check if given path corresponds to any workspace member's path
    pub fn is_member_path(&self, path: &Path) -> Result<bool> {
        Ok(self.member_paths()?.any(|member_path| member_path == path))
    }
}

impl GenericManifestFile for WorkspaceManifestFile {
    /// Given a path to a `Forc.toml`, read it and construct a `PackageManifest`
    ///
    /// This also `validate`s the manifest, returning an `Err` in the case that given members are
    /// not present in the manifest dir.
    fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref().canonicalize()?;
        let parent = path
            .parent()
            .ok_or_else(|| anyhow!("Cannot get parent dir of {:?}", path))?;
        let manifest = WorkspaceManifest::from_file(&path)?;
        manifest.validate(parent)?;
        Ok(Self { manifest, path })
    }

    /// Read the manifest from the `Forc.toml` in the directory specified by the given `path` or
    /// any of its parent directories.
    ///
    /// This is short for `PackageManifest::from_file`, but takes care of constructing the path to the
    /// file.
    fn from_dir<P: AsRef<Path>>(manifest_dir: P) -> Result<Self> {
        let manifest_dir = manifest_dir.as_ref();
        let dir = find_parent_manifest_dir_with_check(manifest_dir, |possible_manifest_dir| {
            // Check if the found manifest file is a workspace manifest file or a standalone
            // package manifest file.
            let possible_path = possible_manifest_dir.join(constants::MANIFEST_FILE_NAME);
            // We should not continue to search if the given manifest is a workspace manifest with
            // some issues.
            //
            // If the error is missing field `workspace` (which happens when trying to read a
            // package manifest as a workspace manifest), look into the parent directories for a
            // legitimate workspace manifest. If the error returned is something else this is a
            // workspace manifest with errors, classify this as a workspace manifest but with
            // errors so that the errors will be displayed to the user.
            Self::from_file(possible_path)
                .err()
                .map(|e| !e.to_string().contains("missing field `workspace`"))
                .unwrap_or_else(|| true)
        })
        .ok_or_else(|| manifest_file_missing(manifest_dir))?;
        let path = dir.join(constants::MANIFEST_FILE_NAME);
        Self::from_file(path)
    }

    fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the location of the lock file for `WorkspaceManifestFile`.
    ///
    /// This will always be a canonical path.
    fn lock_path(&self) -> Result<PathBuf> {
        Ok(self.dir().to_path_buf().join(constants::LOCK_FILE_NAME))
    }

    fn member_manifests(&self) -> Result<MemberManifestFiles> {
        let mut member_manifest_files = BTreeMap::new();
        for member_manifest in self.member_pkg_manifests()? {
            let member_manifest = member_manifest.with_context(|| "Invalid member manifest")?;
            member_manifest_files.insert(member_manifest.project.name.clone(), member_manifest);
        }

        Ok(member_manifest_files)
    }
}

impl WorkspaceManifest {
    /// Given a path to a `Forc.toml`, read it and construct a `WorkspaceManifest`.
    pub fn from_file(path: &Path) -> Result<Self> {
        // While creating a `ManifestFile` we need to check if the given path corresponds to a
        // package or a workspace. While doing so, we should be printing the warnings if the given
        // file parses so that we only see warnings for the correct type of manifest.
        let mut warnings = vec![];
        let manifest_str = std::fs::read_to_string(path)
            .map_err(|e| anyhow!("failed to read manifest at {:?}: {}", path, e))?;
        let toml_de = toml::de::Deserializer::new(&manifest_str);
        let manifest: Self = serde_ignored::deserialize(toml_de, |path| {
            let warning = format!("unused manifest key: {path}");
            warnings.push(warning);
        })
        .map_err(|e| anyhow!("failed to parse manifest: {}.", e))?;
        for warning in warnings {
            println_warning(&warning);
        }
        Ok(manifest)
    }

    /// Validate the `WorkspaceManifest`
    ///
    /// This checks if the listed members in the `WorkspaceManifest` are indeed in the given `Forc.toml`'s directory.
    pub fn validate(&self, path: &Path) -> Result<()> {
        let mut pkg_name_to_paths: HashMap<String, Vec<PathBuf>> = HashMap::new();
        for member in &self.workspace.members {
            let member_path = path.join(member).join("Forc.toml");
            if !member_path.exists() {
                bail!(
                    "{:?} is listed as a member of the workspace but {:?} does not exists",
                    &member,
                    member_path
                );
            }
            if Self::from_file(&member_path).is_ok() {
                bail!("Unexpected nested workspace '{}'. Workspaces are currently only allowed in the project root.", member.display());
            };

            let member_manifest_file = PackageManifestFile::from_file(member_path.clone())?;
            let pkg_name = member_manifest_file.manifest.project.name;
            pkg_name_to_paths
                .entry(pkg_name)
                .or_default()
                .push(member_path);
        }

        // Check for duplicate pkg name entries in member manifests of this workspace.
        let duplicate_pkg_lines = pkg_name_to_paths
            .iter()
            .filter_map(|(pkg_name, paths)| {
                if paths.len() > 1 {
                    let duplicate_paths = pkg_name_to_paths
                        .get(pkg_name)
                        .expect("missing duplicate paths");
                    Some(format!("{pkg_name}: {duplicate_paths:#?}"))
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        if !duplicate_pkg_lines.is_empty() {
            let error_message = duplicate_pkg_lines.join("\n");
            bail!(
                "Duplicate package names detected in the workspace:\n\n{}",
                error_message
            );
        }
        Ok(())
    }
}

impl std::ops::Deref for WorkspaceManifestFile {
    type Target = WorkspaceManifest;
    fn deref(&self) -> &Self::Target {
        &self.manifest
    }
}

/// Attempt to find a `Forc.toml` with the given project name within the given directory.
///
/// Returns the path to the package on success, or `None` in the case it could not be found.
pub fn find_within(dir: &Path, pkg_name: &str) -> Option<PathBuf> {
    use sway_types::constants::STD;
    const SWAY_STD_FOLDER: &str = "sway-lib-std";
    walkdir::WalkDir::new(dir)
        .into_iter()
        .filter_map(|entry| {
            entry
                .ok()
                .filter(|entry| entry.path().ends_with(constants::MANIFEST_FILE_NAME))
        })
        .find_map(|entry| {
            let path = entry.path();
            let manifest = PackageManifest::from_file(path).ok()?;
            // If the package is STD, make sure it is coming from correct folder.
            if (manifest.project.name == pkg_name && pkg_name != STD)
                || (manifest.project.name == STD
                    && path
                        .components()
                        .any(|comp| comp.as_os_str() == SWAY_STD_FOLDER))
            {
                Some(path.to_path_buf())
            } else {
                None
            }
        })
}

/// The same as [find_within], but returns the package's project directory.
pub fn find_dir_within(dir: &Path, pkg_name: &str) -> Option<PathBuf> {
    find_within(dir, pkg_name).and_then(|path| path.parent().map(Path::to_path_buf))
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    #[test]
    fn deserialize_contract_dependency() {
        let contract_dep_str = r#"{"path": "../", "salt": "0x1111111111111111111111111111111111111111111111111111111111111111" }"#;

        let contract_dep_expected: ContractDependency =
            serde_json::from_str(contract_dep_str).unwrap();

        let dependency_det = DependencyDetails {
            path: Some("../".to_owned()),
            ..Default::default()
        };
        let dependency = Dependency::Detailed(dependency_det);
        let contract_dep = ContractDependency {
            dependency,
            salt: HexSalt::from_str(
                "0x1111111111111111111111111111111111111111111111111111111111111111",
            )
            .unwrap(),
        };
        assert_eq!(contract_dep, contract_dep_expected)
    }
    #[test]
    fn test_invalid_dependency_details_mixed_together() {
        let dependency_details_path_branch = DependencyDetails {
            version: None,
            path: Some("example_path/".to_string()),
            git: None,
            branch: Some("test_branch".to_string()),
            tag: None,
            package: None,
            rev: None,
            ipfs: None,
        };

        let dependency_details_branch = DependencyDetails {
            path: None,
            ..dependency_details_path_branch.clone()
        };

        let dependency_details_ipfs_branch = DependencyDetails {
            path: None,
            ipfs: Some("QmVxgEbiDDdHpG9AesCpZAqNvHYp1P3tWLFdrpUBWPMBcc".to_string()),
            ..dependency_details_path_branch.clone()
        };

        let dependency_details_path_tag = DependencyDetails {
            version: None,
            path: Some("example_path/".to_string()),
            git: None,
            branch: None,
            tag: Some("v0.1.0".to_string()),
            package: None,
            rev: None,
            ipfs: None,
        };

        let dependency_details_tag = DependencyDetails {
            path: None,
            ..dependency_details_path_tag.clone()
        };

        let dependency_details_ipfs_tag = DependencyDetails {
            path: None,
            ipfs: Some("QmVxgEbiDDdHpG9AesCpZAqNvHYp1P3tWLFdrpUBWPMBcc".to_string()),
            ..dependency_details_path_branch.clone()
        };

        let dependency_details_path_rev = DependencyDetails {
            version: None,
            path: Some("example_path/".to_string()),
            git: None,
            branch: None,
            tag: None,
            package: None,
            ipfs: None,
            rev: Some("9f35b8e".to_string()),
        };

        let dependency_details_rev = DependencyDetails {
            path: None,
            ..dependency_details_path_rev.clone()
        };

        let dependency_details_ipfs_rev = DependencyDetails {
            path: None,
            ipfs: Some("QmVxgEbiDDdHpG9AesCpZAqNvHYp1P3tWLFdrpUBWPMBcc".to_string()),
            ..dependency_details_path_branch.clone()
        };

        let expected_mismatch_error = "Details reserved for git sources used without a git field";
        assert_eq!(
            dependency_details_path_branch
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_ipfs_branch
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_path_tag
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_ipfs_tag
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_path_rev
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_ipfs_rev
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_branch
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_tag
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
        assert_eq!(
            dependency_details_rev
                .validate()
                .err()
                .map(|e| e.to_string()),
            Some(expected_mismatch_error.to_string())
        );
    }

    #[test]
    #[should_panic(expected = "duplicate key `foo` in table `dependencies`")]
    fn test_error_duplicate_deps_definition() {
        PackageManifest::from_dir("./tests/invalid/duplicate_keys").unwrap();
    }

    #[test]
    fn test_error_duplicate_deps_definition_in_workspace() {
        // Load each project inside a workspace and load their patches
        // definition. There should be zero, because the file workspace file has
        // no patches
        //
        // The code also prints a warning to the stdout
        let workspace =
            WorkspaceManifestFile::from_dir("./tests/invalid/patch_workspace_and_package").unwrap();
        let projects: Vec<_> = workspace
            .member_pkg_manifests()
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(projects.len(), 1);
        let patches: Vec<_> = projects[0].resolve_patches().unwrap().collect();
        assert_eq!(patches.len(), 0);

        // Load the same Forc.toml file but outside of a workspace. There should
        // be a single entry in the patch
        let patches: Vec<_> = PackageManifestFile::from_dir("./tests/test_package")
            .unwrap()
            .resolve_patches()
            .unwrap()
            .collect();
        assert_eq!(patches.len(), 1);
    }

    #[test]
    fn test_valid_dependency_details() {
        let dependency_details_path = DependencyDetails {
            version: None,
            path: Some("example_path/".to_string()),
            git: None,
            branch: None,
            tag: None,
            package: None,
            rev: None,
            ipfs: None,
        };

        let git_source_string = "https://github.com/FuelLabs/sway".to_string();
        let dependency_details_git_tag = DependencyDetails {
            version: None,
            path: None,
            git: Some(git_source_string.clone()),
            branch: None,
            tag: Some("v0.1.0".to_string()),
            package: None,
            rev: None,
            ipfs: None,
        };
        let dependency_details_git_branch = DependencyDetails {
            version: None,
            path: None,
            git: Some(git_source_string.clone()),
            branch: Some("test_branch".to_string()),
            tag: None,
            package: None,
            rev: None,
            ipfs: None,
        };
        let dependency_details_git_rev = DependencyDetails {
            version: None,
            path: None,
            git: Some(git_source_string),
            branch: Some("test_branch".to_string()),
            tag: None,
            package: None,
            rev: Some("9f35b8e".to_string()),
            ipfs: None,
        };

        let dependency_details_ipfs = DependencyDetails {
            version: None,
            path: None,
            git: None,
            branch: None,
            tag: None,
            package: None,
            rev: None,
            ipfs: Some("QmVxgEbiDDdHpG9AesCpZAqNvHYp1P3tWLFdrpUBWPMBcc".to_string()),
        };

        assert!(dependency_details_path.validate().is_ok());
        assert!(dependency_details_git_tag.validate().is_ok());
        assert!(dependency_details_git_branch.validate().is_ok());
        assert!(dependency_details_git_rev.validate().is_ok());
        assert!(dependency_details_ipfs.validate().is_ok());
    }

    #[test]
    fn test_project_with_null_metadata() {
        let project = Project {
            authors: Some(vec!["Test Author".to_string()]),
            name: "test-project".to_string(),
            organization: None,
            license: "Apache-2.0".to_string(),
            entry: "main.sw".to_string(),
            implicit_std: None,
            forc_version: None,
            experimental: HashMap::new(),
            metadata: Some(toml::Value::from(toml::value::Table::new())),
        };

        let serialized = toml::to_string(&project).unwrap();
        let deserialized: Project = toml::from_str(&serialized).unwrap();

        assert_eq!(project.name, deserialized.name);
        assert_eq!(project.metadata, deserialized.metadata);
    }

    #[test]
    fn test_project_without_metadata() {
        let project = Project {
            authors: Some(vec!["Test Author".to_string()]),
            name: "test-project".to_string(),
            organization: None,
            license: "Apache-2.0".to_string(),
            entry: "main.sw".to_string(),
            implicit_std: None,
            forc_version: None,
            experimental: HashMap::new(),
            metadata: None,
        };

        let serialized = toml::to_string(&project).unwrap();
        let deserialized: Project = toml::from_str(&serialized).unwrap();

        assert_eq!(project.name, deserialized.name);
        assert_eq!(project.metadata, deserialized.metadata);
        assert_eq!(project.metadata, None);
    }

    #[test]
    fn test_project_metadata_from_toml() {
        let toml_str = r#"
            name = "test-project"
            license = "Apache-2.0"
            entry = "main.sw"
            authors = ["Test Author"]

            [metadata]
            description = "A test project"
            version = "1.0.0"
            homepage = "https://example.com"
            documentation = "https://docs.example.com"
            repository = "https://github.com/example/test-project"
            keywords = ["test", "project"]
            categories = ["test"]
        "#;

        let project: Project = toml::from_str(toml_str).unwrap();
        assert!(project.metadata.is_some());

        let metadata = project.metadata.unwrap();
        let table = metadata.as_table().unwrap();

        assert_eq!(
            table.get("description").unwrap().as_str().unwrap(),
            "A test project"
        );
        assert_eq!(table.get("version").unwrap().as_str().unwrap(), "1.0.0");
        assert_eq!(
            table.get("homepage").unwrap().as_str().unwrap(),
            "https://example.com"
        );

        let keywords = table.get("keywords").unwrap().as_array().unwrap();
        assert_eq!(keywords[0].as_str().unwrap(), "test");
        assert_eq!(keywords[1].as_str().unwrap(), "project");
    }

    #[test]
    fn test_project_with_invalid_metadata() {
        // Test with invalid TOML syntax - unclosed table
        let invalid_toml = r#"
            name = "test-project"
            license = "Apache-2.0"
            entry = "main.sw"
            
            [metadata
            description = "Invalid TOML"
        "#;

        let result: Result<Project, _> = toml::from_str(invalid_toml);
        assert!(result.is_err());

        // Test with invalid TOML syntax - invalid key
        let invalid_toml = r#"
            name = "test-project"
            license = "Apache-2.0"
            entry = "main.sw"
            
            [metadata]
            ] = "Invalid key"
        "#;

        let result: Result<Project, _> = toml::from_str(invalid_toml);
        assert!(result.is_err());

        // Test with duplicate keys
        let invalid_toml = r#"
            name = "test-project"
            license = "Apache-2.0"
            entry = "main.sw"
            
            [metadata]
            nested = { key = "value1" }

            [metadata.nested]
            key = "value2"
        "#;

        let result: Result<Project, _> = toml::from_str(invalid_toml);
        assert!(result.is_err());
        assert!(result
            .err()
            .unwrap()
            .to_string()
            .contains("duplicate key `nested` in table `metadata`"));
    }

    #[test]
    fn test_metadata_roundtrip() {
        let original_toml = r#"
            name = "test-project"
            license = "Apache-2.0"
            entry = "main.sw"
            
            [metadata]
            boolean = true
            integer = 42
            float = 3.12
            string = "value"
            array = [1, 2, 3]
            mixed_array = [1, "two", true]

            [metadata.nested]
            key = "value2"
        "#;

        let project: Project = toml::from_str(original_toml).unwrap();
        let serialized = toml::to_string(&project).unwrap();
        let deserialized: Project = toml::from_str(&serialized).unwrap();

        // Verify that the metadata is preserved
        assert_eq!(project.metadata, deserialized.metadata);

        // Verify all types were preserved
        let table_val = project.metadata.unwrap();
        let table = table_val.as_table().unwrap();
        assert!(table.get("boolean").unwrap().as_bool().unwrap());
        assert_eq!(table.get("integer").unwrap().as_integer().unwrap(), 42);
        assert_eq!(table.get("float").unwrap().as_float().unwrap(), 3.12);
        assert_eq!(table.get("string").unwrap().as_str().unwrap(), "value");
        assert_eq!(table.get("array").unwrap().as_array().unwrap().len(), 3);
        assert!(table.get("nested").unwrap().as_table().is_some());
    }

    #[test]
    fn test_workspace_with_metadata() {
        let toml_str = r#"
            [workspace]
            members = ["package1", "package2"]
            
            [workspace.metadata]
            description = "A test workspace"
            version = "1.0.0"
            authors = ["Test Author"]
            homepage = "https://example.com"
            
            [workspace.metadata.ci]
            workflow = "main"
            timeout = 3600
        "#;

        let manifest: WorkspaceManifest = toml::from_str(toml_str).unwrap();
        assert!(manifest.workspace.metadata.is_some());

        let metadata = manifest.workspace.metadata.unwrap();
        let table = metadata.as_table().unwrap();

        assert_eq!(
            table.get("description").unwrap().as_str().unwrap(),
            "A test workspace"
        );
        assert_eq!(table.get("version").unwrap().as_str().unwrap(), "1.0.0");

        let ci = table.get("ci").unwrap().as_table().unwrap();
        assert_eq!(ci.get("workflow").unwrap().as_str().unwrap(), "main");
        assert_eq!(ci.get("timeout").unwrap().as_integer().unwrap(), 3600);
    }

    #[test]
    fn test_workspace_without_metadata() {
        let toml_str = r#"
            [workspace]
            members = ["package1", "package2"]
        "#;

        let manifest: WorkspaceManifest = toml::from_str(toml_str).unwrap();
        assert!(manifest.workspace.metadata.is_none());
    }

    #[test]
    fn test_workspace_empty_metadata() {
        let toml_str = r#"
            [workspace]
            members = ["package1", "package2"]
            
            [workspace.metadata]
        "#;

        let manifest: WorkspaceManifest = toml::from_str(toml_str).unwrap();
        assert!(manifest.workspace.metadata.is_some());
        let metadata = manifest.workspace.metadata.unwrap();
        assert!(metadata.as_table().unwrap().is_empty());
    }

    #[test]
    fn test_workspace_complex_metadata() {
        let toml_str = r#"
            [workspace]
            members = ["package1", "package2"]
            
            [workspace.metadata]
            numbers = [1, 2, 3]
            strings = ["a", "b", "c"]
            mixed = [1, "two", true]
            
            [workspace.metadata.nested]
            key = "value"
            
            [workspace.metadata.nested.deep]
            another = "value"
        "#;

        let manifest: WorkspaceManifest = toml::from_str(toml_str).unwrap();
        let metadata = manifest.workspace.metadata.unwrap();
        let table = metadata.as_table().unwrap();

        assert!(table.get("numbers").unwrap().as_array().is_some());
        assert!(table.get("strings").unwrap().as_array().is_some());
        assert!(table.get("mixed").unwrap().as_array().is_some());

        let nested = table.get("nested").unwrap().as_table().unwrap();
        assert_eq!(nested.get("key").unwrap().as_str().unwrap(), "value");

        let deep = nested.get("deep").unwrap().as_table().unwrap();
        assert_eq!(deep.get("another").unwrap().as_str().unwrap(), "value");
    }

    #[test]
    fn test_workspace_metadata_roundtrip() {
        let original = WorkspaceManifest {
            workspace: Workspace {
                members: vec![PathBuf::from("package1"), PathBuf::from("package2")],
                metadata: Some(toml::Value::Table({
                    let mut table = toml::value::Table::new();
                    table.insert("key".to_string(), toml::Value::String("value".to_string()));
                    table
                })),
            },
            patch: None,
        };

        let serialized = toml::to_string(&original).unwrap();
        let deserialized: WorkspaceManifest = toml::from_str(&serialized).unwrap();

        assert_eq!(original.workspace.members, deserialized.workspace.members);
        assert_eq!(original.workspace.metadata, deserialized.workspace.metadata);
    }
}