aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: d443b18daccc8c9d79447d956d40cae1a733365c (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt;
use std::fs::{read_to_string, write};
use std::io::{Error as IoError, Result as IoResult};
use std::path::PathBuf;
use std::str::FromStr;
use wmap_renderer::{Configuration, StageType, render_to_png, render_to_svg};

use thiserror::Error;

#[derive(Debug)]
enum ImageType {
    Png,
    Svg,
}

impl fmt::Display for ImageType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ImageType::Png => write!(f, "png"),
            ImageType::Svg => write!(f, "svg"),
        }
    }
}

#[derive(Error, Debug)]
pub enum WmapError {
    #[error("unknown image type.")]
    UnknownImageType,
    #[error("unknown stage type.")]
    UnknownStageType,
}

impl FromStr for ImageType {
    type Err = WmapError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "png" => Ok(ImageType::Png),
            "svg" => Ok(ImageType::Svg),
            _ => Err(Self::Err::UnknownImageType),
        }
    }
}

fn stage_from_str(s: &str) -> Option<StageType> {
    match s.to_lowercase().as_str() {
        "activities" => Some(StageType::Activities),
        "behavior" => Some(StageType::Behavior),
        "certainty" => Some(StageType::Certainty),
        "comparison" => Some(StageType::Comparison),
        "cynefin" => Some(StageType::Cynefin),
        "data" => Some(StageType::Data),
        "decision_drivers" => Some(StageType::DecisionDrivers),
        "efficiency" => Some(StageType::Efficiency),
        "failure" => Some(StageType::Failure),
        "focus_of_value" => Some(StageType::FocusOfValue),
        "knowledge" => Some(StageType::Knowledge),
        "knowledge_management" => Some(StageType::KnowledgeManagement),
        "market" => Some(StageType::Market),
        "market_action" => Some(StageType::MarketAction),
        "market_perception" => Some(StageType::MarketPerception),
        "perception_in_industry" => Some(StageType::PerceptionInIndustry),
        "practice" => Some(StageType::Practice),
        "publication_types" => Some(StageType::PublicationTypes),
        "ubiquity" => Some(StageType::Ubiquity),
        "understanding" => Some(StageType::Understanding),
        "user_perception" => Some(StageType::UserPerception),
        "evolution_stage" => Some(StageType::EvolutionStage),
        _ => None,
    }
}

struct Arguments {
    input: PathBuf,
    output: PathBuf,
    stage: StageType,
    image_type: ImageType,
}

fn parse_arguments() -> Result<Arguments, lexopt::Error> {
    use lexopt::prelude::*;

    let mut input = None;
    let mut output: String = "".to_string();
    let mut stage = StageType::Activities;
    let mut image_type = ImageType::Png;

    let mut argument_parser = lexopt::Parser::from_env();
    while let Some(argument) = argument_parser.next()? {
        match argument {
            Short('t') | Long("type") => {
                image_type = argument_parser.value()?.parse()?;
            }
            Short('s') | Long("stage") => {
                let stage_string: String = argument_parser.value()?.parse()?;
                stage = stage_from_str(&stage_string).ok_or("Invalid stage name")?
            }
            Short('o') | Long("output") => {
                output = argument_parser.value()?.parse()?;
            }
            Value(value) if input.is_none() => {
                input = Some(value.string()?);
            }
            Short('h') | Long("help") => {
                println!("Usage: wmap [-t|--type=png|svg] [-o=OUTPUT_FILE] INPUT_FILE");
                std::process::exit(0);
            }
            _ => return Err(argument.unexpected()),
        }
    }
    let input = PathBuf::from(input.ok_or("missing argument INPUT_FILE")?);
    let output = match output.is_empty() {
        true => input.with_extension(image_type.to_string()),
        false => PathBuf::from(output),
    };

    Ok(Arguments {
        input,
        output,
        stage,
        image_type,
    })
}

fn main() -> IoResult<()> {
    let arguments = parse_arguments().map_err(|_| IoError::other("Unable to parse arguments"))?;

    arguments.input.try_exists()?;
    let source = read_to_string(arguments.input)?;
    let map = wmap_parser::parse(&source);
    let configuration = Configuration::default();

    let image_data = match arguments.image_type {
        ImageType::Png => render_to_png(&map, arguments.stage, &configuration)
            .map_err(|_| IoError::other("Unable to render PNG"))?,
        ImageType::Svg => render_to_svg(&map, arguments.stage, &configuration)
            .map_err(|_| IoError::other("Unable to render SVG"))?
            .into_bytes(),
    };
    write(arguments.output, image_data)?;
    Ok(())
}