• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FilippoFantinato / algorithms-on-graphs / #13

15 Sep 2024 04:31PM UTC coverage: 56.522% (-2.0%) from 58.559%
#13

push

FilippoFantinato
Updated cli

0 of 16 new or added lines in 2 files covered. (0.0%)

65 of 115 relevant lines covered (56.52%)

114.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/cli/cli.rs
1
use clap::Parser;
2
use mockall::automock;
3
use std::any::Any;
4
use std::fs;
5
use std::ops::{Deref, DerefMut};
6
use std::path::PathBuf;
7
use std::rc::Rc;
8

9
use crate::algorithms::{cycles, minimum_spanning_tree};
10
use crate::graph::graph::{Graph, Weight};
11
use crate::graph::undirected_graph::UndirectedGraph;
12

13
#[derive(clap::ValueEnum, Clone, Debug)]
14
pub enum Algorithm {
15
    IsAcyclic,
16
    KruskalNaive,
17
}
18

19
#[derive(Parser, Debug)]
20
#[command(version, about, long_about = None)]
21
pub struct Args {
22
    #[arg(short, long, value_enum)]
23
    pub algorithm: Algorithm,
24

25
    #[arg(short, long)]
26
    pub file: PathBuf,
27
}
28

NEW
29
pub fn run_cli(args: &Args) -> Box<dyn Any> {
×
30
    match args.algorithm {
×
31
        Algorithm::IsAcyclic => {
NEW
32
            let g = read_graph(&args.file);
×
33
            let res = cycles::is_acyclic::run(g.deref());
×
34

NEW
35
            Box::new(res)
×
36
        }
37
        Algorithm::KruskalNaive => {
NEW
38
            let g = read_graph(&args.file);
×
39
            let path = minimum_spanning_tree::kruskal_naive::run(g.deref());
×
40

NEW
41
            Box::new(path)
×
42
        }
43
    }
44
}
45

NEW
46
fn read_graph(path: &PathBuf) -> Box<dyn Graph> {
×
47
    let lines = fs::read_to_string(path).unwrap();
×
48
    let mut lines = lines.lines();
×
49
    let mut header = lines
×
50
        .next()
51
        .unwrap_or_else(|| panic!("Invalid format"))
×
52
        .split_whitespace();
53
    let _n = header
×
54
        .next()
55
        .map(|v| v.parse::<usize>().unwrap())
×
56
        .unwrap_or_else(|| panic!("Invalid format"));
×
57
    let mut g = Box::new(UndirectedGraph::new());
×
58

59
    lines.for_each(|v| {
×
60
        let mut line = v.split_whitespace();
×
61
        let u = line
×
62
            .next()
×
63
            .map(|v| v.parse::<usize>().unwrap())
×
64
            .unwrap_or_else(|| panic!("Invalid format"));
×
65
        let v = line
×
66
            .next()
×
67
            .map(|v| v.parse::<usize>().unwrap())
×
68
            .unwrap_or_else(|| panic!("Invalid format"));
×
69
        let w = line
×
70
            .next()
×
71
            .map(|v| v.parse::<i128>().unwrap())
×
72
            .unwrap_or_else(|| panic!("Invalid format"));
×
73

74
        g.add_edge(u, v, w);
×
75
    });
76

77
    return g;
×
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc