babbel_bencode 0.2.2

Fast, binary-safe BitTorrent Bencode parser, serializer, and DOM in pure Rust
Documentation
//! Utility library for handling torrent files and related operations.
//! Provides functionality for file system operations specific to torrent files.

use std::fs;
use std::path::Path;

#[cfg(test)]
use std::fs::File;
#[cfg(test)]
use std::io::Write;

/// Returns a list of torrent file paths from the specified directory.
///
/// # Arguments
///
/// * `file_path` - Path to the directory containing torrent files
///
/// # Returns
///
/// A vector of strings containing paths to all .torrent files in the directory
pub fn get_torrent_file_list(file_path: &str) -> Vec<String> {
    let files_dir = Path::new(file_path);
    if !files_dir.exists() {
        let _ = fs::create_dir_all(files_dir);
        return vec![];
    }

    babbel_core::file::list_files_by_extension(file_path, "torrent")
        .unwrap_or_default()
        .into_iter()
        .map(|p| p.to_string_lossy().into_owned())
        .collect()
}

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

    fn create_test_dir(dir: &str) {
        fs::create_dir_all(dir).expect("Failed to create test directory");
    }

    fn create_test_file(path: &str) {
        let mut file = File::create(path).expect("Failed to create test file");
        file.write_all(b"test content")
            .expect("Failed to write to test file");
    }

    fn cleanup_test_dir(dir: &str) {
        fs::remove_dir_all(dir).expect("Failed to clean up test directory");
    }

    #[test]
    fn test_empty_directory() {
        let test_dir = "test_empty";
        create_test_dir(test_dir);

        let files = get_torrent_file_list(test_dir);
        assert!(files.is_empty());

        cleanup_test_dir(test_dir);
    }

    #[test]
    fn test_with_torrent_files() {
        let test_dir = "test_torrents";
        create_test_dir(test_dir);

        create_test_file(&format!("{}/test1.torrent", test_dir));
        create_test_file(&format!("{}/test2.torrent", test_dir));
        create_test_file(&format!("{}/not_torrent.txt", test_dir));

        let files = get_torrent_file_list(test_dir);
        assert_eq!(files.len(), 2);
        assert!(files.iter().all(|f| f.ends_with(".torrent")));

        cleanup_test_dir(test_dir);
    }

    #[test]
    fn test_nonexistent_directory() {
        let test_dir = "test_nonexistent_dir";
        let _ = fs::remove_dir_all(test_dir);
        let files = get_torrent_file_list(test_dir);
        assert!(files.is_empty());
        assert!(Path::new(test_dir).exists());
        let _ = fs::remove_dir_all(test_dir);
    }
}