Skip to content

Commit

Permalink
Add YAML formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Apr 14, 2024
1 parent 4f09a81 commit eb93687
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/formatters/yamlfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::rfs::RfsScan;

use super::stdfmt::DataFormatter;

pub struct YAMLDataFormatter<'a> {
rfs: &'a RfsScan,
}

impl YAMLDataFormatter<'_> {}

impl<'a> DataFormatter<'a> for YAMLDataFormatter<'_> {
fn format(&self) -> String {
let mut out = String::new();

for p in self.rfs.get_pkg_list() {
let pkl = self.rfs.get_pkg_license(p.to_owned());
out.push_str(format!("{}:\n", p).as_str());
if !pkl.get_id().is_empty() {
out.push_str(format!(" - {}\n", pkl.get_id()).as_str());
}
for l in pkl.get_other() {
if l.contains(' ') {
out.push_str(format!(" - \"{}\"\n", l).as_str());
} else {
out.push_str(format!(" - {}\n", l).as_str());
}
}
}

out.push_str("\n");
out
}

fn new(rfs: &'a RfsScan) -> Box<(dyn DataFormatter + 'a)> {
Box::new(YAMLDataFormatter { rfs })
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use formatters::{
csvfmt::CSVDataFormatter,
stdfmt::{DataFormatter, FormatterType},
txtfmt::TextDataFormatter,
yamlfmt::YAMLDataFormatter,
};
use rfs::RfsScan;

Expand All @@ -30,6 +31,7 @@ static VERSION: &str = "0.2";
fn display_licences(rfs: &RfsScan, typ: FormatterType) -> String {
let f: Box<dyn DataFormatter>;
match typ {
FormatterType::YAML => f = YAMLDataFormatter::new(rfs),
FormatterType::CSV => f = CSVDataFormatter::new(rfs),
FormatterType::TEXT => f = TextDataFormatter::new(rfs),
}
Expand Down

0 comments on commit eb93687

Please sign in to comment.