summaryrefslogtreecommitdiff
path: root/src/formatters/table.rs
blob: 4468bd0e21a0800a808acdf5a9371814c18c05eb (plain)
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
use async_trait::async_trait;
use anyhow::Result;
use tabled::{Table, Tabled};

use super::OutputFormatter;
use crate::core::DependencyCollection;

pub struct TableFormatter;

impl TableFormatter {
    pub fn new() -> Self {
        Self
    }
}

impl Default for TableFormatter {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl OutputFormatter for TableFormatter {
    async fn format(&self, dependencies: &DependencyCollection) -> Result<()> {
        if dependencies.is_empty() {
            println!("No dependencies found");
            return Ok(());
        }

        let rows: Vec<DependencyRow> = dependencies
            .iter()
            .map(|dep| DependencyRow {
                name: dep.name.clone(),
                version: dep.version.clone(),
                licenses: dep.license_display(),
                location: dep.location.to_string(),
            })
            .collect();

        let table = Table::new(rows);
        println!("{}", table);

        Ok(())
    }

    fn name(&self) -> &'static str {
        "table"
    }
}

#[derive(Tabled)]
struct DependencyRow {
    #[tabled(rename = "Name")]
    name: String,
    #[tabled(rename = "Version")]
    version: String,
    #[tabled(rename = "Licenses")]
    licenses: String,
    #[tabled(rename = "Location")]
    location: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{Dependency, DependencyCollection};

    #[tokio::test]
    async fn test_table_formatter_empty() {
        let formatter = TableFormatter::new();
        let dependencies = DependencyCollection::new();
        
        let result = formatter.format(&dependencies).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_table_formatter_with_dependencies() {
        let formatter = TableFormatter::new();
        let mut dependencies = DependencyCollection::new();
        
        let dep = Dependency::new("test".to_string(), "1.0.0".to_string())
            .with_license("MIT".to_string());
        dependencies.add(dep);
        
        let result = formatter.format(&dependencies).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_formatter_name() {
        let formatter = TableFormatter::new();
        assert_eq!(formatter.name(), "table");
    }
}