blob: 8e1606042e9fc16087809d9c0b9534fbd7e329ec (
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
|
pub mod table;
pub mod csv;
pub mod json;
use async_trait::async_trait;
use std::collections::HashMap;
use anyhow::Result;
use crate::core::DependencyCollection;
#[async_trait]
pub trait OutputFormatter: Send + Sync {
async fn format(&self, dependencies: &DependencyCollection) -> Result<()>;
fn name(&self) -> &'static str;
}
pub struct FormatterRegistry {
formatters: HashMap<String, Box<dyn OutputFormatter>>,
}
impl FormatterRegistry {
pub fn new() -> Self {
Self {
formatters: HashMap::new(),
}
}
pub fn register<F: OutputFormatter + 'static>(&mut self, formatter: F) {
self.formatters.insert(formatter.name().to_string(), Box::new(formatter));
}
pub fn register_all(&mut self) {
self.register(table::TableFormatter::new());
self.register(csv::CsvFormatter::new());
self.register(json::JsonFormatter::new());
}
pub fn get_formatter(&self, name: &str) -> Option<&dyn OutputFormatter> {
self.formatters.get(name).map(|f| f.as_ref())
}
pub fn supported_formats(&self) -> Vec<&str> {
self.formatters.keys().map(|s| s.as_str()).collect()
}
}
impl Default for FormatterRegistry {
fn default() -> Self {
let mut registry = Self::new();
registry.register_all();
registry
}
}
|