summaryrefslogtreecommitdiffstats
path: root/src/frontend/list.rs
blob: 9e06828aa144ec25207dad5a076d2d06a9c99d19 (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
use std::io::Stdout;
use std::io::Write;
use std::ops::Deref;

use anyhow::Error;
use anyhow::Result;
use librepology::v1::types::Name;
use librepology::v1::types::Package;
use librepology::v1::types::Problem;

use crate::frontend::Frontend;

pub struct ListFrontend(Stdout);

/// A Frontend that prints the data in a human-readable way but without ASCII-art.
///
/// It seperates the values with dashes ("-") for a slightly better reading experience.
impl ListFrontend {
    pub fn new(stdout: Stdout) -> Self {
        ListFrontend(stdout)
    }
}

impl Frontend for ListFrontend {
    fn list_packages(&self, packages: Vec<Package>) -> Result<()> {
        let mut outlock = self.0.lock();

        packages.iter().fold(Ok(()), |accu, package| {
            accu.and_then(|_| {
                let status = if let Some(stat) = package.status() {
                    stat.deref().to_string()
                } else {
                    String::from("No status")
                }; // not optimal, but works for now.

                let url = if let Some(url) = package.www() {
                    if let Some(url) = url.first() {
                        url.deref().to_string()
                    } else {
                        String::from("")
                    }
                } else {
                    String::from("")
                }; // not optimal, but works for now

                writeln!(
                    outlock,
                    "{name:10} - {version:8} - {repo:15} - {status:5} - {www}",
                    name = package
                        .any_name()
                        .map(Name::deref)
                        .map(String::deref)
                        .unwrap_or_else(|| "<unknown>"),
                    version = package.version().deref(),
                    repo = package.repo().deref(),
                    status = status,
                    www = url
                )
                .map(|_| ())
                .map_err(Error::from)
            })
        })
    }

    fn list_problems(&self, problems: Vec<Problem>) -> Result<()> {
        let mut outlock = self.0.lock();

        problems.iter().fold(Ok(()), |accu, problem| {
            accu.and_then(|_| {
                writeln!(
                    outlock,
                    "{repo:10} - {name:10} - {effname:10} - {maintainer:15} - {desc}",
                    repo = problem.repo().deref(),
                    name = problem.name().deref(),
                    effname = problem.effname().deref(),
                    maintainer = problem.maintainer().deref(),
                    desc = problem.problem_description()
                )
                .map(|_| ())
                .map_err(Error::from)
            })
        })
    }

}