ro-crate-rs 0.4.10

Library for creating, modifying and reading RO-Crates
Documentation
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
//! Module for writing RoCrate structures to file.
//!
//! Allows basic ro-crate-metadata.json file creation, as well as archiving
//! via zip.

use crate::ro_crate::read::read_crate;
use crate::ro_crate::rocrate::RoCrate;
use dirs;
use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use url::Url;
use walkdir::WalkDir;
use zip::{write::FileOptions, ZipWriter};

/// Serializes and writes an RO-Crate object to a JSON file.
///
/// This function serializes the given `RoCrate` object into a pretty-printed JSON format and writes it
/// to a file with the specified `name`. The function uses basic error handling, printing error messages
/// to standard error without returning or propagating them, which is noted as an area for future improvement.
///
///
/// # Arguments
/// * `rocrate` - A reference to the `RoCrate` object to serialize.
/// * `name` - The name of the file to which the serialized JSON should be written.
///
/// # Notes
/// Current error handling within this function is minimal, relying on printing to stderr. It is recommended
/// to update this function to return a `Result` type in future revisions for better error handling and integration
/// with calling code.
pub fn write_crate(rocrate: &RoCrate, name: String) {
    match serde_json::to_string_pretty(&rocrate) {
        Ok(json_ld) => match File::create(name) {
            Ok(mut file) => {
                if writeln!(file, "{}", json_ld).is_err() {
                    eprintln!("Failed to write to the file.");
                }
            }
            Err(e) => eprintln!("Failed to create file: {}", e),
        },
        Err(e) => eprintln!("Serialization failed: {}", e),
    }
}

/// Serializes an RO-Crate object and writes it directly to a zip file.
///
/// This method allows for a modified RO-Crate to be efficiently serialized and saved into a zip archive
/// without overwriting the original data. It preserves file paths that are
/// relative or absolute in the original crate, whilst mapping the new relatives of the zip file.
/// The function also supports the potential remapping of all data entity IDs within the crate.
///
/// # Arguments
/// * `rocrate` - A reference to the `RoCrate` object to serialize and save.
/// * `name` - The name under which the serialized crate will be stored in the zip file.
/// * `zip` - A mutable reference to the `ZipWriter` used for writing to the zip file.
/// * `options` - ZipFile options to use when creating the new file in the zip archive.
///
/// # Returns
/// A `Result<(), ZipError>` indicating the success or failure of the operation.
fn write_crate_to_zip(
    rocrate: &RoCrate,
    name: String,
    mut zip_data: RoCrateZip,
) -> Result<(), ZipError> {
    // Attempt to serialize the RoCrate object to a pretty JSON string
    let json_ld = serde_json::to_string_pretty(&rocrate)
        .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

    // Start a new file in the zip archive with the given name and options
    zip_data
        .zip
        .start_file(name, zip_data.options)
        .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

    // Write the serialized JSON data to the file in the zip archive
    zip_data
        .zip
        .write_all(json_ld.as_bytes())
        .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

    zip_data
        .zip
        .finish()
        .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

    // If everything succeeded, return Ok(())
    Ok(())
}

/// Writes the contents of an RO-Crate directory to a zip file.
///
/// This function compresses an entire RO-Crate directory, including all files within the directory structure,
/// into a single zip archive. It's designed to include every file present, without checking their relevance
/// to the crate's metadata, based on the principle that all files in the directory are part of the research
/// data or experiment. If external is true, it will grab and copy external data files
/// to a new `external` folder within the zip. This can increase storage costs, but allows
/// exhaustive capture of data state.
///
/// # Arguments
/// * `crate_path` - The path to the RO-Crate file within crate to zip.
/// * `external` - A boolean flag indicating whether to apply special handling for external resources.
///
/// # Returns
/// A `Result<(), ZipError>` reflecting the success or failure of the operation.
///
/// # Notes
/// The function currently zips everything in the given directory, without analyzing the crate's metadata
/// to selectively include files. This approach ensures no potentially relevant data is omitted but may include
/// unnecessary files. Future versions might consider more selective zipping based on the crate's actual contents.
///
/// # Examples
/// ```
/// let crate_path = Path::new("/path/to/ro-crate-directory/ro-crate-metadata.json");
/// zip_crate(crate_path, false)?;
/// ```
pub fn zip_crate(
    crate_path: &Path,
    external: bool,
    validation_level: i8,
    flatten: bool,
    unique: bool,
) -> Result<(), ZipError> {
    // After prepping create the initial zip file
    let mut zip_paths = construct_paths(crate_path).unwrap();

    // Opens target crate ready for update
    let mut rocrate = read_crate(&zip_paths.absolute_path, validation_level).unwrap();

    // Attach unique identifier if not already present
    rocrate.context.add_urn_uuid();
    println!("{:?}", zip_paths);
    if unique {
        let base_id = rocrate.context.get_specific_context("@base").unwrap();

        let stripped_id = format!("{}.zip", base_id.strip_prefix("urn:uuid:").unwrap());
        zip_paths.zip_file_name = zip_paths.root_path.join(stripped_id);
    }
    println!("{:?}", zip_paths);
    let mut zip_data = build_zip(&zip_paths).unwrap();

    let _ = directory_walk(&mut rocrate, &zip_paths, &mut zip_data, flatten);

    if external {
        zip_data = zip_crate_external(&mut rocrate, zip_data, &zip_paths)?
    }

    let _ = write_crate_to_zip(&rocrate, "ro-crate-metadata.json".to_string(), zip_data);

    Ok(())
}
#[derive(Debug)]
pub struct RoCrateZipPaths {
    absolute_path: PathBuf,
    root_path: PathBuf,
    zip_file_name: PathBuf,
}

fn construct_paths(crate_path: &Path) -> Result<RoCrateZipPaths, Box<dyn std::error::Error>> {
    // TODO: add multile options for walking/compression e.g follow symbolic links etc.
    let absolute_path = get_absolute_path(crate_path).unwrap();
    let root_path = absolute_path.parent().unwrap().to_path_buf();

    let zip_file_base_name = root_path
        .file_name()
        .ok_or(ZipError::FileNameNotFound)?
        .to_str()
        .ok_or(ZipError::FileNameConversionFailed)?;

    let zip_file_name = root_path.join(format!("{}.zip", zip_file_base_name));

    Ok(RoCrateZipPaths {
        absolute_path,
        root_path,
        zip_file_name,
    })
}

fn build_zip(path_information: &RoCrateZipPaths) -> Result<RoCrateZip, Box<dyn std::error::Error>> {
    let file = File::create(&path_information.zip_file_name).map_err(ZipError::IoError)?;
    let zip = ZipWriter::new(file);

    // Can change this to deflated for standard compression
    let options = FileOptions::default().compression_method(zip::CompressionMethod::Deflated);

    Ok(RoCrateZip { zip, options })
}

pub struct RoCrateZip {
    zip: ZipWriter<File>,
    options: FileOptions,
}

/// Sole focus must be on present data
/// As defined by the ro-crate-metadata file, it looks at the root and then
/// every file within it belongs to the crate. Whilst not everything is
/// described in the ro-crate-metadata itself as per spec, it absolutely should
/// get everything that is within the crate
fn directory_walk(
    rocrate: &mut RoCrate,
    zip_paths: &RoCrateZipPaths,
    zip_data: &mut RoCrateZip,
    flatten: bool,
) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
    let mut data_vec: Vec<PathBuf> = Vec::new();
    let contained = get_noncontained_data_entites(rocrate, zip_paths, true);

    for entry in WalkDir::new(&zip_paths.root_path)
        .min_depth(0)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_file())
    // Consider only files, not directories
    {
        let path = entry.path();
        let file_name: String;

        if path == zip_paths.zip_file_name {
            continue;
        }

        if path == zip_paths.absolute_path {
            continue;
        }

        if flatten {
            file_name = path
                .file_name()
                .ok_or(ZipError::FileNameNotFound)?
                .to_str()
                .ok_or(ZipError::FileNameConversionFailed)?
                .to_string();
        } else {
            file_name = path
                .strip_prefix(&zip_paths.root_path)
                .map_err(ZipError::PathError)?
                .to_str()
                .ok_or(ZipError::FileNameConversionFailed)?
                .to_string();
        }

        let mut file = fs::File::open(path).map_err(ZipError::IoError)?;

        zip_data
            .zip
            .start_file(&file_name, zip_data.options)
            .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

        // Once copy the absolute path and relative path needs to be checked
        let abs_path = get_absolute_path(path).unwrap();
        if abs_path.is_file() {
            data_vec.push(abs_path.clone());
        };

        let copy_result = io::copy(&mut file, &mut zip_data.zip).map_err(ZipError::IoError);

        match copy_result {
            Ok(_) => {
                for (key, value) in &contained {
                    if abs_path == value.clone() {
                        rocrate.update_id_recursive(key, &file_name)
                    }
                }
            }
            Err(_e) => println!("problem"),
        }
    }
    println!("0 | Rocrate: {:?}", rocrate);
    Ok(data_vec)
}

#[derive(Debug)]
pub enum ZipError {
    EmptyDirectoryVector,
    FileNameNotFound,
    FileNameConversionFailed,
    PathError(std::path::StripPrefixError),
    ZipOperationError(String),
    IoError(io::Error),
}

impl fmt::Display for ZipError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ZipError::EmptyDirectoryVector => write!(f, "Directory vector is empty"),
            ZipError::FileNameNotFound => write!(f, "File name not found"),
            ZipError::FileNameConversionFailed => write!(f, "Failed to convert file name"),
            ZipError::ZipOperationError(ref msg) => write!(f, "Zip operation Error: {}", msg),
            ZipError::PathError(ref err) => write!(f, "Path error: {}", err),
            ZipError::IoError(ref err) => write!(f, "IO error: {}", err),
        }
    }
}

/// Implements the standard Error trait for ZipError.
///
/// This allows `ZipError` to integrate with Rust's error handling ecosystem, enabling it to be
/// returned and handled in contexts where a standard error type is expected.
impl std::error::Error for ZipError {}

/// Converts an `io::Error` into a `ZipError`.
///
/// This is particularly useful when dealing with file I/O operations that may fail,
/// allowing these errors to be seamlessly converted and handled as `ZipError`s.
impl From<io::Error> for ZipError {
    fn from(err: io::Error) -> ZipError {
        ZipError::IoError(err)
    }
}

/// Converts a `std::path::StripPrefixError` into a `ZipError`.
///
/// This conversion is necessary when manipulating file paths, especially when needing
/// to work with relative paths and encountering errors stripping prefixes from them.
impl From<std::path::StripPrefixError> for ZipError {
    fn from(err: std::path::StripPrefixError) -> ZipError {
        ZipError::PathError(err)
    }
}

/// Packages an RO-Crate and its external files into a zip archive, updating IDs as necessary.
///
/// This function is designed for RO-Crates that reference external files. It packages the crate
/// and any external files into a single zip archive, ensuring that all data entities, whether
/// internal or external to the crate directory, are included. Additionally, it updates the IDs
/// of packaged entities to reflect their new paths within the archive.
///
/// # Arguments
/// * `rocrate` - A mutable reference to the `RoCrate` object being packaged.
/// * `crate_path` - The filesystem path to the directory containing the RO-Crate's metadata and data entities.
/// * `zip` - A `ZipWriter<File>` for writing to the zip archive.
/// * `options` - `FileOptions` determining how files are added to the archive (e.g., compression level).
///
/// # Returns
/// Returns a `Result` containing the updated `ZipWriter<File>` on success, or a `ZipError` on failure,
/// encapsulating any errors that occurred during the operation.
pub fn zip_crate_external(
    rocrate: &mut RoCrate,
    mut zip_data: RoCrateZip,
    crate_path: &RoCrateZipPaths,
) -> Result<RoCrateZip, ZipError> {
    // This parses all the IDs and generates a list of paths that are not
    // contained within the crate itself.
    let noncontained = get_noncontained_data_entites(rocrate, crate_path, false);

    // if noncontained is not empty, means data entities are external
    // therefore we need to package them
    if !noncontained.is_empty() {
        for (id, path) in noncontained {
            // norels = path to file, then we use external path to get folder then add basename
            let file_name = path
                .file_name()
                .ok_or(ZipError::FileNameNotFound)?
                .to_str()
                .ok_or(ZipError::FileNameConversionFailed)?;
            let zip_entry_name = format!("external/{}", file_name);

            let mut file = fs::File::open(&path).map_err(ZipError::IoError)?;

            zip_data
                .zip
                .start_file(&zip_entry_name, zip_data.options)
                .map_err(|e| ZipError::ZipOperationError(e.to_string()))?;

            let copy_result = io::copy(&mut file, &mut zip_data.zip).map_err(ZipError::IoError);
            match copy_result {
                Ok(_) => {
                    rocrate.update_id_recursive(&id, &zip_entry_name);
                }
                Err(e) => return Err(e),
            }
        }
    }

    Ok(zip_data)
}

/// Gets all the described data entities of a crate and filters for
/// files that are not in the crate structure
fn get_noncontained_data_entites(
    rocrate: &mut RoCrate,
    crate_path: &RoCrateZipPaths,
    inverse: bool,
) -> HashMap<String, PathBuf> {
    // Get all IDs for the target crate
    // Assumed that all data entities are discrete objects, and that not file
    // has been referenced without being described
    let mut ids = rocrate.get_all_ids();

    // Pop all non-urls and remove root/ metadata descriptor
    ids.retain(|id| is_not_url(id));

    get_noncontained_paths(ids, &crate_path.root_path, inverse)
}

/// Identifies file paths that are not relative to the given RO-Crate directory.
///
/// When preparing an RO-Crate for zipping, it's important to include all related files, even those
/// not stored within the crate's directory. This function helps identify such external files.
///
/// # Arguments
/// * `ids` - A vector of strings representing the IDs (paths) to check.
/// * `crate_dir` - The base directory of the RO-Crate.
///
fn get_noncontained_paths(
    ids: Vec<&String>,
    crate_dir: &Path,
    inverse: bool,
) -> HashMap<String, PathBuf> {
    let mut nonrels: HashMap<String, PathBuf> = HashMap::new();

    // Get the absolute path of the crate directory
    let rocrate_path = get_absolute_path(crate_dir).unwrap();
    println!("crate path {:?} and target id {:?}", rocrate_path, ids);

    // Iterate over all the ids, check if the paths are relative to the crate.
    // EVERYTHING NEEDS TO BE WITHIN THE CRATE
    for id in ids.iter() {
        // Skip IDs that are fragment references (i.e., starting with '#')
        if id.starts_with('#') {
            continue;
        }

        // Resolve the absolute path of the current ID
        if let Some(path) = get_absolute_path(Path::new(id)) {
            // Check if the path exists
            if path.exists() {
                println!("Absolute path: {:?}", path);
                // Check if the path is outside the base crate directory
                if is_outside_base_folder(&rocrate_path, &path) && !inverse {
                    nonrels.insert(id.to_string(), path);
                } else if inverse {
                    nonrels.insert(id.to_string(), path);
                }
            }
        } else {
            println!("ID: {:?}", id);
            let path = match Path::new(id).canonicalize() {
                Ok(resolved) => Ok(resolved),
                Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(resolve_tilde_path(id)),
                Err(e) => Err(continue),
            };
            println!("Pre Resolved path: {:?}", path);
            let resolved_path = rocrate_path.join(path.unwrap()).canonicalize();
            println!("Resolved path: {:?}", resolved_path);
            match resolved_path {
                Ok(abs_path) => {
                    println!("Can confirm: {:?}", abs_path);
                    if abs_path.exists() {
                        println!("Exists: {:?}", abs_path);
                        if is_outside_base_folder(&rocrate_path, &abs_path) && !inverse {
                            nonrels.insert(id.to_string(), abs_path);
                        } else if inverse {
                            nonrels.insert(id.to_string(), abs_path);
                        }
                    } else {
                        //println!("8 | Failed to resolve ID: {:?}", id);
                    }
                }
                Err(_e) => {} //println!("{}", e),
            }
        }
    }

    nonrels
}

fn resolve_tilde_path(path: &str) -> PathBuf {
    if let Some(home_dir) = dirs::home_dir() {
        if path.starts_with("~") {
            return home_dir.join(path.strip_prefix("~/").unwrap_or(""));
        }
    }
    Path::new(path).to_path_buf()
}
/// Converts a relative path to an absolute one, if possible.
///
/// This utility function is useful for obtaining the absolute path representation of a file or directory.
///
/// # Arguments
/// * `relative_path` - The path to be converted to its absolute form.
///
/// # Returns
/// An `Option<PathBuf>` containing the absolute path, if the conversion was successful; otherwise, `None`.
fn get_absolute_path(relative_path: &Path) -> Option<PathBuf> {
    match fs::canonicalize(relative_path) {
        Ok(path) => Some(path),
        Err(_e) => None,
    }
}
/// Determines whether a given string is not a URL.
///
/// This function checks if the provided string represents a file path rather than a URL. It's particularly
/// useful when filtering a list of identifiers to distinguish between web resources and local files.
///
/// # Arguments
/// * `path` - The string to check.
///
/// # Returns
/// `true` if the string is likely a file path; otherwise, `false`.
///
/// # Examples
/// ```
/// assert!(is_not_url("/path/to/file"));
/// assert!(!is_not_url("http://example.com"));
/// ```
pub fn is_not_url(path: &str) -> bool {
    // Check if the path is likely a Windows extended-length path
    let is_extended_windows_path = path.starts_with(r"\\?\");

    // Check if the path is likely a normal file path
    let is_normal_file_path = path.starts_with(r"\\") // UNC path
        || path.chars().next().map(|c| c.is_alphabetic() && path.chars().nth(1) == Some(':')).unwrap_or(false) // Drive letter, e.g., C:\
        || path.starts_with('/') // Unix-style path
        || path.starts_with('.') // Relative path
        || path.starts_with("file:");

    // If it looks like a file path, return true early
    if path.contains("ro-crate-metadata.json") || path == "./" {
        return false;
    }

    if is_extended_windows_path || is_normal_file_path {
        return true;
    }

    Url::parse(path).is_err()
}

/// Checks if a given file path lies outside of a specified base folder.
///
/// This function is critical in identifying external resources that need special handling when
/// preparing an RO-Crate for packaging or distribution.
///
/// # Arguments
/// * `base_folder` - The base directory against which to compare.
/// * `file_path` - The path of the file to check.
///
/// # Returns
/// `true` if the file is outside the base folder; otherwise, `false`.
///
/// # Examples
/// ```
/// let base_folder = Path::new("/path/to/base");
/// let file_path = Path::new("/path/to/base/subdir/file");
/// assert!(!is_outside_base_folder(base_folder, file_path));
/// ```
fn is_outside_base_folder(base_folder: &Path, file_path: &Path) -> bool {
    // Compare the given file path with the base folder path
    println!("Base folder: {:?} | file path {:?}", base_folder, file_path);
    !file_path.starts_with(base_folder)
}

#[cfg(test)]
mod write_crate_tests {
    use super::*;
    use crate::ro_crate::read::read_crate;
    use std::collections::HashMap;
    use std::env;
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;

    fn fixture_path(relative_path: &str) -> PathBuf {
        Path::new("tests/fixtures").join(relative_path)
    }

    #[test]
    fn test_write_crate_success() {
        let path = fixture_path("_ro-crate-metadata-minimal.json");
        let rocrate = read_crate(&path, 0).unwrap();
        let file_name = "test_rocrate_output.json";

        // Call the function to write the crate to a file
        write_crate(&rocrate, file_name.to_string());

        // Check if the file is created
        assert!(Path::new(file_name).exists());

        // Read the file content and verify if it matches the expected JSON
        let file_content = fs::read_to_string(file_name).expect("Failed to read file");
        let expected_json = serde_json::to_string_pretty(&rocrate).expect("Failed to serialize");
        println!("{}", file_content);
        assert_eq!(file_content.trim_end(), expected_json);

        // Clean up: Remove the created file after the test
        fs::remove_file(file_name).expect("Failed to remove test file");
    }

    #[test]
    fn test_zip_crate_basic() {
        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json");

        let zipped = zip_crate(&path, false, 0, false, false);
        println!("{:?}", zipped);
    }

    #[test]
    fn test_zip_crate_external_full() {
        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json");

        let zipped = zip_crate(&path, true, 0, false, false);
        println!("{:?}", zipped);
    }

    #[test]
    fn test_zip_crate_external_full_unique() {
        let path = fixture_path("unique_zips/_ro-crate-metadata-minimal.json");

        let zipped = zip_crate(&path, true, 0, false, true);
        println!("{:?}", zipped);
    }

    #[test]
    fn test_construct_paths() {
        let cwd = env::current_dir().unwrap();
        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json")
            .canonicalize()
            .unwrap();

        let paths = construct_paths(&path).unwrap();

        assert_eq!(paths.absolute_path, cwd.join(&path));
        assert_eq!(
            paths.root_path,
            cwd.join(PathBuf::from("tests/fixtures/test_experiment"))
                .canonicalize()
                .unwrap()
        );
        assert_eq!(
            paths.zip_file_name,
            cwd.join(
                PathBuf::from("tests/fixtures/test_experiment/test_experiment.zip")
                    .canonicalize()
                    .unwrap()
            )
        );
    }

    #[test]
    fn test_directory_walk() {
        let cwd = env::current_dir().unwrap();
        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json");

        let zip_paths = RoCrateZipPaths {
            absolute_path: cwd.join(&path),
            root_path: cwd.join(PathBuf::from("tests/fixtures/test_experiment")),
            zip_file_name: cwd.join(PathBuf::from(
                "tests/fixtures/test_experiment/test_experiment.zip",
            )),
        };

        let mut zip_data = RoCrateZip {
            zip: ZipWriter::new(File::create(&zip_paths.zip_file_name).unwrap()),
            options: FileOptions::default().compression_method(zip::CompressionMethod::Deflated),
        };

        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json");

        let mut rocrate = read_crate(&path, 0).unwrap();
        /*
        let mut contained: HashMap<String, PathBuf> = HashMap::new();
        contained.insert(
            "../external.txt".to_string(),
            cwd.join(PathBuf::from("tests/fixtures/external.txt")),
        );
        contained.insert(
            cwd.display().to_string() + "/tests/fixtures/test_experiment/data.csv",
            cwd.join(PathBuf::from("tests/fixtures/test_experiment/data.csv")),
        );
        contained.insert(
            "text_1.txt".to_string(),
            cwd.join(PathBuf::from("tests/fixtures/test_experiment/text_1.txt")),
        );
        */

        let mut directory_contents =
            directory_walk(&mut rocrate, &zip_paths, &mut zip_data, false).unwrap();

        let mut test_vec: Vec<PathBuf> = vec![
            cwd.join(
                PathBuf::from("tests/fixtures/test_experiment/data.csv")
                    .canonicalize()
                    .unwrap(),
            ),
            cwd.join(
                PathBuf::from("tests/fixtures/test_experiment/text_1.txt")
                    .canonicalize()
                    .unwrap(),
            ),
        ];

        directory_contents.sort();
        test_vec.sort();

        assert_eq!(directory_contents, test_vec);
    }

    #[test]
    fn test_is_not_url() {
        let mut url_types: HashMap<&str, bool> = HashMap::new();

        // A highly expansive and overkill URL/file path fixture generated with
        // GPT
        // URLs (false)
        url_types.insert("http://example.com", false); // HTTP
        url_types.insert("https://example.com", false); // HTTPS
        url_types.insert("ftp://ftp.example.com", false); // FTP
        url_types.insert("sftp://example.com", false); // SFTP
        url_types.insert("ws://example.com/socket", false); // WS
        url_types.insert("wss://example.com/socket", false); // WSS
        url_types.insert("data:text/html,<html>Hello!</html>", false); // DATA
        url_types.insert("blob:https://example.com/uuid", false); // BLOB
        url_types.insert("mailto:someone@example.com?subject=Hello", false); // MAILTO
        url_types.insert("tel:+1234567890", false); // TEL
        url_types.insert("sms:+1234567890?body=Hello", false); // SMS
        url_types.insert("jdbc:mysql://localhost:3306/database", false); // JDBC
        url_types.insert("urn:uuid:123e4567-e89b-12d3-a456-426614174000", false); // URN
        url_types.insert("ldap://example.com:389/dc=example,dc=com", false); // LDAP
        url_types.insert("ssh://user@server.com", false); // SSH
        url_types.insert("rtsp://media.example.com/video", false); // RTSP
        url_types.insert("mms://stream.example.com", false); // MMS
        url_types.insert("magnet:?xt=urn:btih:hash1234", false); // MAGNET
        url_types.insert("geo:37.7749,-122.4194", false); // GEO
        url_types.insert(
            "bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.01",
            false,
        ); // BITCOIN
        url_types.insert("ipfs://bafybeic3cphk4a", false); // IPFS
        url_types.insert("irc://irc.libera.chat/#rust", false); // IRC
        url_types.insert("git://github.com/user/repo.git", false); // GIT
        url_types.insert("telnet://192.168.1.1", false); // TELNET
        url_types.insert("news:comp.lang.rust", false); // NEWS
        url_types.insert("about:blank", false); // ABOUT
        url_types.insert("chrome://settings/", false); // CHROME
        url_types.insert("javascript:alert('Hello')", false); // JAVASCRIPT
        url_types.insert("s3://bucket-name/object-key", false); // AWS S3 Object
        url_types.insert("gs://bucket-name/object-key", false); // Google Cloud Storage Object
        url_types.insert("azure://container-name/blob-name", false); // Azure Blob Storage
        url_types.insert("swift://container-name/object-name", false); // OpenStack Swift Object
        url_types.insert("wasabi://bucket-name/object-key", false); // Wasabi Object Storage
        url_types.insert("minio://bucket-name/object-key", false); // MinIO Object Storage
        url_types.insert("aliyun://bucket-name/object-key", false); // Alibaba Cloud OSS
        url_types.insert("digitalocean://bucket-name/object-key", false); // DigitalOcean Spaces
        url_types.insert("ibmcloud://bucket-name/object-key", false); // IBM Cloud Object Storage
        url_types.insert("backblaze://bucket-name/object-key", false); // Backblaze B2 Storage
        url_types.insert("rackspace://container-name/object-name", false); // Rackspace Cloud Files
        url_types.insert("oracle://bucket-name/object-key", false); // Oracle Cloud Object Storage

        // Fragment ref
        url_types.insert("#test", true); // Fragment ref
        url_types.insert(
            "main.nf#main/FAMOSAB_WRROCMETATEST:WRROCMETATEST:FASTP",
            true,
        );

        // File Paths (true)
        url_types.insert("file:///C:/Windows/System32/drivers/etc/hosts", true); // Windows File Path
        url_types.insert("file:///Users/user/Documents/notes.txt", true); // macOS File Path
        url_types.insert("/home/user/Documents/report.pdf", true); // Linux Absolute Path
        url_types.insert("C:\\Users\\User\\Downloads\\file.txt", true); // Windows Backslash Path
        url_types.insert("../relative/path/to/file.txt", true); // Relative Path
        url_types.insert("./current/directory/file.txt", true); // Current Directory Path
        url_types.insert("/var/log/syslog", true); // Unix Log File
        url_types.insert("~/.ssh/config", true); // Unix Home Directory
        url_types.insert("~/Documents/resume.docx", true); // Unix Home Directory Expanded
        url_types.insert("/mnt/data/project/files", true); // Mounted Drive Path
        url_types.insert("E:\\Music\\playlist.m3u", true); // External Drive on Windows
        url_types.insert("\\\\network\\share\\folder\\file.txt", true); // UNC Path on Windows
        url_types.insert("/etc/nginx/nginx.conf", true); // Configuration File Path
        url_types.insert("/opt/app/bin/start.sh", true); // Unix Application Path
        url_types.insert("/dev/null", true); // Unix Special Device Path
        url_types.insert("C:/Program Files/App/app.exe", true); // Windows Program Path
        url_types.insert("/usr/local/bin/script", true); // Unix Local Bin Path
        url_types.insert("D:/Projects/Code/main.rs", true); // Windows Drive Path

        for (key, value) in url_types {
            let test = is_not_url(key);
            println!("Func result: {}, testing: {}, {}", test, key, value);
            assert_eq!(test, value);
        }
    }

    fn user_root_unix(mut path_types: HashMap<&str, bool>) -> HashMap<&str, bool> {
        if !cfg!(windows) {
            path_types.insert("~/.cargo/env", true); // Relative Path
            path_types.insert("/var/log/syslog", true); // Windows Backslash Path
        }
        path_types
    }

    #[test]
    fn test_get_noncontained_paths() {
        let mut path_types: HashMap<&str, bool> = HashMap::new();
        let cwd = env::current_dir().unwrap();
        let crate_path = cwd.join(PathBuf::from("tests/fixtures/test_experiment"));

        path_types.insert("../invalid.json", true); // Windows File Path
        path_types.insert("../external.txt", true); // Windows File Path
        path_types.insert("./data.csv", false); // macOS File Path
        path_types.insert("./text_1.txt", false); // Linux Absolute Path
        path_types.insert("text_1.txt", false); // Linux Absolute Path
        path_types.insert("#fragment", false); // Relative Path

        path_types = user_root_unix(path_types); //Check tilde paths

        // abs path but not relative
        let abs_not = cwd
            .join(PathBuf::from("README.md"))
            .to_str()
            .unwrap()
            .to_string();
        path_types.insert(&abs_not, true);

        // abs path rel
        let abs_is = cwd
            .join(crate_path.join(PathBuf::from("data.csv")))
            .to_str()
            .unwrap()
            .to_string();
        path_types.insert(&abs_is, false);

        for (key, value) in path_types {
            let mut input_vec: Vec<&String> = Vec::new();
            let target = key.to_string();
            input_vec.push(&target);

            let test = get_noncontained_paths(input_vec.clone(), &crate_path, false);
            if test.is_empty() {
                println!("Test is empty for relative ID: {}", key);
                assert_eq!(value, false)
            } else {
                println!("Test is successful for relative ID: {}", key);
                assert_eq!(value, true)
            }
        }
    }

    #[test]
    fn test_zip_crate_external_func() {
        let cwd = env::current_dir().unwrap();
        let path = fixture_path("test_experiment/_ro-crate-metadata-minimal.json");

        let mut rocrate = read_crate(&path, 0).unwrap();
        let zip_paths = RoCrateZipPaths {
            absolute_path: cwd.join(&path),
            root_path: cwd.join(PathBuf::from("tests/fixtures/test_experiment")),
            zip_file_name: cwd.join(PathBuf::from(
                "tests/fixtures/test_experiment/test_experiment.zip",
            )),
        };

        let zip_data = RoCrateZip {
            zip: ZipWriter::new(File::create(&zip_paths.zip_file_name).unwrap()),
            options: FileOptions::default().compression_method(zip::CompressionMethod::Deflated),
        };

        let _zipped = zip_crate_external(&mut rocrate, zip_data, &zip_paths);
    }
}