unicode_graph/
lib.rs

1// Copyright (c) 2016 Patrick Burroughs <celti@celti.name>.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0>, or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except
7// according to those terms.
8
9pub mod braille;
10
11pub mod graph {
12    use std::{char, error, fmt, result};
13
14    #[derive(Debug)]
15    pub struct ParseGraphError;
16
17    pub type Result<T> = result::Result<T, ParseGraphError>;
18
19    impl error::Error for ParseGraphError {
20        fn description(&self) -> &str { "failed to parse graph" }
21    }
22
23    impl fmt::Display for ParseGraphError {
24        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25            write!(f, "Parse error: failed to parse graph")
26        }
27    }
28
29    pub fn graph_to_strings(input: Vec<Vec<u32>>) -> Result<Vec<String>> {
30        let mut output = Vec::new();
31
32        for (index,line) in input.iter().enumerate() {
33            let mut string = String::new();
34
35            for block in line {
36                string.push(try!(char::from_u32(*block).ok_or(ParseGraphError)))
37            };
38
39            output.insert(index,string)
40        };
41
42        Ok(output)
43    }
44}