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

FilippoFantinato / algorithms-on-graphs / #6

12 Sep 2024 04:59AM UTC coverage: 42.254% (-44.8%) from 87.097%
#6

push

FilippoFantinato
Added basic cli

24 of 65 new or added lines in 4 files covered. (36.92%)

30 of 71 relevant lines covered (42.25%)

27.92 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 std::fs;
3
use std::ops::{Deref, DerefMut};
4
use std::path::PathBuf;
5
use std::rc::Rc;
6

7
use crate::algorithms::cycles;
8
use crate::graph::directed_graph::DirectedGraph;
9
use crate::graph::graph::Graph;
10

11
#[derive(clap::ValueEnum, Clone, Debug)]
12
enum Algorithm {
13
    IsAcyclic,
14
}
15

16
/// Simple program to greet a person
17
#[derive(Parser, Debug)]
18
#[command(version, about, long_about = None)]
19
pub struct Args {
20
    #[arg(short, long, value_enum)]
21
    algorithm: Algorithm,
22

23
    #[arg(short, long)]
24
    file: PathBuf,
25
}
26

NEW
27
pub fn run_cli() {
×
NEW
28
    let args: Args = Args::parse();
×
29

NEW
30
    match args.algorithm {
×
NEW
31
        Algorithm::IsAcyclic => {
×
NEW
32
            let g = read_graph(args.file);
×
NEW
33
            let res = cycles::is_acyclic::run(g.deref());
×
34

NEW
35
            println!("Is acylic result: {:}", res);
×
36
        }
37
    }
38
}
39

NEW
40
fn read_graph(path: PathBuf) -> Box<dyn Graph> {
×
NEW
41
    let lines = fs::read_to_string(path).unwrap();
×
NEW
42
    let mut lines = lines.lines();
×
NEW
43
    let mut header = lines
×
44
        .next()
NEW
45
        .unwrap_or_else(|| panic!("Invalid format"))
×
46
        .split_whitespace();
NEW
47
    let n = header
×
48
        .next()
NEW
49
        .map(|v| v.parse::<usize>().unwrap())
×
NEW
50
        .unwrap_or_else(|| panic!("Invalid format"));
×
NEW
51
    let mut g = Box::new(DirectedGraph::new(n));
×
52

NEW
53
    lines.for_each(|v| {
×
NEW
54
        let mut line = v.split_whitespace();
×
NEW
55
        let u = line
×
NEW
56
            .next()
×
NEW
57
            .map(|v| v.parse::<usize>().unwrap() - 1)
×
NEW
58
            .unwrap_or_else(|| panic!("Invalid format"));
×
NEW
59
        let v = line
×
NEW
60
            .next()
×
NEW
61
            .map(|v| v.parse::<usize>().unwrap() - 1)
×
NEW
62
            .unwrap_or_else(|| panic!("Invalid format"));
×
NEW
63
        let w = line
×
NEW
64
            .next()
×
NEW
65
            .map(|v| v.parse::<i128>().unwrap() - 1)
×
NEW
66
            .unwrap_or_else(|| panic!("Invalid format"));
×
67

NEW
68
        g.add_edge(u, v, w);
×
NEW
69
        g.add_edge(v, u, w);
×
70
    });
71

NEW
72
    return g;
×
73
}
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