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
//---------------------------------------------------------------------------------------------------- Use
use anyhow::{anyhow,bail,Error};
use directories::ProjectDirs;

use std::path::{Path,PathBuf};
use crate::Dir;

//---------------------------------------------------------------------------------------------------- Common Functions.
#[inline(always)]
// Create the `ProjectDirs` struct from a project name.
pub(crate) fn base(project_name: &str) -> Result<ProjectDirs, Error> {
	match ProjectDirs::from("", "", project_name) {
		Some(p) => Ok(p),
		None    => Err(anyhow!("User directories could not be found")),
	}
}

// Get the absolute OS + Project PATH.
pub(crate) fn get_projectdir(dir: &Dir, project_name: &str) -> Result<PathBuf, Error> {
	let project_dir = base(project_name)?;

	use Dir::*;
	Ok(match &dir {
		Project    => project_dir.project_path(),
		Cache      => project_dir.cache_dir(),
		Config     => project_dir.config_dir(),
		Data       => project_dir.data_dir(),
		DataLocal  => project_dir.data_local_dir(),
		Preference => project_dir.preference_dir(),
	}.to_path_buf())
}

#[inline(always)]
// Some errors don't work with `anyhow` since they don't implement `std::error::Error`
// but they usually do implement `Display`, so use that and rewrap the `Result`.
pub(crate) fn convert_error<T, E: std::fmt::Display + std::fmt::Debug + Send + Sync + 'static>(result: Result<T, E>) -> Result<T, Error> {
	match result {
		Ok(t)  => Ok(t),
		Err(e) => Err(anyhow!(e)),
	}
}

#[inline(always)]
// Assert PATH is safe (absolute).
pub(crate) fn assert_safe_path(path: &Path) -> Result<(), Error> {
	if !path.is_absolute() { bail!("Aborting: dangerous PATH detected") }

	Ok(())
}

#[inline(always)]
pub(crate) fn decompress<R>(reader: R) -> Result<Vec<u8>, Error>
where
	R: std::io::BufRead,
{
	use std::io::prelude::*;
	use flate2::bufread::GzDecoder;

	// Buffer to store decompressed bytes.
	let mut buf = Vec::new();

	// Decode compressed file bytes into buffer.
	GzDecoder::new(reader).read_to_end(&mut buf)?;

	buf.shrink_to_fit();
	Ok(buf)
}

#[inline(always)]
pub(crate) fn compress(bytes: &[u8]) -> Result<Vec<u8>, Error> {
	use std::io::prelude::*;
	use flate2::Compression;
	use flate2::write::GzEncoder;

	// Compress bytes and write.
	let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
	encoder.write_all(bytes)?;
	let buf = encoder.finish()?;

	Ok(buf)
}

#[inline(always)]
// Returns 0 on error.
pub(crate) fn filesize(path: &Path) -> u64 {
	match std::fs::metadata(path) {
		Ok(f)  => f.len(),
		Err(_) => 0,
	}
}

// Create a `File` -> `BufReader`.
macro_rules! file_bufr {
	() => {
		std::io::BufReader::new(
			std::fs::OpenOptions::new()
			.read(true)
			.create(true)
			.open(Self::absolute_path()?)?
		)
	}
}
pub(crate) use file_bufr;

// Create a `File` -> `BufReader` for gzip.
macro_rules! file_bufr_gzip {
	() => {
		std::io::BufReader::new(
			std::fs::OpenOptions::new()
			.read(true)
			.create(true)
			.open(Self::absolute_path_gzip()?)?
		)
	}
}


// Create a `File` -> `BufWriter` from a `Path`.
macro_rules! file_bufw {
	($path:expr) => {
		std::io::BufWriter::new(
			std::fs::OpenOptions::new()
			.write(true)
			.truncate(true)
			.create(true)
			.open(&$path)?
		)
	}
}
pub(crate) use file_bufw;

//---------------------------------------------------------------------------------------------------- impl_file_bytes
// Implements `file_bytes()` for 32/64bit.
macro_rules! impl_file_bytes {
	($bit:literal, $unsigned:tt) => {
		#[inline]
		#[cfg(target_pointer_width = $bit)]
		/// Reads a range of bytes of the associated file of [`Self`].
		///
		/// ## Errors
		/// If `start` is greater than `end`, this returns error.
		fn file_bytes(start: usize, end: usize) -> Result<Vec<u8>, anyhow::Error> {
			use std::io::Read;
			use std::io::{Seek,SeekFrom};

			if start > end {
				bail!("file_bytes(): start > end");
			}

			let mut buf = {
				let len = match start == end {
					true  => 1,
					false => end - start,
				};
				vec![0; len]
			};

			let file = std::fs::File::open(Self::absolute_path()?)?;
			let mut file = std::io::BufReader::new(file);

			file.seek(SeekFrom::Start(start as $unsigned))?;
			file.read_exact(&mut buf)?;

			Ok(buf)
		}

		#[inline]
		#[cfg(target_pointer_width = $bit)]
		/// Same as [`Self::file_bytes`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		fn file_bytes_memmap(start: usize, end: usize) -> Result<Vec<u8>, anyhow::Error> {
			if start > end {
				bail!("file_bytes(): start > end");
			}

			let file = std::fs::File::open(Self::absolute_path()?)?;
			let mmap = unsafe { memmap2::MmapOptions::new().map(&file)? };
			let len = mmap.len();

			if mmap.len() < end {
				bail!("file_bytes(): file length ({len}) less than end ({end})");
			}

			Ok(mmap[start..end].to_vec())
		}
	}
}
pub(crate) use impl_file_bytes;

//---------------------------------------------------------------------------------------------------- impl_io
// Implements I/O methods for all traits.
macro_rules! impl_io {
	($file_ext:literal) => {
		#[inline]
		/// Consume [`Self`] into bytes
		fn into_bytes(self) -> Result<Vec<u8>, anyhow::Error> {
			self.to_bytes()
		}

		#[inline(always)]
		/// Read the file directly as bytes.
		fn read_to_bytes() -> Result<Vec<u8>, anyhow::Error> {
			use std::io::Read;

			let mut bufr = crate::common::file_bufr!();
			let mut vec  = match bufr.get_ref().metadata() {
				Ok(m) => Vec::with_capacity(m.len().try_into().unwrap_or(0)),
				_     => Vec::new(),
			};
			bufr.read_to_end(&mut vec)?;
			Ok(vec)
		}

		/// Read the file directly as bytes, and attempt `gzip` decompression.
		///
		/// This assumes the file is suffixed with `.gz`, for example:
		/// ```text,ignore
		/// config.json    // What `.read_to_bytes()` will look for
		/// config.json.gz // What `.read_to_bytes_gzip()` will look for
		/// ```
		fn read_to_bytes_gzip() -> Result<Vec<u8>, anyhow::Error> {
			// Decode compressed file bytes.
			let buf = common::decompress(crate::common::file_bufr!())?;

			Ok(buf)
		}

		#[inline(always)]
		/// Same as [`Self::exists()`] but checks if the `gzip` file exists.
		///
		/// - [`Self::exists()`] checks for `file.toml`.
		/// - [`Self::exists_gzip()`] checks for `file.toml.gz`.
		fn exists_gzip() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::absolute_path_gzip()?;
			match path.exists() {
				true  => Ok(crate::Metadata::new(crate::common::filesize(&path), path)),
				false => Err(anyhow!("{:?} doesn't exist", path)),
			}
		}

		#[inline(always)]
		/// Read the file as bytes and deserialize into [`Self`].
		fn from_file() -> Result<Self, anyhow::Error> {
			Self::__from_file()
		}

		#[inline(always)]
		/// Read the file as bytes, decompress with `gzip` and deserialize into [`Self`].
		fn from_file_gzip() -> Result<Self, anyhow::Error> {
			Self::from_bytes(&Self::read_to_bytes_gzip()?)
		}

		#[inline(always)]
		/// Same as [`Self::from_file`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn from_file_memmap() -> Result<Self, anyhow::Error> {
			let file = std::fs::File::open(Self::absolute_path()?)?;
			let mmap = unsafe { memmap2::Mmap::map(&file)? };
			Self::from_bytes(&*mmap)
		}

		#[inline(always)]
		/// Same as [`Self::from_file_gzip`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn from_file_gzip_memmap() -> Result<Self, anyhow::Error> {
			let file = std::fs::File::open(Self::absolute_path_gzip()?)?;
			let mmap = unsafe { memmap2::Mmap::map(&file)? };
			Self::from_bytes(&common::decompress(&*mmap)?)
		}

		/// Try saving as a file.
		///
		/// This will return the amount of `bytes` saved and the [`PathBuf`] on success.
		///
 		/// Calling this will automatically create the directories leading up to the file.
		fn save(&self) -> Result<crate::Metadata, anyhow::Error> {
			use std::io::Write;
			let bytes = self.to_writeable_fmt()?;

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;
			path.push(Self::FILE_NAME);

			// Write.
			crate::common::file_bufw!(&path).write_all(&bytes)?;
			Ok(crate::Metadata::new(bytes.len() as u64, path))
		}


		/// Same as [`Self::save`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn save_memmap(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Create bytes.
			let bytes = self.to_bytes()?;
			let len = bytes.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;
			path.push(Self::FILE_NAME);

			// Open file.
			let file = std::fs::OpenOptions::new()
				.read(true)
				.write(true)
				.create(true)
				.open(&path)?;

			// Resize file length.
			#[cfg(target_pointer_width = "64")]
			file.set_len(len as u64)?;
			#[cfg(not(target_pointer_width = "64"))]
			file.set_len(len.try_into()?)?;

			// Write and flush.
			let mut mmap = unsafe { memmap2::MmapMut::map_mut(&file)? };
			mmap.copy_from_slice(&bytes);
			mmap.flush_async()?;

			Ok(crate::Metadata::new(len as u64, path))
		}

		/// Try saving as a compressed file using `gzip`.
		///
		/// On success, this will return:
		/// - The amount of compressed `bytes` saved to disk
		/// - The [`PathBuf`] of the file
		///
		/// This will suffix the file with `.gz`, for example:
		/// ```text,ignore
		/// config.json    // Normal file name with `.save()`
		/// config.json.gz // File name when using `.save_gzip()`
		/// ```
		///
		/// Calling this will automatically create the directories leading up to the file.
		fn save_gzip(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Compress bytes and write.
			let c = common::compress(&self.to_bytes()?)?;
			let c_len = c.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;
			path.push(Self::FILE_NAME_GZIP);

			// Write.
			use std::io::Write;
			crate::common::file_bufw!(&path).write_all(&c)?;

			Ok(crate::Metadata::new(c_len as u64, path))
		}

		/// Same as [`Self::save_gzip`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn save_gzip_memmap(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Compress bytes and write.
			let c = common::compress(&self.to_bytes()?)?;
			let c_len = c.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;
			path.push(Self::FILE_NAME_GZIP);

			// Open file.
			let file = std::fs::OpenOptions::new()
				.read(true)
				.write(true)
				.create(true)
				.open(&path)?;

			// Resize file length.
			#[cfg(target_pointer_width = "64")]
			file.set_len(c_len as u64)?;
			#[cfg(not(target_pointer_width = "64"))]
			file.set_len(c_len.try_into()?)?;

			// Write and flush.
			let mut mmap = unsafe { memmap2::MmapMut::map_mut(&file)? };
			mmap.copy_from_slice(&c);
			mmap.flush_async()?;

			Ok(crate::Metadata::new(c_len as u64, path))
		}

		/// Try saving to a TEMPORARY file first, then renaming it to the associated file.
		///
		/// This lowers the chance for data corruption on interrupt.
		///
		/// The temporary file is removed if the rename fails.
		///
		/// The temporary file name is: `file_name` + `extension` + `.tmp`, for example:
		/// ```text,ignore
		/// config.toml     // <- Real file
		/// config.toml.tmp // <- Temporary version
		/// ```
		/// Already existing `.tmp` files will be overwritten.
		///
		/// This will return the amount of `bytes` saved and the [`PathBuf`] on success.
		///
		/// Calling this will automatically create the directories leading up to the file.
		fn save_atomic(&self) -> Result<crate::Metadata, anyhow::Error> {
			let bytes = self.to_writeable_fmt()?;

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;

			// TMP and normal PATH.
			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_TMP);
			path.push(Self::FILE_NAME);

			// Write to TMP.
			use std::io::Write;
			if let Err(e) = crate::common::file_bufw!(&tmp).write_all(&bytes) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			// Rename TMP to normal.
			if let Err(e) = std::fs::rename(&tmp, &path) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			Ok(crate::Metadata::new(bytes.len() as u64, path))
		}

		/// Combines [`Self::save_gzip()`] and [`Self::save_atomic()`].
		fn save_atomic_gzip(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Compress bytes.
			let c = common::compress(&self.to_bytes()?)?;
			let c_len = c.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;

			// Create TMP and normal.
			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_GZIP_TMP);
			path.push(Self::FILE_NAME_GZIP);

			// Write to TMP.
			use std::io::Write;
			if let Err(e) = crate::common::file_bufw!(&tmp).write_all(&c) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			// Rename TMP to normal.
			if let Err(e) = std::fs::rename(&tmp, &path) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			Ok(crate::Metadata::new(c_len as u64, path))
		}

		/// Same as [`Self::save_atomic()`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn save_atomic_memmap(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Create bytes
			let bytes = self.to_bytes()?;
			let len = bytes.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;

			// TMP and normal PATH.
			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_TMP);
			path.push(Self::FILE_NAME);

			// Open file.
			let file = std::fs::OpenOptions::new()
				.read(true)
				.write(true)
				.create(true)
				.open(&tmp)?;

			// Resize file length.
			#[cfg(target_pointer_width = "64")]
			file.set_len(len as u64)?;
			#[cfg(not(target_pointer_width = "64"))]
			file.set_len(len.try_into()?)?;

			// Write to TMP.
			let mut mmap = unsafe { memmap2::MmapMut::map_mut(&file)? };
			mmap.copy_from_slice(&bytes);

			// Hang on flush.
			if let Err(e) = mmap.flush() {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			// Rename TMP to normal.
			if let Err(e) = std::fs::rename(&tmp, &path) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			Ok(crate::Metadata::new(len as u64, path))
		}

		/// Same as [`Self::save_atomic_gzip()`] but with [`memmap2`](https://docs.rs/memmap2).
		///
		/// ## Safety
		/// You _must_ understand all the invariants that `memmap` comes with.
		///
		/// More details [here](https://docs.rs/memmap2/latest/memmap2/struct.Mmap.html).
		unsafe fn save_atomic_gzip_memmap(&self) -> Result<crate::Metadata, anyhow::Error> {
			// Compress bytes.
			let c = common::compress(&self.to_bytes()?)?;
			let c_len = c.len();

			// Create PATH.
			let mut path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;

			// TMP and normal PATH.
			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_GZIP_TMP);
			path.push(Self::FILE_NAME_GZIP);

			// Open file.
			let file = std::fs::OpenOptions::new()
				.read(true)
				.write(true)
				.create(true)
				.open(&tmp)?;

			// Resize file length.
			#[cfg(target_pointer_width = "64")]
			file.set_len(c_len as u64)?;
			#[cfg(not(target_pointer_width = "64"))]
			file.set_len(c_len.try_into()?)?;

			// Write to TMP.
			let mut mmap = unsafe { memmap2::MmapMut::map_mut(&file)? };
			mmap.copy_from_slice(&c);

			// Hang on flush.
			if let Err(e) = mmap.flush() {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			// Rename TMP to normal.
			if let Err(e) = std::fs::rename(&tmp, &path) {
				std::fs::remove_file(&tmp)?;
				bail!(e);
			}

			Ok(crate::Metadata::new(c_len as u64, path))
		}

		/// Rename the associated file before attempting to delete it.
		///
		/// This lowers the chance for data corruption on interrupt.
		///
		/// On success, this returns:
		/// - The amount of bytes removed
		/// - The [`PathBuf`] that was removed
		///
		/// The temporary file name is: `file_name` + `extension` + `.tmp`, for example:
		/// ```text,ignore
		/// config.toml     // <- Real file
		/// config.toml.tmp // <- Temporary version
		/// ```
		/// Already existing `.tmp` files will be overwritten.
		fn rm_atomic() -> Result<crate::Metadata, anyhow::Error> {
			let mut path = Self::base_path()?;

			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_TMP);
			path.push(Self::FILE_NAME);

			if !path.exists() { return Ok(crate::Metadata::zero(path)) }

			let size = crate::common::filesize(&path);
			std::fs::rename(&path, &tmp)?;
			std::fs::remove_file(&tmp)?;

			Ok(crate::Metadata::new(size, path))
		}

		/// Same as [`Self::rm_atomic()`] but looks for the `.gz` extension.
		fn rm_atomic_gzip() -> Result<crate::Metadata, anyhow::Error> {
			let mut path = Self::base_path()?;

			let mut tmp = path.clone();
			tmp.push(Self::FILE_NAME_GZIP_TMP);
			path.push(Self::FILE_NAME_GZIP);

			if !path.exists() { return Ok(crate::Metadata::zero(path)) }

			let size = crate::common::filesize(&path);
			std::fs::rename(&path, &tmp)?;
			std::fs::remove_file(&tmp)?;

			Ok(crate::Metadata::new(size, path))
		}

		/// Try deleting any leftover `.tmp` files from [`Self::save_atomic()`] or [`Self::save_atomic_gzip()`]
		///
		/// This will return success if the files don't exist or if deleted.
		///
		/// It will return failure if files existed but could not be deleted or if any other error occurs.
		fn rm_tmp() -> Result<(), anyhow::Error> {
			let mut tmp = Self::base_path()?;
			let mut gzip = tmp.clone();

			tmp.push(Self::FILE_NAME_TMP);
			gzip.push(Self::FILE_NAME_GZIP_TMP);

			if !tmp.exists() && !gzip.exists() { return Ok(()) }

			std::fs::remove_file(tmp)?;
			std::fs::remove_file(gzip)?;
			Ok(())
		}

		#[inline(always)]
		/// The absolute PATH of the file associated with this struct WITH the `.gz` extension.
		fn absolute_path_gzip() -> Result<PathBuf, anyhow::Error> {
			let mut base = Self::base_path()?;
			base.push(Self::FILE_NAME_GZIP);

			common::assert_safe_path(&base)?;

			Ok(base)
		}

		#[inline(always)]
		/// Returns the `gzip` file size in bytes and it's [`PathBuf`].
		fn file_size_gzip() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::absolute_path_gzip()?;
			let file = std::fs::File::open(&path)?;
			let size = file.metadata()?.len();

			Ok(crate::Metadata::new(size, path))
		}

		$crate::common::impl_file_bytes!("64", u64);
		$crate::common::impl_file_bytes!("32", u32);
	}
}
pub(crate) use impl_io;

//---------------------------------------------------------------------------------------------------- impl_common
// Implements the CONSTANTS and common PATH methods for all traits.
macro_rules! impl_common {
	($file_ext:literal) => {
		/// Which OS directory it will be saved in.
		const OS_DIRECTORY: $crate::Dir;
		/// What the main project directory will be.
		const PROJECT_DIRECTORY: &'static str;
		/// Optional sub directories in between the project directory and file.
		const SUB_DIRECTORIES: &'static str;
		/// What the raw file name will be (no extension).
		const FILE: &'static str;
		/// What the file extension will be.
		const FILE_EXT: &'static str;
		/// What the full filename + extension will be.
		const FILE_NAME: &'static str;
		/// What the `gzip` variant of the filename will be.
		const FILE_NAME_GZIP: &'static str;
		/// What the `tmp` variant of the filename will be.
		const FILE_NAME_TMP: &'static str;
		/// What the `gzip` + `tmp` variant of the filename will be.
		const FILE_NAME_GZIP_TMP: &'static str;

		#[inline]
		/// Create the directories leading up-to the file.
		///
		/// Returns the [`PathBuf`] created on success.
		///
		/// This is not necessary when using any variant of
		/// `Self::save()` as the directories are created implicitly.
		fn mkdir() -> Result<PathBuf, anyhow::Error> {
			let path = Self::base_path()?;
			std::fs::create_dir_all(&path)?;
			Ok(path)
		}

		#[inline(always)]
		/// Check if the file exists.
		///
		/// On success, this returns:
		/// - The file size in bytes
		/// - The [`PathBuf`] it's located at
		fn exists() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::absolute_path()?;

			match path.exists() {
				true  => Ok(crate::Metadata::new(crate::common::filesize(&path), path)),
				false => Err(anyhow!("{:?} does not exist", path)),
			}
		}

		#[inline(always)]
		/// Returns the file size in bytes and it's [`PathBuf`].
		fn file_size() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::absolute_path()?;
			let file = std::fs::File::open(&path)?;
			let size = file.metadata()?.len();

			Ok(crate::Metadata::new(size, path))
		}

		/// Returns the full base path associated with this struct (PATH leading up to the file).
		///
		/// In contrast to [`Self::sub_dir_parent_path`], this returns all sub-directories,
		/// e.g: `my/sub/dirs` would return `/.../my/sub/dirs`
		///
		/// This includes [`Self::PROJECT_DIRECTORY`], [`Self::SUB_DIRECTORIES`] and excludes [`Self::FILE_NAME`].
		fn base_path() -> Result<PathBuf, anyhow::Error> {
			// Get a `ProjectDir` from our project name.
			let mut base = Self::project_dir_path()?;

			// Append sub directories (if any).
			if Self::SUB_DIRECTORIES.len() != 0 {
				#[cfg(target_os = "windows")]
				Self::SUB_DIRECTORIES.split_terminator(&['/', '\\'][..]).for_each(|dir| base.push(dir));
				#[cfg(target_family = "unix")]
				Self::SUB_DIRECTORIES.split_terminator('/').for_each(|dir| base.push(dir));
			}

			Ok(base)
		}

		#[inline(always)]
		/// Returns the absolute PATH of the file associated with this struct.
		///
		/// This includes [`Self::PROJECT_DIRECTORY`], [`Self::SUB_DIRECTORIES`] and [`Self::FILE_NAME`].
		fn absolute_path() -> Result<PathBuf, anyhow::Error> {
			let mut base = Self::base_path()?;
			base.push(Self::FILE_NAME);

			common::assert_safe_path(&base)?;

			Ok(base)
		}

		/// Try deleting the file.
		///
		/// This will return success if the file doesn't exist or if deleted.
		///
		/// It will return failure if the file existed but could not be deleted or if any other error occurs.
		///
		/// On success, this returns:
		/// - The amount of bytes removed
		/// - The [`PathBuf`] that was removed
		fn rm() -> Result<crate::Metadata, anyhow::Error> {
			let mut path = Self::base_path()?;
			path.push(Self::FILE_NAME);

			if !path.exists() { return Ok(crate::Metadata::zero(path)) }

			let size = crate::common::filesize(&path);
			std::fs::remove_file(&path)?;
			Ok(crate::Metadata::new(size, path))
		}

		#[inline]
		/// Recursively remove this file's sub-directories.
		///
		/// This deletes _all_ directories starting from the parent [`Self::SUB_DIRECTORIES`].
		/// For example:
		/// ```rust,ignore
		/// disk::toml!(State, disk::Dir::Data, "MyProject", "some/sub/dirs", "state");
		/// ```
		/// Everything starting from  `~/.local/share/myproject/some` gets removed recursively.
		///
		/// This is akin to running:
		/// ```ignore
		/// rm -rf ~/.local/share/myproject/some
		/// ```
		///
		/// This function calls [`std::fs::remove_dir_all`], which does _not_ follow symlinks.
		///
		/// On success, this returns:
		/// - The amount of bytes removed
		/// - The [`PathBuf`] that was removed
		fn rm_sub() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::sub_dir_parent_path()?;
			let size = crate::common::filesize(&path);
			std::fs::remove_dir_all(&path)?;
			Ok(crate::Metadata::new(size, path))
		}

		#[inline]
		/// Recursively remove this file's project directory.
		///
		/// This deletes _all_ directories starting from [`Self::PROJECT_DIRECTORY`].
		/// For example:
		/// ```rust,ignore
		/// disk::toml!(State, disk::Dir::Data, "MyProject", "sub_dir", "state");
		/// ```
		/// This project's file would be located at `~/.local/share/myproject`.
		/// This is the `PATH` that gets removed recursively.
		///
		/// This is akin to running:
		/// ```ignore
		/// rm -rf ~/.local/share/myproject
		/// ```
		/// The input to all `disk` macros are sanity checked.
		/// The worst you can do with this function is delete your project's directory.
		///
		/// This function calls [`std::fs::remove_dir_all`], which does _not_ follow symlinks.
		///
		/// On success, this returns:
		/// - The amount of bytes removed
		/// - The [`PathBuf`] that was removed
		fn rm_project() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::project_dir_path()?;
			let size = crate::common::filesize(&path);
			std::fs::remove_dir_all(&path)?;
			Ok(crate::Metadata::new(size, path))
		}

		#[inline(always)]
		/// Returns the file's parent sub-directory size in bytes and it's [`PathBuf`].
		///
		/// This errors if the PATH does not exist.
		///
		/// This starts from the first [`Self::SUB_DIRECTORIES`],
		/// and does not include the [`Self::PROJECT_DIRECTORY`].
		fn sub_dir_size() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::sub_dir_parent_path()?;
			let dir = std::fs::File::open(&path)?;
			let size = dir.metadata()?.len();

			Ok(crate::Metadata::new(size, path))
		}

		#[inline(always)]
		/// Returns the file's project directory size
		/// in bytes ([`Self::PROJECT_DIRECTORY`]) and it's [`PathBuf`].
		///
		/// This errors if the PATH does not exist.
		fn project_dir_size() -> Result<crate::Metadata, anyhow::Error> {
			let path = Self::project_dir_path()?;
			let file = std::fs::File::open(&path)?;
			let size = file.metadata()?.len();

			Ok(crate::Metadata::new(size, path))
		}

		/// Return the full parent project directory associated with this struct.
		///
		/// This is the `PATH` leading up to [`Self::PROJECT_DIRECTORY`].
		fn project_dir_path() -> Result<PathBuf, anyhow::Error> {
			// Get a `ProjectDir` from our project name.
			common::get_projectdir(&Self::OS_DIRECTORY, &Self::PROJECT_DIRECTORY)
		}

		/// Returns the top-level parent sub-directory associated with this struct.
		///
		/// This _only_ returns the top level sub-directory, so if multiple are defined,
		/// only the first will be returned, e.g: `my/sub/dirs` would return `/.../my`
		///
		/// If no sub-directory is defined, this will return the PATH leading up to [`Self::PROJECT_DIRECTORY`].
		fn sub_dir_parent_path() -> Result<PathBuf, anyhow::Error> {
			// Get a `ProjectDir` from our project name.
			let mut base = Self::project_dir_path()?;

			// Append sub directories (if any).
			if Self::SUB_DIRECTORIES.len() != 0 {
				#[cfg(target_os = "windows")]
				if let Some(sub) = Self::SUB_DIRECTORIES.split_terminator(&['/', '\\'][..]).next() {
					base.push(sub);
				}
				#[cfg(target_family = "unix")]
				if let Some(sub) = Self::SUB_DIRECTORIES.split_terminator('/').next() {
					base.push(sub);
				}
			}

			Ok(base)
		}
	}
}
pub(crate) use impl_common;

//---------------------------------------------------------------------------------------------------- impl_string
// Implements common methods on a [String] based trait.
// This automatically implements [impl_common!()].
macro_rules! impl_string {
	($file_ext:literal) => {
		#[inline(always)]
		/// Turn [`Self`] into bytes, maintaining formatting if possible.
		fn to_writeable_fmt(&self) -> Result<Vec<u8>, anyhow::Error> {
			Ok(self.to_string()?.into_bytes())
		}

		#[inline(always)]
		/// Consume [`Self`] into bytes, maintaining formatting if possible.
		fn into_writeable_fmt(self) -> Result<Vec<u8>, anyhow::Error> {
			Ok(self.to_string()?.into_bytes())
		}

		#[inline(always)]
		/// Read the file directly as a [`String`].
		fn read_to_string() -> Result<String, anyhow::Error> {
			Ok(std::fs::read_to_string(Self::absolute_path()?)?)
		}

		common::impl_io!($file_ext);
		common::impl_common!($file_ext);
	};
}
pub(crate) use impl_string;

//---------------------------------------------------------------------------------------------------- impl_binary
// Implements common methods on a binary based trait.
// This automatically implements `impl_common!()`.
macro_rules! impl_binary {
	($file_ext:literal) => {
		#[inline(always)]
		/// Turn [`Self`] into bytes that can be written to disk.
		fn to_writeable_fmt(&self) -> Result<Vec<u8>, anyhow::Error> {
			self.to_bytes()
		}

		#[inline(always)]
		/// Consume [`Self`] into bytes that can be written to disk.
		fn into_writeable_fmt(self) -> Result<Vec<u8>, anyhow::Error> {
			self.to_bytes()
		}

		crate::common::impl_io!($file_ext);
		crate::common::impl_common!($file_ext);
	};
}
pub(crate) use impl_binary;

//---------------------------------------------------------------------------------------------------- Compile-time assertions, sanity checks.
// Assert string does not contain invalid path symbol.
#[doc(hidden)]
#[macro_export]
macro_rules! assert_str_invalid_symbol {
	($symbol:literal, $project:tt, $sub:tt, $file:tt) => {
		$crate::const_assert!(!$crate::contains!($project, $symbol), "disk: 'Project Directory' must not contain '{}'", $symbol);
		$crate::const_assert!(!$crate::contains!($sub, $symbol), "disk: 'Sub Directories' must not contain '{}'", $symbol);
		$crate::const_assert!(!$crate::contains!($file, $symbol), "disk: 'File Name' must not contain '{}'", $symbol);
	}
}

// INVARIANT: Input should be UPPERCASE.
// Assert string is not a reserved file name.
#[doc(hidden)]
#[macro_export]
macro_rules! assert_str_reserved {
	($symbol:literal, $project:tt, $sub:tt, $file:tt) => {
		$crate::const_assert!(!$crate::convert_case!(upper, $project), $symbol, "disk: 'Project Directory' must not be a reserved filename: '{}'", $symbol);
		$crate::const_assert!(!$crate::convert_case!(upper, $sub),     $symbol, "disk: 'Sub Directories' must not be a reserved filename: '{}'", $symbol);
		$crate::const_assert!(!$crate::convert_case!(upper, $file),    $symbol, "disk: 'File Name' must not be a reserved filename: '{}'", $symbol);
		$crate::seq!(N in 0..10 {
			const _: () = {
				if !$crate::contains!($sub, '\\') && $sub.len() > 255 {
					::std::panic!("disk: the single 'Sub Directory' is a reserved filename");
				} else if N < $crate::split!($sub, '\\').len() {
					if $crate::split!($sub, '\\')[N].len() > 255 {
						::std::panic!("disk: one of the 'Sub Directories' is a reserved filename");
					}
				}
			};
			const _: () = {
				if !$crate::contains!($sub, '/') && $sub.len() > 255 {
					::std::panic!("disk: the single 'Sub Directory' is a reserved filename");
				} else if N < $crate::split!($sub, '/').len() {
					if $crate::split!($sub, '/')[N].len() > 255 {
						::std::panic!("disk: one of the 'Sub Directories' is a reserved filename");
					}
				}
			};
		});
	}
}

// Assert string does not contain invalid path symbol.
#[doc(hidden)]
#[macro_export]
macro_rules! assert_str_invalid_symbol_start_end {
	($symbol:literal, $project:tt, $sub:tt, $file:tt) => {
		$crate::const_assert!(!$crate::starts_with!($project, $symbol), "disk: 'Project Directory' must not start with '{}'", $symbol);
		$crate::const_assert!(!$crate::starts_with!($sub,     $symbol), "disk: 'Sub Directories' must not start with '{}'", $symbol);
		$crate::const_assert!(!$crate::starts_with!($file,    $symbol), "disk: 'File Name' must not start with '{}'", $symbol);
		$crate::const_assert!(!$crate::ends_with!($project,   $symbol), "disk: 'Project Directory' must not end with '{}'", $symbol);
		$crate::const_assert!(!$crate::ends_with!($sub,       $symbol), "disk: 'Sub Directories' must not end with '{}'", $symbol);
		$crate::const_assert!(!$crate::ends_with!($file,      $symbol), "disk: 'File Name' must not end with '{}'", $symbol);
		#[cfg(target_os = "windows")]
		$crate::seq!(N in 0..10 {
			const _: () = {
				if N < $crate::split!($sub, '\\').len() {
					if $crate::starts_with!($crate::split!($sub, '\\')[N], $symbol) {
						panic!("disk: one of the 'Sub Directories' starts with an invalid symbol");
					}
				}
			};
		});
		$crate::seq!(N in 0..10 {
			const _: () = {
				if N < $crate::split!($sub, '/').len() {
					if $crate::starts_with!($crate::split!($sub, '/')[N], $symbol) {
						panic!("disk: one of the 'Sub Directories' starts with an invalid symbol");
					}
				}
			};
		});
		#[cfg(target_os = "windows")]
		$crate::seq!(N in 0..10 {
			const _: () = {
				if N < $crate::split!($sub, '\\').len() {
					if $crate::ends_with!($crate::split!($sub, '\\')[N], $symbol) {
						panic!("disk: one of the 'Sub Directories' ends with an invalid symbol");
					}
				}
			};
		});
		$crate::seq!(N in 0..10 {
			const _: () = {
				if N < $crate::split!($sub, '/').len() {
					if $crate::ends_with!($crate::split!($sub, '/')[N], $symbol) {
						panic!("disk: one of the 'Sub Directories' ends with an invalid symbol");
					}
				}
			};
		});
	}
}

// Assert string inputs are valid.
#[doc(hidden)]
#[macro_export]
macro_rules! assert_str {
	($project:tt, $sub:tt, $file:tt) => {
		// Non-Zero length check.
		$crate::const_assert!($project.len() != 0, "disk: 'Project Directory' must not be an empty string");
		$crate::const_assert!($file.len() != 0, "disk: 'File Name' must not be an empty string!");

		// `Project` + `File` Length overflow check.
		$crate::const_assert!($project.len() < 255, "disk: 'Project Directory' must be less than 255 bytes long");
		$crate::const_assert!($file.len() < 255, "disk: 'File Name' must be less than 255 bytes long!");

		// `Project` + `Sub` + `File` length overflow check.
		$crate::const_assert!($project.len() + $sub.len() + $file.len() < 4000, "disk: Directories combined must be less than 4000 bytes long");

		// `Sub` count overflow check.
		$crate::const_assert!($crate::split!($sub, '/').len() < 10, "disk: 'Sub Directories' are limited to 10-depth");

		// Individual `Sub` length overflow check.
		#[cfg(target_os = "windows")]
		$crate::seq!(N in 0..10 {
			const _: () = {
				if !$crate::contains!($sub, '\\') && $sub.len() > 255 {
					::std::panic!("disk: the single 'Sub Directory' is longer than 255 bytes");
				} else if N < $crate::split!($sub, '\\').len() {
					if $crate::split!($sub, '\\')[N].len() > 255 {
						::std::panic!("disk: one of the 'Sub Directories' is longer than 255 bytes");
					}
				}
			};
		});
		$crate::seq!(N in 0..10 {
			const _: () = {
				if !$crate::contains!($sub, '/') && $sub.len() > 255 {
					::std::panic!("disk: the single 'Sub Directory' is longer than 255 bytes");
				} else if N < $crate::split!($sub, '/').len() {
					if $crate::split!($sub, '/')[N].len() > 255 {
						::std::panic!("disk: one of the 'Sub Directories' is longer than 255 bytes");
					}
				}
			};
		});

		// Reserved file name check (windows-only).
//		$crate::assert_str_reserved!("CON",  $project, $sub, $file);

		// Weird symbol checks.
		$crate::const_assert!(!$crate::contains!($project, "/"), "disk: 'Project Directory' must not contain '/'");
		$crate::const_assert!(!$crate::contains!($project, "\\"), "disk: 'Project Directory' must not contain '\\'");
		$crate::const_assert!(!$crate::contains!($file, "/"), "disk: 'File Name' must not contain '/'");
		$crate::const_assert!(!$crate::contains!($file, "\\"), "disk: 'File Name' must not contain '\\'");
		$crate::assert_str_invalid_symbol!("<",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!(">",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!(":",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("\"", $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("\'", $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("|",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("?",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("*",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("^",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("$",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("&",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!("(",  $project, $sub, $file);
		$crate::assert_str_invalid_symbol!(")",  $project, $sub, $file);

		// Assert PATHs do not start/end with invalid symbol.
		$crate::assert_str_invalid_symbol_start_end!(" ", $project, $sub, $file);
		$crate::assert_str_invalid_symbol_start_end!("/", $project, $sub, $file);
		$crate::assert_str_invalid_symbol_start_end!("\\", $project, $sub, $file);
	}
}

//---------------------------------------------------------------------------------------------------- Macros for impl macro.
// Binary files.
macro_rules! impl_macro_binary {
	($trait:ident, $file_ext:literal) => {
		use $crate::Dir;
		paste::item! {
			#[doc = "
Implement the [`" $trait "`] trait

File extension is `" $file_ext "` and is automatically appended.

### Input
These are the inputs you need to provide to implement [`" $trait "`].

| Variable             | Description                             | Related Trait Constant            | Type               | Example       |
|----------------------|-----------------------------------------|-----------------------------------|--------------------|---------------|
| `$data`              | Identifier of the data to implement for |                                   | `struct` or `enum` | `State`
| `$dir`               | Which OS directory to use               | [`" $trait "::OS_DIRECTORY`]      | [`Dir`]            | [`Dir::Data`]
| `$project_directory` | The name of the top project folder      | [`" $trait "::PROJECT_DIRECTORY`] | [`&str`]           | `\"MyProject\"`
| `$sub_directories`   | (Optional) sub-directories before file  | [`" $trait "::SUB_DIRECTORIES`]   | [`&str`]           | `\"some/dirs\"`
| `$file_name`         | The file name to use                    | [`" $trait "::FILE_NAME`]         | [`&str`]           | `\"state\"`
| `$header`            | `24` custom byte header                 | [`" $trait "::HEADER`]            | `[u8; 24]`         | `[1_u8; 24]`
| `$version`           | `1` byte custom version                 | [`" $trait "::VERSION`]           | `u8`               | `5_u8`

### Example
```rust,ignore
use serde::{Serialize,Deserialize};
use disk::*;

const HEADER: [u8; 24] = [1_u8; 24];
const VERSION: u8 = 5;

" $trait:lower "!(State, Dir::Data, \"MyProject\", \"some/dirs\", \"state\", HEADER, VERSION);
#[derive(Serialize,Deserialize)]
struct State {
    string: String,
    number: u32,
}
```

This example would be located at `~/.local/share/myproject/some/dirs/state." $file_ext "`.
"]
			#[macro_export]
			macro_rules! [<$trait:lower>] {
				($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt, $file_name:tt, $header:tt, $version:tt) => {
					$crate::assert_str!($project_directory, $sub_directories, $file_name);

					// SAFETY: The input to this `" $trait "` implementation was verified and sanity-checked via macro.
			 		unsafe impl $crate::$trait for $data {
						const OS_DIRECTORY:       $crate::Dir  = $dir;
						const PROJECT_DIRECTORY:  &'static str = $project_directory;
						const SUB_DIRECTORIES:    &'static str = $sub_directories;
						const FILE:               &'static str = $file_name;
						const FILE_EXT:           &'static str = $file_ext;
						const FILE_NAME:          &'static str = $crate::const_format!("{}.{}", $file_name, $file_ext);
						const FILE_NAME_GZIP:     &'static str = $crate::const_format!("{}.{}.gz", $file_name, $file_ext);
						const FILE_NAME_TMP:      &'static str = $crate::const_format!("{}.{}.tmp", $file_name, $file_ext);
						const FILE_NAME_GZIP_TMP: &'static str = $crate::const_format!("{}.{}.gz.tmp", $file_name, $file_ext);
						const HEADER:             [u8; 24]     = $header;
						const VERSION:            u8           = $version;
					}
				};
			}
			pub(crate) use [<$trait:lower>];
		}
	};
}
pub(crate) use impl_macro_binary;

// Empty (no extension) file.
macro_rules! impl_macro_no_ext {
	($trait:ident) => {
		use $crate::Dir;
		paste::item! {
			#[doc = "
Implement the [`" $trait "`] trait

[`" $trait "`] has no file extension.

### Input
These are the inputs you need to provide to implement [`" $trait "`].

| Variable             | Description                             | Related Trait Constant            | Type               | Example       |
|----------------------|-----------------------------------------|-----------------------------------|--------------------|---------------|
| `$data`              | Identifier of the data to implement for |                                   | `struct` or `enum` | `MyState`
| `$dir`               | Which OS directory to use               | [`" $trait "::OS_DIRECTORY`]      | [`Dir`]            | [`Dir::Data`]
| `$project_directory` | The name of the top project folder      | [`" $trait "::PROJECT_DIRECTORY`] | [`&str`]           | `\"MyProject\"`
| `$sub_directories`   | (Optional) sub-directories before file  | [`" $trait "::SUB_DIRECTORIES`]   | [`&str`]           | `\"some/dirs\"`
| `$file_name`         | The file name to use                    | [`" $trait "::FILE_NAME`]         | [`&str`]           | `\"state\"`

### Example
```rust
use serde::{Serialize,Deserialize};
use disk::*;

" $trait:lower "!(State, Dir::Data, \"MyProject\", \"some/dirs\", \"state\");
#[derive(Serialize,Deserialize)]
struct State {
    string: String,
    number: u32,
}
```

This example would be located at `~/.local/share/myproject/some/dirs/state`.
"]
			#[macro_export]
			macro_rules! [<$trait:lower>] {
				($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt, $file_name:tt) => {
					$crate::assert_str!($project_directory, $sub_directories, $file_name);

					// SAFETY: The input to this `" $trait "` implementation was verified and sanity-checked via macro.
			 		unsafe impl $crate::$trait for $data {
						const OS_DIRECTORY:      $crate::Dir  = $dir;
						const PROJECT_DIRECTORY:  &'static str = $project_directory;
						const SUB_DIRECTORIES:    &'static str = $sub_directories;
						const FILE:               &'static str = $file_name;
						const FILE_EXT:           &'static str = "";
						const FILE_NAME:          &'static str = $file_name;
						const FILE_NAME_GZIP:     &'static str = $crate::const_format!("{}.gz", $file_name);
						const FILE_NAME_TMP:      &'static str = $crate::const_format!("{}.tmp", $file_name);
						const FILE_NAME_GZIP_TMP: &'static str = $crate::const_format!("{}.gz.tmp", $file_name);
					}
				};
			}
			pub(crate) use [<$trait:lower>];
		}
	};
}
pub(crate) use impl_macro_no_ext;

// Regular files.
macro_rules! impl_macro {
	($trait:ident, $file_ext:literal) => {
		use $crate::Dir;
		paste::paste! {
			#[doc = "
Implement the [`" $trait "`] trait

File extension is `" $file_ext "` and is automatically appended.

### Input
These are the inputs you need to provide to implement [`" $trait "`].

| Variable             | Description                             | Related Trait Constant            | Type               | Example       |
|----------------------|-----------------------------------------|-----------------------------------|--------------------|---------------|
| `$data`              | Identifier of the data to implement for |                                   | `struct` or `enum` | `MyState`
| `$dir`               | Which OS directory to use               | [`" $trait "::OS_DIRECTORY`]      | [`Dir`]            | [`Dir::Data`]
| `$project_directory` | The name of the top project folder      | [`" $trait "::PROJECT_DIRECTORY`] | [`&str`]           | `\"MyProject\"`
| `$sub_directories`   | (Optional) sub-directories before file  | [`" $trait "::SUB_DIRECTORIES`]   | [`&str`]           | `\"some/dirs\"`
| `$file_name`         | The file name to use                    | [`" $trait "::FILE_NAME`]         | [`&str`]           | `\"state\"`

### Example
```rust
use serde::{Serialize,Deserialize};
use disk::*;

" $trait:lower "!(State, Dir::Data, \"MyProject\", \"some/dirs\", \"state\");
#[derive(Serialize,Deserialize)]
struct State {
    string: String,
    number: u32,
}
```

This example would be located at `~/.local/share/myproject/some/dirs/state." $file_ext "`.
"]
			#[macro_export]
			macro_rules! [<$trait:lower>] {
				($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt, $file_name:tt) => {
					$crate::assert_str!($project_directory, $sub_directories, $file_name);

					// SAFETY: The input to this `" $trait "` implementation was verified and sanity-checked via macro.
			 		unsafe impl $crate::$trait for $data {
						const OS_DIRECTORY:       $crate::Dir  = $dir;
						const PROJECT_DIRECTORY:  &'static str = $project_directory;
						const SUB_DIRECTORIES:    &'static str = $sub_directories;
						const FILE:               &'static str = $file_name;
						const FILE_EXT:           &'static str = $file_ext;
						const FILE_NAME:          &'static str = $crate::const_format!("{}.{}", $file_name, $file_ext);
						const FILE_NAME_GZIP:     &'static str = $crate::const_format!("{}.{}.gz", $file_name, $file_ext);
						const FILE_NAME_TMP:      &'static str = $crate::const_format!("{}.{}.tmp", $file_name, $file_ext);
						const FILE_NAME_GZIP_TMP: &'static str = $crate::const_format!("{}.{}.gz.tmp", $file_name, $file_ext);
					}
				};
			}
			pub(crate) use [<$trait:lower>];
		}

	};
}
pub(crate) use impl_macro;

//macro_rules! impl_macro_outer {
//	($trait:ident, $file_ext:literal) => {
//		paste::paste! {
//			// No `Dir`.
//			($data:ty, $project_directory:tt, $sub_directories:tt, $file_name:tt) => {
//				$crate::[<$trait:lower>]!($data, $crate::Dir::Data, $project_directory, $sub_directories, $file_name);
//			};
//			// No `file_name`.
//			($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt) => {
//				$crate::[<$trait:lower>]!($data, $dir, $project_directory, $sub_directories, $crate::convert_case!(lower_camel, $data));
//			};
//			// No `sub_dirs`.
//			($data:ty, $project_directory:tt, $file_name:tt) => {
//				$crate::[<$trait:lower>]!($data, $dir, $project_directory, "", $file_name);
//			};
//			// No `Dir` + `sub_dirs`.
//			($data:ty, $project_directory:tt, $file_name:tt) => {
//				$crate::[<$trait:lower>]!($data, $crate::Dir::Data, $project_directory, "", $file_name);
//			};
//			// No `Dir` + `file_name`.
//			($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt) => {
//				$crate::[<$trait:lower>]!($data, $crate::Dir::Data, $project_directory, $sub_directories, $crate::convert_case!(lower_camel, $data));
//			};
//			// No `sub_dirs` + `file_name`.
//			($data:ty, $project_directory:tt, $sub_directories:tt) => {
//				$crate::[<$trait:lower>]!($data, $dir, $project_directory, "", $crate::convert_case!(lower_camel, $data));
//			};
//			// No `Dir` + `sub_dirs` + `file_name`.
//			($data:ty, $project_directory:tt, $sub_directories:tt) => {
//				$crate::[<$trait:lower>]!($data, $crate::Dir::Data, $project_directory, "", $crate::convert_case!(lower_camel, $data));
//			};
//			($data:ty, $dir:expr, $project_directory:tt, $sub_directories:tt, $file_name:tt) => {
//				$crate::impl_macro!($data, $dir, $project_directory, $sub_directories, $file_name);
//			};
//		}
//	}
//}
//pub(crate) use impl_macro_outer;
//---------------------------------------------------------------------------------------------------- TESTS
//#[cfg(test)]
//mod test {
//  #[test]
//  fn _() {
//  }
//}