torsh-package 0.1.1

Model packaging and distribution utilities for ToRSh
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
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
//! Format compatibility layer for different model package formats
//!
//! This module provides compatibility with external package formats including
//! PyTorch torch.package, HuggingFace Hub, ONNX models, and MLflow packages.

use std::collections::HashMap;
use std::fs;

use oxiarc_archive::zip::{ZipCompressionLevel, ZipReader, ZipWriter};
use serde::{Deserialize, Serialize};
use torsh_core::error::{Result, TorshError};

use crate::package::Package;
use crate::resources::{Resource, ResourceType};

/// Supported external package formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PackageFormat {
    /// PyTorch torch.package format
    PyTorch,
    /// HuggingFace Hub format
    HuggingFace,
    /// ONNX model format
    Onnx,
    /// MLflow model format
    MLflow,
    /// Native ToRSh format
    ToRSh,
}

/// Format-specific converter trait
pub trait FormatConverter {
    /// Convert from the external format to ToRSh Package
    fn import_from_format(&self, path: &std::path::Path) -> Result<Package>;

    /// Convert from ToRSh Package to the external format
    fn export_to_format(&self, package: &Package, path: &std::path::Path) -> Result<()>;

    /// Get the format this converter handles
    fn format(&self) -> PackageFormat;

    /// Validate if a path contains a valid package of this format
    fn is_valid_format(&self, path: &std::path::Path) -> bool;
}

/// PyTorch torch.package compatibility layer
pub struct PyTorchConverter {
    preserve_python_code: bool,
    extract_models: bool,
}

/// HuggingFace Hub compatibility layer
pub struct HuggingFaceConverter {
    include_tokenizer: bool,
    include_config: bool,
    model_type: Option<String>,
}

/// ONNX model compatibility layer
pub struct OnnxConverter {
    include_metadata: bool,
    optimize_for_inference: bool,
}

/// MLflow model compatibility layer
pub struct MLflowConverter {
    include_conda_env: bool,
    include_requirements: bool,
    flavor: Option<String>,
}

/// PyTorch package manifest structure (simplified)
#[derive(Debug, Clone, Serialize, Deserialize)]
struct PyTorchManifest {
    code_version: String,
    main_module: String,
    dependencies: Vec<String>,
    python_version: Option<String>,
}

/// HuggingFace model configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
struct HuggingFaceConfig {
    model_type: String,
    task: Option<String>,
    architectures: Option<Vec<String>>,
    tokenizer_class: Option<String>,
    vocab_size: Option<u64>,
}

/// ONNX model metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
struct OnnxMetadata {
    ir_version: i64,
    producer_name: String,
    producer_version: String,
    domain: String,
    model_version: i64,
    doc_string: String,
}

/// MLflow model metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
struct MLflowMetadata {
    artifact_path: String,
    flavors: HashMap<String, serde_json::Value>,
    model_uuid: String,
    run_id: String,
    utc_time_created: String,
    mlflow_version: String,
}

impl Default for PyTorchConverter {
    fn default() -> Self {
        Self {
            preserve_python_code: true,
            extract_models: true,
        }
    }
}

impl PyTorchConverter {
    /// Create a new PyTorch converter
    pub fn new() -> Self {
        Self::default()
    }

    /// Configure whether to preserve Python code
    pub fn with_preserve_python_code(mut self, preserve: bool) -> Self {
        self.preserve_python_code = preserve;
        self
    }

    /// Configure whether to extract model weights
    pub fn with_extract_models(mut self, extract: bool) -> Self {
        self.extract_models = extract;
        self
    }

    /// Extract PyTorch package contents
    fn extract_pytorch_package(
        &self,
        path: &std::path::Path,
    ) -> Result<(PyTorchManifest, Vec<Resource>)> {
        let file = fs::File::open(path)
            .map_err(|e| TorshError::IoError(format!("Failed to open PyTorch package: {}", e)))?;

        let mut archive = ZipReader::new(file)
            .map_err(|e| TorshError::InvalidArgument(format!("Invalid ZIP archive: {}", e)))?;

        let mut manifest = None;
        let mut resources = Vec::new();

        // Collect entries to avoid borrow checker issues
        let entries: Vec<_> = archive.entries().to_vec();

        for entry in entries {
            let file_name = entry.name.clone();

            // Read file contents
            let contents = archive
                .extract(&entry)
                .map_err(|e| TorshError::IoError(format!("Failed to read archive entry: {}", e)))?;

            if file_name == ".data/version" {
                // PyTorch package version info - convert to manifest
                let version_str = String::from_utf8(contents).map_err(|_| {
                    TorshError::InvalidArgument("Invalid UTF-8 in version file".to_string())
                })?;

                manifest = Some(PyTorchManifest {
                    code_version: version_str.trim().to_string(),
                    main_module: "main".to_string(), // Default
                    dependencies: Vec::new(),
                    python_version: None,
                });
            } else if file_name.ends_with(".py") && self.preserve_python_code {
                // Python source code
                resources.push(Resource {
                    name: file_name.clone(),
                    resource_type: ResourceType::Source,
                    data: contents,
                    metadata: {
                        let mut meta = HashMap::new();
                        meta.insert("language".to_string(), "python".to_string());
                        meta.insert("original_format".to_string(), "pytorch".to_string());
                        meta
                    },
                });
            } else if file_name.ends_with(".pkl") && self.extract_models {
                // Pickle files (likely model weights)
                resources.push(Resource {
                    name: file_name.clone(),
                    resource_type: ResourceType::Model,
                    data: contents,
                    metadata: {
                        let mut meta = HashMap::new();
                        meta.insert("format".to_string(), "pickle".to_string());
                        meta.insert("original_format".to_string(), "pytorch".to_string());
                        meta
                    },
                });
            } else {
                // Other data files
                resources.push(Resource {
                    name: file_name.clone(),
                    resource_type: ResourceType::Data,
                    data: contents,
                    metadata: {
                        let mut meta = HashMap::new();
                        meta.insert("original_format".to_string(), "pytorch".to_string());
                        meta
                    },
                });
            }
        }

        let manifest = manifest.unwrap_or_else(|| PyTorchManifest {
            code_version: "1.0.0".to_string(),
            main_module: "main".to_string(),
            dependencies: Vec::new(),
            python_version: None,
        });

        Ok((manifest, resources))
    }
}

impl FormatConverter for PyTorchConverter {
    fn import_from_format(&self, path: &std::path::Path) -> Result<Package> {
        let (pytorch_manifest, resources) = self.extract_pytorch_package(path)?;

        let package_name = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("imported_pytorch_model")
            .to_string();

        let mut package = Package::new(package_name, pytorch_manifest.code_version);

        // Add resources
        for resource in resources {
            package.add_resource(resource);
        }

        // Add PyTorch-specific metadata
        package
            .manifest_mut()
            .metadata
            .insert("original_format".to_string(), "pytorch".to_string());
        package
            .manifest_mut()
            .metadata
            .insert("main_module".to_string(), pytorch_manifest.main_module);

        if let Some(python_version) = pytorch_manifest.python_version {
            package
                .manifest_mut()
                .metadata
                .insert("python_version".to_string(), python_version);
        }

        // Add dependencies
        for dep in pytorch_manifest.dependencies {
            package.add_dependency(&dep, "*");
        }

        Ok(package)
    }

    fn export_to_format(&self, package: &Package, path: &std::path::Path) -> Result<()> {
        let file = fs::File::create(path)
            .map_err(|e| TorshError::IoError(format!("Failed to create output file: {}", e)))?;

        let mut zip = ZipWriter::new(file);
        zip.set_compression(ZipCompressionLevel::Normal);

        // Add version file
        let version_data = package.get_version().as_bytes();
        zip.add_file(".data/version", version_data)
            .map_err(|e| TorshError::IoError(format!("Failed to create version file: {}", e)))?;

        // Add resources
        for (name, resource) in package.resources() {
            let file_path =
                if resource.resource_type == ResourceType::Source && name.ends_with(".py") {
                    format!("code/{}", name)
                } else if resource.resource_type == ResourceType::Model {
                    format!("data/{}", name)
                } else {
                    name.clone()
                };

            zip.add_file(&file_path, &resource.data).map_err(|e| {
                TorshError::IoError(format!("Failed to create file {}: {}", file_path, e))
            })?;
        }

        zip.finish()
            .map_err(|e| TorshError::IoError(format!("Failed to finalize ZIP archive: {}", e)))?;

        Ok(())
    }

    fn format(&self) -> PackageFormat {
        PackageFormat::PyTorch
    }

    fn is_valid_format(&self, path: &std::path::Path) -> bool {
        // Check if it's a ZIP file with PyTorch package structure
        if let Ok(file) = fs::File::open(path) {
            if let Ok(archive) = ZipReader::new(file) {
                // Look for characteristic PyTorch package files
                for entry in archive.entries() {
                    let name = &entry.name;
                    if name == ".data/version"
                        || name.starts_with("code/")
                        || name.ends_with(".pkl")
                    {
                        return true;
                    }
                }
            }
        }
        false
    }
}

impl Default for HuggingFaceConverter {
    fn default() -> Self {
        Self {
            include_tokenizer: true,
            include_config: true,
            model_type: None,
        }
    }
}

impl HuggingFaceConverter {
    /// Create a new HuggingFace converter
    pub fn new() -> Self {
        Self::default()
    }

    /// Configure whether to include tokenizer files
    pub fn with_include_tokenizer(mut self, include: bool) -> Self {
        self.include_tokenizer = include;
        self
    }

    /// Configure whether to include model configuration
    pub fn with_include_config(mut self, include: bool) -> Self {
        self.include_config = include;
        self
    }

    /// Set expected model type
    pub fn with_model_type(mut self, model_type: String) -> Self {
        self.model_type = Some(model_type);
        self
    }

    /// Load HuggingFace model directory
    fn load_huggingface_model(
        &self,
        path: &std::path::Path,
    ) -> Result<(HuggingFaceConfig, Vec<Resource>)> {
        let model_dir = path;

        if !model_dir.is_dir() {
            return Err(TorshError::InvalidArgument(
                "HuggingFace path must be a directory".to_string(),
            ));
        }

        let mut config = None;
        let mut resources = Vec::new();

        // Read configuration file
        let config_path = model_dir.join("config.json");
        if config_path.exists() && self.include_config {
            let config_data = fs::read(&config_path)
                .map_err(|e| TorshError::IoError(format!("Failed to read config.json: {}", e)))?;

            config = Some(
                serde_json::from_slice::<HuggingFaceConfig>(&config_data).map_err(|e| {
                    TorshError::SerializationError(format!("Invalid config.json: {}", e))
                })?,
            );

            resources.push(Resource {
                name: "config.json".to_string(),
                resource_type: ResourceType::Config,
                data: config_data,
                metadata: {
                    let mut meta = HashMap::new();
                    meta.insert("original_format".to_string(), "huggingface".to_string());
                    meta
                },
            });
        }

        // Read model files (pytorch_model.bin, model.safetensors, etc.)
        for entry in fs::read_dir(model_dir)
            .map_err(|e| TorshError::IoError(format!("Failed to read model directory: {}", e)))?
        {
            let entry = entry.map_err(|e| {
                TorshError::IoError(format!("Failed to read directory entry: {}", e))
            })?;
            let file_path = entry.path();
            let file_name = file_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("")
                .to_string();

            if file_name.ends_with(".bin") || file_name.ends_with(".safetensors") {
                // Model weight files
                let data = fs::read(&file_path).map_err(|e| {
                    TorshError::IoError(format!("Failed to read {}: {}", file_name, e))
                })?;

                resources.push(Resource {
                    name: file_name.clone(),
                    resource_type: ResourceType::Model,
                    data,
                    metadata: {
                        let mut meta = HashMap::new();
                        meta.insert("original_format".to_string(), "huggingface".to_string());
                        if file_name.ends_with(".safetensors") {
                            meta.insert("format".to_string(), "safetensors".to_string());
                        } else {
                            meta.insert("format".to_string(), "pytorch".to_string());
                        }
                        meta
                    },
                });
            } else if self.include_tokenizer
                && (file_name.starts_with("tokenizer") || file_name.ends_with(".json"))
            {
                // Tokenizer files
                let data = fs::read(&file_path).map_err(|e| {
                    TorshError::IoError(format!("Failed to read {}: {}", file_name, e))
                })?;

                resources.push(Resource {
                    name: file_name,
                    resource_type: ResourceType::Data,
                    data,
                    metadata: {
                        let mut meta = HashMap::new();
                        meta.insert("original_format".to_string(), "huggingface".to_string());
                        meta.insert("type".to_string(), "tokenizer".to_string());
                        meta
                    },
                });
            }
        }

        let config = config.unwrap_or_else(|| HuggingFaceConfig {
            model_type: self
                .model_type
                .clone()
                .unwrap_or_else(|| "unknown".to_string()),
            task: None,
            architectures: None,
            tokenizer_class: None,
            vocab_size: None,
        });

        Ok((config, resources))
    }
}

impl FormatConverter for HuggingFaceConverter {
    fn import_from_format(&self, path: &std::path::Path) -> Result<Package> {
        let (hf_config, resources) = self.load_huggingface_model(path)?;

        let package_name = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("imported_huggingface_model")
            .to_string();

        let mut package = Package::new(package_name, "1.0.0".to_string());

        // Add resources
        for resource in resources {
            package.add_resource(resource);
        }

        // Add HuggingFace-specific metadata
        package
            .manifest_mut()
            .metadata
            .insert("original_format".to_string(), "huggingface".to_string());
        package
            .manifest_mut()
            .metadata
            .insert("model_type".to_string(), hf_config.model_type);

        if let Some(task) = hf_config.task {
            package
                .manifest_mut()
                .metadata
                .insert("task".to_string(), task);
        }

        if let Some(architectures) = hf_config.architectures {
            package.manifest_mut().metadata.insert(
                "architectures".to_string(),
                serde_json::to_string(&architectures).unwrap_or_default(),
            );
        }

        Ok(package)
    }

    fn export_to_format(&self, package: &Package, path: &std::path::Path) -> Result<()> {
        let output_dir = path;

        if !output_dir.exists() {
            fs::create_dir_all(output_dir).map_err(|e| {
                TorshError::IoError(format!("Failed to create output directory: {}", e))
            })?;
        }

        // Export resources to appropriate files
        for (name, resource) in package.resources() {
            let file_path = output_dir.join(name);
            fs::write(&file_path, &resource.data)
                .map_err(|e| TorshError::IoError(format!("Failed to write {}: {}", name, e)))?;
        }

        // Create or update config.json if not present
        let config_path = output_dir.join("config.json");
        if !config_path.exists() {
            let default_config = HuggingFaceConfig {
                model_type: package
                    .metadata()
                    .metadata
                    .get("model_type")
                    .cloned()
                    .unwrap_or_else(|| "unknown".to_string()),
                task: package.metadata().metadata.get("task").cloned(),
                architectures: package
                    .metadata()
                    .metadata
                    .get("architectures")
                    .and_then(|s| serde_json::from_str(s).ok()),
                tokenizer_class: None,
                vocab_size: None,
            };

            let config_json = serde_json::to_string_pretty(&default_config).map_err(|e| {
                TorshError::SerializationError(format!("Failed to serialize config: {}", e))
            })?;

            fs::write(&config_path, config_json)
                .map_err(|e| TorshError::IoError(format!("Failed to write config.json: {}", e)))?;
        }

        Ok(())
    }

    fn format(&self) -> PackageFormat {
        PackageFormat::HuggingFace
    }

    fn is_valid_format(&self, path: &std::path::Path) -> bool {
        let model_dir = path;

        if !model_dir.is_dir() {
            return false;
        }

        // Check for characteristic HuggingFace files
        let config_path = model_dir.join("config.json");
        if config_path.exists() {
            return true;
        }

        // Check for model weight files
        if let Ok(entries) = fs::read_dir(model_dir) {
            for entry in entries {
                if let Ok(entry) = entry {
                    let file_name = entry.file_name();
                    let file_name_str = file_name.to_string_lossy();
                    if file_name_str.ends_with(".bin") || file_name_str.ends_with(".safetensors") {
                        return true;
                    }
                }
            }
        }

        false
    }
}

impl Default for OnnxConverter {
    fn default() -> Self {
        Self {
            include_metadata: true,
            optimize_for_inference: false,
        }
    }
}

impl OnnxConverter {
    /// Create a new ONNX converter
    pub fn new() -> Self {
        Self::default()
    }

    /// Configure whether to include model metadata
    pub fn with_include_metadata(mut self, include: bool) -> Self {
        self.include_metadata = include;
        self
    }

    /// Configure optimization for inference
    pub fn with_optimize_for_inference(mut self, optimize: bool) -> Self {
        self.optimize_for_inference = optimize;
        self
    }

    /// Extract ONNX model metadata
    fn extract_onnx_metadata(&self, path: &std::path::Path) -> Result<OnnxMetadata> {
        // For now, return a basic metadata structure
        // In a real implementation, you would parse the ONNX protobuf format
        Ok(OnnxMetadata {
            ir_version: 8,
            producer_name: "torsh-package".to_string(),
            producer_version: "1.0.0".to_string(),
            domain: "ai.onnx".to_string(),
            model_version: 1,
            doc_string: format!("ONNX model imported from {:?}", path),
        })
    }
}

impl FormatConverter for OnnxConverter {
    fn import_from_format(&self, path: &std::path::Path) -> Result<Package> {
        let model_data = fs::read(path)
            .map_err(|e| TorshError::IoError(format!("Failed to read ONNX model: {}", e)))?;

        let package_name = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("imported_onnx_model")
            .to_string();

        let mut package = Package::new(package_name, "1.0.0".to_string());

        // Add ONNX model as a resource
        let model_resource = Resource {
            name: "model.onnx".to_string(),
            resource_type: ResourceType::Model,
            data: model_data,
            metadata: {
                let mut meta = HashMap::new();
                meta.insert("original_format".to_string(), "onnx".to_string());
                meta.insert("format".to_string(), "onnx".to_string());
                meta
            },
        };
        package.add_resource(model_resource);

        // Add metadata if requested
        if self.include_metadata {
            let onnx_metadata = self.extract_onnx_metadata(path)?;
            package.manifest_mut().metadata.insert(
                "onnx_ir_version".to_string(),
                onnx_metadata.ir_version.to_string(),
            );
            package
                .manifest_mut()
                .metadata
                .insert("onnx_producer".to_string(), onnx_metadata.producer_name);
            package.manifest_mut().metadata.insert(
                "onnx_producer_version".to_string(),
                onnx_metadata.producer_version,
            );
        }

        package
            .manifest_mut()
            .metadata
            .insert("original_format".to_string(), "onnx".to_string());

        Ok(package)
    }

    fn export_to_format(&self, package: &Package, path: &std::path::Path) -> Result<()> {
        // Find the ONNX model resource
        let model_resource = package
            .resources()
            .iter()
            .find(|(_, resource)| {
                resource.resource_type == ResourceType::Model
                    && (resource.name.ends_with(".onnx")
                        || resource
                            .metadata
                            .get("format")
                            .map_or(false, |f| f == "onnx"))
            })
            .map(|(_, resource)| resource)
            .ok_or_else(|| {
                TorshError::InvalidArgument("No ONNX model found in package".to_string())
            })?;

        // Write the ONNX model to file
        fs::write(path, &model_resource.data)
            .map_err(|e| TorshError::IoError(format!("Failed to write ONNX model: {}", e)))?;

        Ok(())
    }

    fn format(&self) -> PackageFormat {
        PackageFormat::Onnx
    }

    fn is_valid_format(&self, path: &std::path::Path) -> bool {
        if let Ok(file) = fs::File::open(path) {
            use std::io::Read;
            let mut buffer = [0u8; 16];
            let mut reader = std::io::BufReader::new(file);

            // Check for ONNX magic bytes (protobuf)
            if reader.read_exact(&mut buffer).is_ok() {
                // ONNX files typically start with protobuf headers
                // This is a simplified check; real implementation would parse protobuf
                return path
                    .extension()
                    .and_then(|e| e.to_str())
                    .map_or(false, |e| e == "onnx");
            }
        }
        false
    }
}

impl Default for MLflowConverter {
    fn default() -> Self {
        Self {
            include_conda_env: true,
            include_requirements: true,
            flavor: None,
        }
    }
}

impl MLflowConverter {
    /// Create a new MLflow converter
    pub fn new() -> Self {
        Self::default()
    }

    /// Configure whether to include conda environment
    pub fn with_include_conda_env(mut self, include: bool) -> Self {
        self.include_conda_env = include;
        self
    }

    /// Configure whether to include requirements.txt
    pub fn with_include_requirements(mut self, include: bool) -> Self {
        self.include_requirements = include;
        self
    }

    /// Set MLflow flavor
    pub fn with_flavor(mut self, flavor: String) -> Self {
        self.flavor = Some(flavor);
        self
    }

    /// Load MLflow model directory
    fn load_mlflow_model(&self, path: &std::path::Path) -> Result<(MLflowMetadata, Vec<Resource>)> {
        if !path.is_dir() {
            return Err(TorshError::InvalidArgument(
                "MLflow path must be a directory".to_string(),
            ));
        }

        let mut metadata = None;
        let mut resources = Vec::new();

        // Read MLmodel file
        let mlmodel_path = path.join("MLmodel");
        if mlmodel_path.exists() {
            let mlmodel_data = fs::read_to_string(&mlmodel_path)
                .map_err(|e| TorshError::IoError(format!("Failed to read MLmodel: {}", e)))?;

            // Parse MLmodel (YAML format)
            // For simplicity, we'll create a basic metadata structure
            metadata = Some(MLflowMetadata {
                artifact_path: path.to_string_lossy().to_string(),
                flavors: HashMap::new(),
                model_uuid: uuid::Uuid::new_v4().to_string(),
                run_id: "imported".to_string(),
                utc_time_created: chrono::Utc::now().to_rfc3339(),
                mlflow_version: "2.0.0".to_string(),
            });

            resources.push(Resource {
                name: "MLmodel".to_string(),
                resource_type: ResourceType::Config,
                data: mlmodel_data.into_bytes(),
                metadata: {
                    let mut meta = HashMap::new();
                    meta.insert("original_format".to_string(), "mlflow".to_string());
                    meta
                },
            });
        }

        // Read model files from subdirectories
        for entry in fs::read_dir(path)
            .map_err(|e| TorshError::IoError(format!("Failed to read MLflow directory: {}", e)))?
        {
            let entry = entry.map_err(|e| {
                TorshError::IoError(format!("Failed to read directory entry: {}", e))
            })?;
            let file_path = entry.path();

            if file_path.is_file() {
                let file_name = file_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("")
                    .to_string();

                if file_name != "MLmodel" {
                    let data = fs::read(&file_path).map_err(|e| {
                        TorshError::IoError(format!("Failed to read {}: {}", file_name, e))
                    })?;

                    let resource_type = if file_name.ends_with(".pkl")
                        || file_name.ends_with(".pt")
                        || file_name.ends_with(".h5")
                    {
                        ResourceType::Model
                    } else if file_name.ends_with(".json") || file_name.ends_with(".yaml") {
                        ResourceType::Config
                    } else if file_name == "requirements.txt" || file_name == "conda.yaml" {
                        ResourceType::Documentation
                    } else {
                        ResourceType::Data
                    };

                    resources.push(Resource {
                        name: file_name,
                        resource_type,
                        data,
                        metadata: {
                            let mut meta = HashMap::new();
                            meta.insert("original_format".to_string(), "mlflow".to_string());
                            meta
                        },
                    });
                }
            }
        }

        let metadata = metadata.unwrap_or_else(|| MLflowMetadata {
            artifact_path: path.to_string_lossy().to_string(),
            flavors: HashMap::new(),
            model_uuid: uuid::Uuid::new_v4().to_string(),
            run_id: "imported".to_string(),
            utc_time_created: chrono::Utc::now().to_rfc3339(),
            mlflow_version: "2.0.0".to_string(),
        });

        Ok((metadata, resources))
    }
}

impl FormatConverter for MLflowConverter {
    fn import_from_format(&self, path: &std::path::Path) -> Result<Package> {
        let (mlflow_metadata, resources) = self.load_mlflow_model(path)?;

        let package_name = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("imported_mlflow_model")
            .to_string();

        let mut package = Package::new(package_name, "1.0.0".to_string());

        // Add resources
        for resource in resources {
            package.add_resource(resource);
        }

        // Add MLflow-specific metadata
        package
            .manifest_mut()
            .metadata
            .insert("original_format".to_string(), "mlflow".to_string());
        package
            .manifest_mut()
            .metadata
            .insert("mlflow_version".to_string(), mlflow_metadata.mlflow_version);
        package
            .manifest_mut()
            .metadata
            .insert("model_uuid".to_string(), mlflow_metadata.model_uuid);
        package
            .manifest_mut()
            .metadata
            .insert("run_id".to_string(), mlflow_metadata.run_id);

        if let Some(flavor) = &self.flavor {
            package
                .manifest_mut()
                .metadata
                .insert("flavor".to_string(), flavor.clone());
        }

        Ok(package)
    }

    fn export_to_format(&self, package: &Package, path: &std::path::Path) -> Result<()> {
        let output_dir = path;

        if !output_dir.exists() {
            fs::create_dir_all(output_dir).map_err(|e| {
                TorshError::IoError(format!("Failed to create output directory: {}", e))
            })?;
        }

        // Export resources to appropriate files
        for (name, resource) in package.resources() {
            let file_path = output_dir.join(name);
            fs::write(&file_path, &resource.data)
                .map_err(|e| TorshError::IoError(format!("Failed to write {}: {}", name, e)))?;
        }

        // Create MLmodel file if not present
        let mlmodel_path = output_dir.join("MLmodel");
        if !mlmodel_path.exists() {
            let mlmodel_content = format!(
                r#"artifact_path: {}
flavors:
  python_function:
    env: conda.yaml
    loader_module: mlflow.pyfunc.model
    python_version: 3.9
model_uuid: {}
run_id: {}
utc_time_created: '{}'
mlflow_version: 2.0.0
"#,
                output_dir.to_string_lossy(),
                package
                    .metadata()
                    .metadata
                    .get("model_uuid")
                    .cloned()
                    .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
                package
                    .metadata()
                    .metadata
                    .get("run_id")
                    .cloned()
                    .unwrap_or_else(|| "exported".to_string()),
                chrono::Utc::now().to_rfc3339()
            );

            fs::write(&mlmodel_path, mlmodel_content)
                .map_err(|e| TorshError::IoError(format!("Failed to write MLmodel: {}", e)))?;
        }

        Ok(())
    }

    fn format(&self) -> PackageFormat {
        PackageFormat::MLflow
    }

    fn is_valid_format(&self, path: &std::path::Path) -> bool {
        if !path.is_dir() {
            return false;
        }

        // Check for MLmodel file
        let mlmodel_path = path.join("MLmodel");
        mlmodel_path.exists()
    }
}

/// Format compatibility manager
pub struct FormatCompatibilityManager {
    converters: HashMap<PackageFormat, Box<dyn FormatConverter>>,
}

impl Default for FormatCompatibilityManager {
    fn default() -> Self {
        let mut manager = Self {
            converters: HashMap::new(),
        };

        // Register default converters
        manager.register_converter(Box::new(PyTorchConverter::new()));
        manager.register_converter(Box::new(HuggingFaceConverter::new()));
        manager.register_converter(Box::new(OnnxConverter::new()));
        manager.register_converter(Box::new(MLflowConverter::new()));

        manager
    }
}

impl FormatCompatibilityManager {
    /// Create a new format compatibility manager
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a format converter
    pub fn register_converter(&mut self, converter: Box<dyn FormatConverter>) {
        let format = converter.format();
        self.converters.insert(format, converter);
    }

    /// Auto-detect format and import package
    pub fn import_package(&self, path: &std::path::Path) -> Result<(PackageFormat, Package)> {
        for (format, converter) in &self.converters {
            if converter.is_valid_format(path) {
                let package = converter.import_from_format(path)?;
                return Ok((*format, package));
            }
        }

        Err(TorshError::InvalidArgument(
            "Unrecognized package format".to_string(),
        ))
    }

    /// Export package to specific format
    pub fn export_package(
        &self,
        package: &Package,
        format: PackageFormat,
        path: &std::path::Path,
    ) -> Result<()> {
        let converter = self.converters.get(&format).ok_or_else(|| {
            TorshError::InvalidArgument(format!("Unsupported export format: {:?}", format))
        })?;

        converter.export_to_format(package, path)
    }

    /// List supported formats
    pub fn supported_formats(&self) -> Vec<PackageFormat> {
        self.converters.keys().copied().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_pytorch_converter_format_detection() {
        let converter = PyTorchConverter::new();
        assert_eq!(converter.format(), PackageFormat::PyTorch);
    }

    #[test]
    fn test_huggingface_converter_creation() {
        let converter = HuggingFaceConverter::new()
            .with_include_tokenizer(false)
            .with_model_type("bert".to_string());

        assert_eq!(converter.format(), PackageFormat::HuggingFace);
        assert!(!converter.include_tokenizer);
        assert_eq!(converter.model_type, Some("bert".to_string()));
    }

    #[test]
    fn test_format_manager() {
        let manager = FormatCompatibilityManager::new();
        let formats = manager.supported_formats();

        assert!(formats.contains(&PackageFormat::PyTorch));
        assert!(formats.contains(&PackageFormat::HuggingFace));
    }

    #[test]
    fn test_huggingface_directory_validation() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");

        // Create a mock HuggingFace model directory
        let config_path = temp_dir.path().join("config.json");
        let mut config_file = fs::File::create(&config_path).unwrap();
        writeln!(
            config_file,
            r#"{{"model_type": "bert", "task": "text-classification"}}"#
        )
        .unwrap();

        let converter = HuggingFaceConverter::new();
        assert!(converter.is_valid_format(temp_dir.path()));
    }

    #[test]
    fn test_package_format_enum() {
        assert_eq!(PackageFormat::PyTorch, PackageFormat::PyTorch);
        assert_ne!(PackageFormat::PyTorch, PackageFormat::HuggingFace);
    }
}