subversion 0.1.10

Rust bindings for Subversion
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
//! Keyword substitution and EOL translation
//!
//! This module provides safe Rust wrappers around Subversion's keyword and
//! end-of-line substitution functionality.

use crate::{with_tmp_pool, Error};
use std::collections::HashMap;

/// EOL style for text files
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EolStyle {
    /// Unrecognized style
    Unknown,
    /// No EOL translation
    None,
    /// Use the native EOL style for the platform
    Native,
    /// Use a fixed EOL style (LF, CR, or CRLF)
    Fixed,
}

impl From<subversion_sys::svn_subst_eol_style_t> for EolStyle {
    fn from(style: subversion_sys::svn_subst_eol_style_t) -> Self {
        match style {
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_unknown => EolStyle::Unknown,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_none => EolStyle::None,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_native => EolStyle::Native,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_fixed => EolStyle::Fixed,
            _ => unreachable!("unknown svn_subst_eol_style_t value: {}", style),
        }
    }
}

impl From<EolStyle> for subversion_sys::svn_subst_eol_style_t {
    fn from(style: EolStyle) -> Self {
        match style {
            EolStyle::Unknown => subversion_sys::svn_subst_eol_style_svn_subst_eol_style_unknown,
            EolStyle::None => subversion_sys::svn_subst_eol_style_svn_subst_eol_style_none,
            EolStyle::Native => subversion_sys::svn_subst_eol_style_svn_subst_eol_style_native,
            EolStyle::Fixed => subversion_sys::svn_subst_eol_style_svn_subst_eol_style_fixed,
        }
    }
}

/// Parse an EOL style value from a property string
///
/// Returns the EOL style and the corresponding EOL string.
/// The EOL string is None for EolStyle::None, the platform native
/// EOL for EolStyle::Native, or the specific EOL for EolStyle::Fixed.
pub fn eol_style_from_value(value: &str) -> (EolStyle, Option<String>) {
    let value_cstr = std::ffi::CString::new(value).unwrap();

    unsafe {
        let mut style: subversion_sys::svn_subst_eol_style_t =
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_unknown;
        let mut eol_ptr: *const std::ffi::c_char = std::ptr::null();

        subversion_sys::svn_subst_eol_style_from_value(
            &mut style,
            &mut eol_ptr,
            value_cstr.as_ptr(),
        );

        let eol_str = if eol_ptr.is_null() {
            None
        } else {
            Some(
                std::ffi::CStr::from_ptr(eol_ptr)
                    .to_str()
                    .unwrap()
                    .to_string(),
            )
        };

        (EolStyle::from(style), eol_str)
    }
}

/// Check if translation is required for the given parameters
///
/// Returns true if the working copy and normalized versions of a file
/// with the given parameters differ.
pub fn translation_required(
    style: EolStyle,
    eol: Option<&str>,
    keywords: Option<&HashMap<String, String>>,
    special: bool,
    force_eol_check: bool,
) -> bool {
    with_tmp_pool(|pool| {
        let eol_cstr = eol.map(|s| std::ffi::CString::new(s).unwrap());
        let eol_ptr = eol_cstr
            .as_ref()
            .map(|c| c.as_ptr())
            .unwrap_or(std::ptr::null());

        let keywords_hash = match keywords {
            Some(kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                for (k, v) in kw.iter() {
                    unsafe {
                        // Copy key into pool to ensure it outlives the hash
                        let k_pooled = apr::strings::pstrdup_raw(k, pool).unwrap();
                        // Create svn_string_t for the value (SVN expects svn_string_t*, not const char*)
                        let v_cstr = std::ffi::CString::new(v.as_str()).unwrap();
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        // Use the pooled key which is null-terminated
                        let key_slice =
                            std::slice::from_raw_parts(k_pooled as *const u8, k.len() + 1);
                        hash.insert(key_slice, svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        unsafe {
            subversion_sys::svn_subst_translation_required(
                style.into(),
                eol_ptr,
                keywords_hash,
                if special { 1 } else { 0 },
                if force_eol_check { 1 } else { 0 },
            ) != 0
        }
    })
}

/// Build keyword hash from keyword string and file information
///
/// Creates a keyword hash suitable for use with translation functions.
/// The keywords_string should be the value of the svn:keywords property.
pub fn build_keywords(
    keywords_string: &str,
    rev: Option<&str>,
    url: Option<&str>,
    date: Option<apr::time::Time>,
    author: Option<&str>,
    repos_root_url: Option<&str>,
) -> Result<HashMap<String, String>, Error<'static>> {
    with_tmp_pool(|pool| {
        let keywords_cstr = std::ffi::CString::new(keywords_string).unwrap();
        let rev_cstr = rev.map(|s| std::ffi::CString::new(s).unwrap());
        let url_cstr = url.map(|s| std::ffi::CString::new(s).unwrap());
        let author_cstr = author.map(|s| std::ffi::CString::new(s).unwrap());
        let repos_root_cstr = repos_root_url.map(|s| std::ffi::CString::new(s).unwrap());

        let mut keywords_hash: *mut apr_sys::apr_hash_t = std::ptr::null_mut();

        unsafe {
            let err = subversion_sys::svn_subst_build_keywords3(
                &mut keywords_hash,
                keywords_cstr.as_ptr(),
                rev_cstr
                    .as_ref()
                    .map(|c| c.as_ptr())
                    .unwrap_or(std::ptr::null()),
                url_cstr
                    .as_ref()
                    .map(|c| c.as_ptr())
                    .unwrap_or(std::ptr::null()),
                repos_root_cstr
                    .as_ref()
                    .map(|c| c.as_ptr())
                    .unwrap_or(std::ptr::null()),
                date.map(|d| d.into()).unwrap_or(0),
                author_cstr
                    .as_ref()
                    .map(|c| c.as_ptr())
                    .unwrap_or(std::ptr::null()),
                pool.as_mut_ptr(),
            );
            Error::from_raw(err)?;
        }

        if keywords_hash.is_null() {
            return Ok(HashMap::new());
        }

        let prop_hash = unsafe { crate::props::PropHash::from_ptr(keywords_hash) };
        Ok(prop_hash.to_string_hashmap())
    })
}

/// Check if two keyword hashes differ
///
/// If compare_values is true, both the keys and values must match.
/// If false, only the keys need to match (values can differ).
pub fn keywords_differ(
    a: Option<&HashMap<String, String>>,
    b: Option<&HashMap<String, String>>,
    compare_values: bool,
) -> Result<bool, Error<'static>> {
    with_tmp_pool(|pool| {
        // Keep c_strings alive until after the FFI call
        // Store key AND CString value together to ensure iteration order matches
        // Sort by key to ensure deterministic order regardless of HashMap iteration
        let mut c_strings_a: Vec<_> = match a {
            Some(kw) => kw
                .iter()
                .map(|(k, v)| (k.as_str(), std::ffi::CString::new(v.as_str()).unwrap()))
                .collect(),
            None => Vec::new(),
        };
        c_strings_a.sort_by(|a, b| a.0.cmp(b.0));

        let hash_a = match a {
            Some(_kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                // Use c_strings_a directly instead of re-iterating over HashMap
                // to ensure consistent iteration order
                for (k, v_cstr) in c_strings_a.iter() {
                    unsafe {
                        // Create svn_string_t for the value (SVN expects svn_string_t*, not const char*)
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        hash.insert(k.as_bytes(), svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        // Keep c_strings alive until after the FFI call
        // Store key AND CString value together to ensure iteration order matches
        // Sort by key to ensure deterministic order regardless of HashMap iteration
        let mut c_strings_b: Vec<_> = match b {
            Some(kw) => kw
                .iter()
                .map(|(k, v)| (k.as_str(), std::ffi::CString::new(v.as_str()).unwrap()))
                .collect(),
            None => Vec::new(),
        };
        c_strings_b.sort_by(|a, b| a.0.cmp(b.0));

        let hash_b = match b {
            Some(_kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                // Use c_strings_b directly instead of re-iterating over HashMap
                // to ensure consistent iteration order
                for (k, v_cstr) in c_strings_b.iter() {
                    unsafe {
                        // Create svn_string_t for the value (SVN expects svn_string_t*, not const char*)
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        hash.insert(k.as_bytes(), svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        unsafe {
            let result = subversion_sys::svn_subst_keywords_differ2(
                hash_a,
                hash_b,
                if compare_values { 1 } else { 0 },
                pool.as_mut_ptr(),
            );
            Ok(result != 0)
        }
    })
}

/// Create a translated stream for keyword and EOL substitution
///
/// Returns a stream that performs EOL translation and keyword expansion/contraction
/// when data is read from or written to it.
pub fn stream_translated(
    mut source_stream: crate::io::Stream,
    eol_str: Option<&str>,
    repair: bool,
    keywords: Option<&HashMap<String, String>>,
    expand: bool,
) -> Result<crate::io::Stream, Error<'static>> {
    // Create result_pool OUTSIDE with_tmp_pool so it survives
    let result_pool = apr::Pool::new();

    // Copy eol_str into result_pool so it survives
    let eol_ptr = match eol_str {
        Some(s) => apr::strings::pstrdup_raw(s, &result_pool)?,
        None => std::ptr::null(),
    };

    // Allocate keywords_hash in result_pool, not temporary pool
    let keywords_hash = match keywords {
        Some(kw) => {
            // Create C strings and copy them into the result_pool using apr::strings::pstrdup
            let mut hash = apr::hash::Hash::new(&result_pool);
            for (k, v) in kw.iter() {
                unsafe {
                    // Copy both key and value into result_pool
                    let key_ptr = apr::strings::pstrdup_raw(k, &result_pool)?;
                    let val_ptr = apr::strings::pstrdup_raw(v, &result_pool)?;
                    hash.insert(
                        std::slice::from_raw_parts(key_ptr as *const u8, k.len()),
                        val_ptr as *mut std::ffi::c_void,
                    );
                }
            }
            unsafe { hash.as_mut_ptr() }
        }
        None => std::ptr::null_mut(),
    };

    unsafe {
        let translated_stream = subversion_sys::svn_subst_stream_translated(
            source_stream.as_mut_ptr(),
            eol_ptr,
            if repair { 1 } else { 0 },
            keywords_hash,
            if expand { 1 } else { 0 },
            result_pool.as_mut_ptr(),
        );

        Ok(crate::io::Stream::from_ptr(translated_stream, result_pool))
    }
}

/// Create a translated stream for converting to normal form
///
/// This creates a stream that translates content to the "normal form"
/// used internally by Subversion (LF line endings, expanded keywords).
pub fn stream_translated_to_normal_form(
    mut source_stream: crate::io::Stream,
    eol_style: EolStyle,
    eol_str: Option<&str>,
    always_repair_eols: bool,
    keywords: Option<&HashMap<String, String>>,
) -> Result<crate::io::Stream, Error<'static>> {
    // Create result_pool OUTSIDE with_tmp_pool so it survives
    let result_pool = apr::Pool::new();

    // Copy eol_str into result_pool so it survives
    let eol_ptr = match eol_str {
        Some(s) => apr::strings::pstrdup_raw(s, &result_pool)?,
        None => std::ptr::null(),
    };

    // Allocate keywords_hash in result_pool, not temporary pool
    let keywords_hash = match keywords {
        Some(kw) => {
            // Create C strings and copy them into the result_pool using apr::strings::pstrdup
            let mut hash = apr::hash::Hash::new(&result_pool);
            for (k, v) in kw.iter() {
                unsafe {
                    // Copy both key and value into result_pool
                    let key_ptr = apr::strings::pstrdup_raw(k, &result_pool)?;
                    let val_ptr = apr::strings::pstrdup_raw(v, &result_pool)?;
                    hash.insert(
                        std::slice::from_raw_parts(key_ptr as *const u8, k.len()),
                        val_ptr as *mut std::ffi::c_void,
                    );
                }
            }
            unsafe { hash.as_mut_ptr() }
        }
        None => std::ptr::null_mut(),
    };

    unsafe {
        let mut translated_stream: *mut subversion_sys::svn_stream_t = std::ptr::null_mut();
        let err = subversion_sys::svn_subst_stream_translated_to_normal_form(
            &mut translated_stream,
            source_stream.as_mut_ptr(),
            eol_style.into(),
            eol_ptr,
            if always_repair_eols { 1 } else { 0 },
            keywords_hash,
            result_pool.as_mut_ptr(),
        );
        Error::from_raw(err)?;

        Ok(crate::io::Stream::from_ptr(translated_stream, result_pool))
    }
}

/// Copy and translate a file with keyword and EOL substitution
///
/// This is a convenience function that copies from source to destination
/// while performing translation. Either eol_str or keywords (or both) must
/// be provided.
pub fn translate_stream(
    src_stream: &mut crate::io::Stream,
    dst_stream: &mut crate::io::Stream,
    eol_str: Option<&str>,
    repair: bool,
    keywords: Option<&HashMap<String, String>>,
    expand: bool,
) -> Result<(), Error<'static>> {
    with_tmp_pool(|pool| {
        let eol_cstr = eol_str.map(|s| std::ffi::CString::new(s).unwrap());
        let eol_ptr = eol_cstr
            .as_ref()
            .map(|c| c.as_ptr())
            .unwrap_or(std::ptr::null());

        let keywords_hash = match keywords {
            Some(kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                for (k, v) in kw.iter() {
                    unsafe {
                        // Copy key into pool to ensure it outlives the hash
                        let k_pooled = apr::strings::pstrdup_raw(k, pool).unwrap();
                        // Create svn_string_t for the value (SVN expects svn_string_t*, not const char*)
                        let v_cstr = std::ffi::CString::new(v.as_str()).unwrap();
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        // Use the pooled key which is null-terminated
                        let key_slice =
                            std::slice::from_raw_parts(k_pooled as *const u8, k.len() + 1);
                        hash.insert(key_slice, svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        unsafe {
            let err = subversion_sys::svn_subst_translate_stream3(
                src_stream.as_mut_ptr(),
                dst_stream.as_mut_ptr(),
                eol_ptr,
                if repair { 1 } else { 0 },
                keywords_hash,
                if expand { 1 } else { 0 },
                pool.as_mut_ptr(),
            );
            Error::from_raw(err)
        }
    })
}

/// Copy and translate a file with keyword and EOL substitution.
///
/// Copies from `src` to `dst` while performing EOL and keyword translation.
/// If `special` is true, the file is treated as a special file (symlink, etc.).
pub fn copy_and_translate(
    src: &std::path::Path,
    dst: &std::path::Path,
    eol_str: Option<&str>,
    repair: bool,
    keywords: Option<&HashMap<String, String>>,
    expand: bool,
    special: bool,
) -> Result<(), Error<'static>> {
    with_tmp_pool(|pool| {
        let src_cstr = std::ffi::CString::new(
            src.to_str()
                .ok_or_else(|| Error::from_message("Invalid source path encoding"))?,
        )?;
        let dst_cstr = std::ffi::CString::new(
            dst.to_str()
                .ok_or_else(|| Error::from_message("Invalid destination path encoding"))?,
        )?;

        let eol_cstr = eol_str.map(|s| std::ffi::CString::new(s).unwrap());
        let eol_ptr = eol_cstr
            .as_ref()
            .map(|c| c.as_ptr())
            .unwrap_or(std::ptr::null());

        let keywords_hash = match keywords {
            Some(kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                for (k, v) in kw.iter() {
                    unsafe {
                        let k_pooled = apr::strings::pstrdup_raw(k, pool).unwrap();
                        let v_cstr = std::ffi::CString::new(v.as_str()).unwrap();
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        let key_slice =
                            std::slice::from_raw_parts(k_pooled as *const u8, k.len() + 1);
                        hash.insert(key_slice, svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        unsafe {
            let err = subversion_sys::svn_subst_copy_and_translate4(
                src_cstr.as_ptr(),
                dst_cstr.as_ptr(),
                eol_ptr,
                if repair { 1 } else { 0 },
                keywords_hash,
                if expand { 1 } else { 0 },
                if special { 1 } else { 0 },
                None,
                std::ptr::null_mut(),
                pool.as_mut_ptr(),
            );
            Error::from_raw(err)
        }
    })
}

/// Translate a string with keyword and EOL substitution.
///
/// Returns the translated string.
pub fn translate_cstring(
    src: &str,
    eol_str: Option<&str>,
    repair: bool,
    keywords: Option<&HashMap<String, String>>,
    expand: bool,
) -> Result<String, Error<'static>> {
    with_tmp_pool(|pool| {
        let src_cstr = std::ffi::CString::new(src)?;
        let eol_cstr = eol_str.map(|s| std::ffi::CString::new(s).unwrap());
        let eol_ptr = eol_cstr
            .as_ref()
            .map(|c| c.as_ptr())
            .unwrap_or(std::ptr::null());

        let keywords_hash = match keywords {
            Some(kw) => {
                let mut hash = apr::hash::Hash::new(pool);
                for (k, v) in kw.iter() {
                    unsafe {
                        let k_pooled = apr::strings::pstrdup_raw(k, pool).unwrap();
                        let v_cstr = std::ffi::CString::new(v.as_str()).unwrap();
                        let svn_str =
                            subversion_sys::svn_string_create(v_cstr.as_ptr(), pool.as_mut_ptr());
                        let key_slice =
                            std::slice::from_raw_parts(k_pooled as *const u8, k.len() + 1);
                        hash.insert(key_slice, svn_str as *mut std::ffi::c_void);
                    }
                }
                unsafe { hash.as_mut_ptr() }
            }
            None => std::ptr::null_mut(),
        };

        let mut dst_ptr: *const std::ffi::c_char = std::ptr::null();

        unsafe {
            let err = subversion_sys::svn_subst_translate_cstring2(
                src_cstr.as_ptr(),
                &mut dst_ptr,
                eol_ptr,
                if repair { 1 } else { 0 },
                keywords_hash,
                if expand { 1 } else { 0 },
                pool.as_mut_ptr(),
            );
            Error::from_raw(err)?;

            if dst_ptr.is_null() {
                return Ok(String::new());
            }

            Ok(std::ffi::CStr::from_ptr(dst_ptr)
                .to_str()
                .map_err(|_| Error::from_message("Translated string is not valid UTF-8"))?
                .to_string())
        }
    })
}

/// Translate a string to UTF-8 and LF line endings.
///
/// Returns the translated string and flags indicating what translations were performed.
pub fn translate_string(
    value: &[u8],
    encoding: Option<&str>,
    repair: bool,
) -> Result<(Vec<u8>, bool, bool), Error<'static>> {
    with_tmp_pool(|pool| {
        let encoding_cstr = encoding.map(|s| std::ffi::CString::new(s).unwrap());
        let encoding_ptr = encoding_cstr
            .as_ref()
            .map(|c| c.as_ptr())
            .unwrap_or(std::ptr::null());

        // Create svn_string_t for input
        let input_str = unsafe {
            subversion_sys::svn_string_ncreate(
                value.as_ptr() as *const std::ffi::c_char,
                value.len(),
                pool.as_mut_ptr(),
            )
        };

        let mut new_value: *mut subversion_sys::svn_string_t = std::ptr::null_mut();
        let mut translated_to_utf8: i32 = 0;
        let mut translated_line_endings: i32 = 0;

        unsafe {
            let err = subversion_sys::svn_subst_translate_string2(
                &mut new_value,
                &mut translated_to_utf8,
                &mut translated_line_endings,
                input_str,
                encoding_ptr,
                if repair { 1 } else { 0 },
                pool.as_mut_ptr(),
                pool.as_mut_ptr(),
            );
            Error::from_raw(err)?;

            if new_value.is_null() || (*new_value).data.is_null() {
                return Ok((Vec::new(), false, false));
            }

            let data = std::slice::from_raw_parts((*new_value).data as *const u8, (*new_value).len);
            Ok((
                data.to_vec(),
                translated_to_utf8 != 0,
                translated_line_endings != 0,
            ))
        }
    })
}

/// Open a stream for reading a special file (symlink, etc.).
///
/// Returns a stream that reads the "normal form" content of the special file.
pub fn read_specialfile(path: &std::path::Path) -> Result<crate::io::Stream, Error<'static>> {
    let pool = apr::Pool::new();
    let path_cstr = std::ffi::CString::new(
        path.to_str()
            .ok_or_else(|| Error::from_message("Invalid path encoding"))?,
    )?;

    let mut stream: *mut subversion_sys::svn_stream_t = std::ptr::null_mut();

    unsafe {
        let err = subversion_sys::svn_subst_read_specialfile(
            &mut stream,
            path_cstr.as_ptr(),
            pool.as_mut_ptr(),
            pool.as_mut_ptr(),
        );
        Error::from_raw(err)?;

        Ok(crate::io::Stream::from_ptr(stream, pool))
    }
}

/// Create a stream for writing a special file (symlink, etc.).
///
/// Returns a stream that accepts content in "normal form" and creates
/// the special file when closed.
pub fn create_specialfile(path: &std::path::Path) -> Result<crate::io::Stream, Error<'static>> {
    let pool = apr::Pool::new();
    let path_cstr = std::ffi::CString::new(
        path.to_str()
            .ok_or_else(|| Error::from_message("Invalid path encoding"))?,
    )?;

    let mut stream: *mut subversion_sys::svn_stream_t = std::ptr::null_mut();

    unsafe {
        let err = subversion_sys::svn_subst_create_specialfile(
            &mut stream,
            path_cstr.as_ptr(),
            pool.as_mut_ptr(),
            pool.as_mut_ptr(),
        );
        Error::from_raw(err)?;

        Ok(crate::io::Stream::from_ptr(stream, pool))
    }
}

/// Create a detranslated stream (from working copy to repository form).
///
/// Returns a stream that translates content from the working copy format
/// to the repository format.
pub fn stream_detranslated(
    src_path: &std::path::Path,
    eol_style: EolStyle,
    eol_str: Option<&str>,
    always_repair_eols: bool,
    keywords: Option<&HashMap<String, String>>,
    special: bool,
) -> Result<crate::io::Stream, Error<'static>> {
    let pool = apr::Pool::new();
    let path_cstr = std::ffi::CString::new(
        src_path
            .to_str()
            .ok_or_else(|| Error::from_message("Invalid path encoding"))?,
    )?;

    let eol_ptr = match eol_str {
        Some(s) => apr::strings::pstrdup_raw(s, &pool)?,
        None => std::ptr::null(),
    };

    let keywords_hash = match keywords {
        Some(kw) => {
            let mut hash = apr::hash::Hash::new(&pool);
            for (k, v) in kw.iter() {
                unsafe {
                    let key_ptr = apr::strings::pstrdup_raw(k, &pool)?;
                    let val_ptr = apr::strings::pstrdup_raw(v, &pool)?;
                    hash.insert(
                        std::slice::from_raw_parts(key_ptr as *const u8, k.len()),
                        val_ptr as *mut std::ffi::c_void,
                    );
                }
            }
            unsafe { hash.as_mut_ptr() }
        }
        None => std::ptr::null_mut(),
    };

    let mut stream: *mut subversion_sys::svn_stream_t = std::ptr::null_mut();

    unsafe {
        let err = subversion_sys::svn_subst_stream_detranslated(
            &mut stream,
            path_cstr.as_ptr(),
            eol_style.into(),
            eol_ptr,
            if always_repair_eols { 1 } else { 0 },
            keywords_hash,
            if special { 1 } else { 0 },
            pool.as_mut_ptr(),
        );
        Error::from_raw(err)?;

        Ok(crate::io::Stream::from_ptr(stream, pool))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_eol_style_from_value() {
        let (style, eol) = eol_style_from_value("native");
        assert_eq!(style, EolStyle::Native);
        assert!(eol.is_some());

        let (style, eol) = eol_style_from_value("LF");
        assert_eq!(style, EolStyle::Fixed);
        assert_eq!(eol.as_deref(), Some("\n"));

        let (style, eol) = eol_style_from_value("CRLF");
        assert_eq!(style, EolStyle::Fixed);
        assert_eq!(eol.as_deref(), Some("\r\n"));

        let (style, eol) = eol_style_from_value("CR");
        assert_eq!(style, EolStyle::Fixed);
        assert_eq!(eol.as_deref(), Some("\r"));

        // Test invalid value returns Unknown
        let (style, eol) = eol_style_from_value("invalid");
        assert_eq!(style, EolStyle::Unknown);
        assert!(eol.is_none());
    }

    #[test]
    fn test_translation_required() {
        // No translation needed
        assert!(!translation_required(
            EolStyle::None,
            None,
            None,
            false,
            false
        ));

        // EOL translation may or may not be needed depending on platform
        // On Unix systems, native is \n, so \n -> native requires no translation
        let _result = translation_required(EolStyle::Native, Some("\n"), None, false, false);
        // Don't assert since this is platform-dependent

        // Keyword translation needed
        let mut keywords = HashMap::new();
        keywords.insert("Id".to_string(), "$Id$".to_string());
        assert!(translation_required(
            EolStyle::None,
            None,
            Some(&keywords),
            false,
            false
        ));
    }

    #[test]
    fn test_build_keywords() {
        let keywords = build_keywords(
            "Id Rev Author Date",
            Some("123"),
            Some("http://example.com/repo/file.txt"),
            Some(apr::time::Time::now()),
            Some("testuser"),
            Some("http://example.com/repo"),
        )
        .unwrap();

        assert!(keywords.contains_key("Id"));
        assert!(keywords.contains_key("Rev"));
        assert!(keywords.contains_key("Author"));
        assert!(keywords.contains_key("Date"));

        // Test with empty keyword string
        let empty_keywords = build_keywords("", None, None, None, None, None).unwrap();
        assert!(empty_keywords.is_empty());
    }

    #[test]
    fn test_keywords_differ() {
        let mut kw1 = HashMap::new();
        kw1.insert("Id".to_string(), "$Id$".to_string());
        kw1.insert("Rev".to_string(), "$Rev$".to_string());

        let mut kw2 = HashMap::new();
        kw2.insert("Id".to_string(), "$Id$".to_string());
        kw2.insert("Rev".to_string(), "$Rev$".to_string());

        // Same keywords
        assert!(!keywords_differ(Some(&kw1), Some(&kw2), true).unwrap());
        assert!(!keywords_differ(Some(&kw1), Some(&kw2), false).unwrap());

        // Different values
        kw2.insert("Rev".to_string(), "$Rev: 456$".to_string());
        assert!(keywords_differ(Some(&kw1), Some(&kw2), true).unwrap());
        assert!(!keywords_differ(Some(&kw1), Some(&kw2), false).unwrap());

        // Different keys
        kw2.insert("Date".to_string(), "$Date$".to_string());
        assert!(keywords_differ(Some(&kw1), Some(&kw2), true).unwrap());
        assert!(keywords_differ(Some(&kw1), Some(&kw2), false).unwrap());

        // One empty, one not
        assert!(keywords_differ(Some(&kw1), None, false).unwrap());
        assert!(keywords_differ(None, Some(&kw2), false).unwrap());

        // Both empty
        assert!(!keywords_differ(None, None, false).unwrap());
    }

    #[test]
    fn test_eol_style_conversions() {
        // Test From<svn_subst_eol_style_t> for EolStyle
        let unknown: EolStyle =
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_unknown.into();
        assert_eq!(unknown, EolStyle::Unknown);

        let none: EolStyle = subversion_sys::svn_subst_eol_style_svn_subst_eol_style_none.into();
        assert_eq!(none, EolStyle::None);

        let native: EolStyle =
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_native.into();
        assert_eq!(native, EolStyle::Native);

        let fixed: EolStyle = subversion_sys::svn_subst_eol_style_svn_subst_eol_style_fixed.into();
        assert_eq!(fixed, EolStyle::Fixed);

        // Test From<EolStyle> for svn_subst_eol_style_t
        let unknown_raw: subversion_sys::svn_subst_eol_style_t = EolStyle::Unknown.into();
        assert_eq!(
            unknown_raw,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_unknown
        );

        let none_raw: subversion_sys::svn_subst_eol_style_t = EolStyle::None.into();
        assert_eq!(
            none_raw,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_none
        );

        let native_raw: subversion_sys::svn_subst_eol_style_t = EolStyle::Native.into();
        assert_eq!(
            native_raw,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_native
        );

        let fixed_raw: subversion_sys::svn_subst_eol_style_t = EolStyle::Fixed.into();
        assert_eq!(
            fixed_raw,
            subversion_sys::svn_subst_eol_style_svn_subst_eol_style_fixed
        );
    }

    #[test]
    fn test_build_keywords_with_nulls() {
        // Test with all None values
        let keywords = build_keywords("Id", None, None, None, None, None).unwrap();
        assert!(keywords.contains_key("Id"));

        // Test with some None values
        let keywords = build_keywords("Rev", Some("100"), None, None, None, None).unwrap();
        assert!(keywords.contains_key("Rev"));
    }

    #[test]
    fn test_stream_translated() {
        // Create a simple memory stream with test content
        let content = b"Test content with $Id$ keyword\n";
        let source = crate::io::Stream::from(&content[..]);

        // Test basic stream translation without keywords
        stream_translated(source, Some("\n"), false, None, true).unwrap();
    }

    #[test]
    fn test_stream_translated_with_keywords() {
        let content = b"Test content with $Id$ keyword\n";
        let source = crate::io::Stream::from(&content[..]);

        let mut keywords = HashMap::new();
        keywords.insert("Id".to_string(), "test-id".to_string());

        stream_translated(source, Some("\n"), false, Some(&keywords), true).unwrap();
    }

    #[test]
    fn test_stream_translated_to_normal_form() {
        let content = b"Test content\r\n";
        let source = crate::io::Stream::from(&content[..]);

        stream_translated_to_normal_form(source, EolStyle::Native, Some("\n"), false, None)
            .unwrap();
    }

    #[test]
    fn test_stream_translated_to_normal_form_with_keywords() {
        let content = b"Test $Rev$ content\n";
        let source = crate::io::Stream::from(&content[..]);

        let mut keywords = HashMap::new();
        keywords.insert("Rev".to_string(), "123".to_string());

        stream_translated_to_normal_form(
            source,
            EolStyle::Fixed,
            Some("\n"),
            false,
            Some(&keywords),
        )
        .unwrap();
    }

    #[test]
    fn test_translate_stream() {
        let content = b"Test content\n";
        let mut source = crate::io::Stream::from(&content[..]);
        let mut dest = crate::io::Stream::empty();

        translate_stream(&mut source, &mut dest, Some("\n"), false, None, false).unwrap();
    }

    #[test]
    fn test_translate_stream_with_keywords() {
        let content = b"Test $Id$ content\n";
        let mut source = crate::io::Stream::from(&content[..]);
        let mut dest = crate::io::Stream::empty();

        let mut keywords = HashMap::new();
        keywords.insert("Id".to_string(), "test-id".to_string());

        translate_stream(
            &mut source,
            &mut dest,
            Some("\n"),
            false,
            Some(&keywords),
            true,
        )
        .unwrap();
    }

    #[test]
    fn test_translate_cstring() {
        let src = "Hello\nWorld\n";
        let result = translate_cstring(src, Some("\n"), false, None, false).unwrap();
        assert_eq!(result, "Hello\nWorld\n");
    }

    #[test]
    fn test_translate_cstring_with_eol() {
        let src = "Hello\nWorld\n";
        let result = translate_cstring(src, Some("\r\n"), false, None, false).unwrap();
        assert_eq!(result, "Hello\r\nWorld\r\n");
    }

    #[test]
    fn test_translate_string() {
        let value = b"Hello\nWorld\n";
        let (result, to_utf8, line_endings) = translate_string(value, None, false).unwrap();
        assert_eq!(result, b"Hello\nWorld\n");
        // Input is already UTF-8 with LF endings, so no translation needed
        assert!(!to_utf8);
        assert!(!line_endings);
    }

    #[test]
    fn test_copy_and_translate() {
        let td = tempfile::tempdir().unwrap();
        let src = td.path().join("src.txt");
        let dst = td.path().join("dst.txt");

        std::fs::write(&src, "Hello\nWorld\n").unwrap();

        copy_and_translate(&src, &dst, Some("\n"), false, None, false, false).unwrap();

        let content = std::fs::read_to_string(&dst).unwrap();
        assert_eq!(content, "Hello\nWorld\n");
    }
}