blob: a425a54358151c8043f0c135ab59c9feef74bd4b (
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
|
use async_trait::async_trait;
use anyhow::Result;
use serde_json;
use super::OutputFormatter;
use crate::core::DependencyCollection;
pub struct JsonFormatter;
impl JsonFormatter {
pub fn new() -> Self {
Self
}
}
impl Default for JsonFormatter {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl OutputFormatter for JsonFormatter {
async fn format(&self, dependencies: &DependencyCollection) -> Result<()> {
// Output as line-delimited JSON (one JSON object per line)
for dep in dependencies.iter() {
let json = serde_json::to_string(dep)?;
println!("{}", json);
}
Ok(())
}
fn name(&self) -> &'static str {
"json"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::{Dependency, DependencyCollection};
#[tokio::test]
async fn test_json_formatter_empty() {
let formatter = JsonFormatter::new();
let dependencies = DependencyCollection::new();
let result = formatter.format(&dependencies).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_json_formatter_with_dependencies() {
let formatter = JsonFormatter::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 = JsonFormatter::new();
assert_eq!(formatter.name(), "json");
}
}
|