Minimal version

This commit is contained in:
Aaronepower 2016-08-01 12:11:40 +01:00
parent b2790c4eee
commit e4435402de
8 changed files with 108 additions and 97 deletions

10
Cargo.lock generated
View File

@ -12,7 +12,7 @@ dependencies = [
"serde_codegen 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.1.30 (git+https://github.com/alexcrichton/toml-rs)",
"walkdir 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -69,7 +69,7 @@ dependencies = [
[[package]]
name = "dtoa"
version = "0.2.1"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -208,7 +208,7 @@ name = "serde_json"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dtoa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -293,9 +293,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
source = "git+https://github.com/alexcrichton/toml-rs#dd109bc9e9b8a6a5585e8bf1093ec84dcad61309"
dependencies = [
"serde 0.7.15 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -55,7 +55,7 @@ version = "~0.4.0"
default-features = false
features = ["serde"]
optional = true
version = "~0.1.30"
git = "https://github.com/alexcrichton/toml-rs"
[features]
all = ["json", "cbor", "toml-io", "yaml"]

View File

@ -1,16 +1,15 @@
#[cfg(feature = "io")]
extern crate serde_codegen;
use std::env;
use std::path::Path;
pub fn main() {
fn main() {
expand();
}
#[cfg(feature = "io")]
fn expand() {
use std::env;
use std::path::Path;
let out_dir = env::var_os("OUT_DIR").unwrap();
let src = Path::new("src/lib/lib.rs.in");

View File

@ -6,8 +6,8 @@ use sort::Sort::*;
use stats::Stats;
/// Struct representing a single Language.
#[cfg_attr(feature = "io", derive(Clone, Debug, Deserialize, Default, Eq, Ord, PartialEq, PartialOrd, Serialize))]
#[cfg(feature = "io")]
#[cfg_attr(feature = "io", derive(Clone, Debug, Deserialize, Default, Eq, Ord, PartialEq, PartialOrd, Serialize))]
pub struct Language {
/// Number of blank lines.
pub blanks: usize,

View File

@ -61,6 +61,8 @@ pub enum LanguageType {
FortranModern,
/// Go
Go,
/// Handlebars
Handlebars,
/// Haskell
Haskell,
/// Html
@ -198,6 +200,7 @@ impl LanguageType {
FortranLegacy => "FORTRAN Legacy",
FortranModern => "FORTRAN Modern",
Go => "Go",
Handlebars => "Handlebars",
Haskell => "Haskell",
Html => "HTML",
Idris => "Idris",
@ -283,6 +286,7 @@ impl LanguageType {
"f03" | "f08" | "f90" | "f95" => Some(FortranModern),
"go" => Some(Go),
"h" => Some(CHeader),
"hbs" | "handlebars" => Some(Handlebars),
"hh" | "hpp" | "hxx" => Some(CppHeader),
"hs" => Some(Haskell),
"html" => Some(Html),
@ -371,6 +375,7 @@ impl From<String> for LanguageType {
"FortranLegacy" => FortranLegacy,
"FortranModern" => FortranModern,
"Go" => Go,
"Handlebars" => Handlebars,
"Haskell" => Haskell,
"Html" => Html,
"Idris" => Idris,

View File

@ -9,8 +9,8 @@ use std::io::Read;
use std::iter::IntoIterator;
use std::ops::{AddAssign, Deref, DerefMut};
#[cfg(feature = "cbor")]
use serde_cbor;
// #[cfg(feature = "cbor")]
// use serde_cbor;
#[cfg(feature = "json")]
use serde_json;
#[cfg(feature = "yaml")]
@ -24,9 +24,11 @@ use super::{Language, LanguageType};
use super::LanguageType::*;
use stats::Stats;
const CBOR_ERROR: &'static str = "Tokei was not compiled with the `cbor` flag.";
#[cfg(not(feature = "json"))]
const JSON_ERROR: &'static str = "Tokei was not compiled with the `json` flag.";
#[cfg(not(feature = "toml-io"))]
const TOML_ERROR: &'static str = "Tokei was not compiled with the `toml-io` flag.";
#[cfg(not(feature = "yaml"))]
const YAML_ERROR: &'static str = "Tokei was not compiled with the `yaml` flag.";
/// A collection of existing languages([_List of Languages_](https://github.com/Aaronepower/tokei#supported-languages))
@ -37,34 +39,34 @@ pub struct Languages {
impl Languages {
/// Creates a `Languages` struct from cbor.
///
/// ```
/// # extern crate tokei;
/// # use tokei::*;
/// # extern crate rustc_serialize;
/// # use rustc_serialize::hex::FromHex;
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e7473\
/// 0065737461747381a566626c616e6b730564636f64650c68636f6d6d656e74730065\
/// 6c696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e\
/// 6573116b746f74616c5f66696c657301";
///
/// let mut languages = Languages::from_cbor(&*cbor.from_hex().unwrap()).unwrap();
/// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
/// # }
/// ```
#[cfg(feature = "cbor")]
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
let map = try!(serde_cbor::from_slice(cbor.into()));
// /// Creates a `Languages` struct from cbor.
// ///
// /// ```
// /// # extern crate tokei;
// /// # use tokei::*;
// /// # extern crate rustc_serialize;
// /// # use rustc_serialize::hex::FromHex;
// /// # fn main () {
// /// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e7473\
// /// 0065737461747381a566626c616e6b730564636f64650c68636f6d6d656e74730065\
// /// 6c696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e\
// /// 6573116b746f74616c5f66696c657301";
// ///
// /// let mut languages = Languages::from_cbor(&*cbor.from_hex().unwrap()).unwrap();
// /// assert_eq!(12, languages.get_mut(&LanguageType::Rust).unwrap().code);
// /// # }
// /// ```
// #[cfg(feature = "cbor")]
// pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
// let map = try!(serde_cbor::from_slice(cbor.into()));
//
// Ok(Self::from_previous(map))
// }
Ok(Self::from_previous(map))
}
#[cfg(not(feature = "cbor"))]
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> ! {
panic!(CBOR_ERROR)
}
// #[cfg(not(feature = "cbor"))]
// pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> ! {
// panic!(CBOR_ERROR)
// }
/// Creates a `Languages` struct from json.
///
@ -98,6 +100,7 @@ impl Languages {
}
#[cfg(not(feature = "json"))]
#[allow(unused_variables)]
pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> ! {
panic!(JSON_ERROR)
}
@ -134,10 +137,12 @@ impl Languages {
}
#[cfg(not(feature = "yaml"))]
#[allow(unused_variables)]
pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> ! {
panic!(YAML_ERROR)
}
#[cfg(feature = "io")]
fn from_previous(map: BTreeMap<LanguageType, Language>) -> Self {
let mut _self = Self::new();
@ -292,6 +297,7 @@ impl Languages {
FortranLegacy => Language::new_single(vec!["c","C","!","*"]),
FortranModern => Language::new_single(vec!["!"]),
Go => Language::new_c(),
Handlebars => Language::new_multi(vec![("<!--", "-->"), ("{{!", "}}")]),
Haskell => Language::new_single(vec!["--"]),
Html => Language::new_html(),
Idris => Language::new(vec!["--"], vec![("{-", "-}")]),
@ -379,35 +385,36 @@ impl Languages {
map
}
/// Converts `Languages` to CBOR.
///
/// ```no_run
/// extern crate tokei;
/// # use tokei::*;
/// extern crate rustc_serialize;
/// use rustc_serialize::hex::ToHex;
///
/// # fn main () {
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\
/// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\
/// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\
/// 3116b746f74616c5f66696c657301";
///
/// let mut languages = Languages::new();
/// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
///
/// assert_eq!(cbor, languages.to_cbor().unwrap().to_hex());
/// # }
/// ```
#[cfg(feature = "cbor")]
pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
serde_cbor::to_vec(&self.remove_empty())
}
// /// Converts `Languages` to CBOR.
// ///
// /// ```no_run
// /// extern crate tokei;
// /// # use tokei::*;
// /// extern crate rustc_serialize;
// /// use rustc_serialize::hex::ToHex;
// ///
// /// # fn main () {
// /// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\
// /// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\
// /// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\
// /// 3116b746f74616c5f66696c657301";
// ///
// /// let mut languages = Languages::new();
// /// languages.get_statistics(&*vec!["src/lib/build.rs"], &*vec![".git"]);
// ///
// /// assert_eq!(cbor, languages.to_cbor().unwrap().to_hex());
// /// # }
// /// ```
// #[cfg(feature = "cbor")]
// pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
// serde_cbor::to_vec(&self.remove_empty())
// }
#[cfg(not(feature = "cbor"))]
pub fn to_cbor(&self) -> ! {
panic!(CBOR_ERROR)
}
// #[cfg(not(feature = "cbor"))]
// #[allow(unused_variables)]
// pub fn to_cbor(&self) -> ! {
// panic!(CBOR_ERROR)
// }
/// Converts `Languages` to JSON.
///
@ -442,6 +449,7 @@ impl Languages {
}
#[cfg(not(feature = "json"))]
#[allow(unused_variables)]
pub fn to_json(&self) -> ! {
panic!(JSON_ERROR)
}
@ -452,6 +460,7 @@ impl Languages {
}
#[cfg(not(feature = "toml-io"))]
#[allow(unused_variables)]
pub fn to_toml(&self) -> ! {
panic!(TOML_ERROR)
}
@ -484,6 +493,7 @@ impl Languages {
}
#[cfg(not(feature = "yaml"))]
#[allow(unused_variables)]
pub fn to_yaml(&self) -> ! {
panic!(YAML_ERROR)
}

View File

@ -52,13 +52,13 @@ extern crate glob;
extern crate rayon;
#[cfg(feature = "io")]
extern crate serde;
#[cfg(feature = "cbor")]
extern crate serde_cbor;
// #[cfg(feature = "cbor")]
// extern crate serde_cbor;
#[cfg(feature = "json")]
extern crate serde_json;
#[cfg(feature = "yaml")]
extern crate serde_yaml;
#[cfg(feature = "toml")]
#[cfg(feature = "toml-io")]
extern crate toml;
extern crate walkdir;

View File

@ -4,29 +4,28 @@
#[macro_use]
extern crate clap;
#[cfg(feature = "cbor")]
extern crate serde_cbor;
// #[cfg(feature = "cbor")]
// extern crate serde_cbor;
#[cfg(feature = "json")]
extern crate serde_json;
#[cfg(feature = "yaml")]
extern crate serde_yaml;
#[cfg(feature = "toml-io")]
extern crate toml;
#[cfg(feature = "cbor")]
extern crate rustc_serialize;
// #[cfg(feature = "cbor")]
// extern crate rustc_serialize;
extern crate tokei;
use std::borrow::Cow;
#[cfg(feature = "io")]
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Read;
use std::thread;
use std::time::Duration;
use std::sync::mpsc::channel;
use clap::App;
#[cfg(feature = "cbor")]
use rustc_serialize::hex::FromHex;
// #[cfg(feature = "cbor")]
// use rustc_serialize::hex::FromHex;
use tokei::{Languages, Language, LanguageType};
use tokei::Sort::*;
@ -37,6 +36,7 @@ const COMMENTS: &'static str = "comments";
const CODE: &'static str = "code";
const FILES: &'static str = "files";
const LINES: &'static str = "lines";
#[cfg(not(feature = "io"))]
const OUTPUT_ERROR: &'static str = "This version of tokei was compiled without any serialization
formats, to enable serialization, reinstall tokei with the features flag.
@ -46,16 +46,13 @@ const OUTPUT_ERROR: &'static str = "This version of tokei was compiled without a
JSON:
cargo install tokei --features json
CBOR:
cargo install tokei --features cbor
TOML:
cargo install tokei --features toml
YAML:
cargo install toke --features yaml
You can also have any mix of json cbor toml, or yaml.
You can also have any mix of json, toml, or yaml.
";
fn main() {
@ -159,7 +156,7 @@ fn main() {
}
if let Some(format) = output_option {
match_output(&format);
match_output(&format, &languages);
} else if let Some(sort_category) = sort_option {
for (_, ref mut language) in &mut languages {
@ -217,7 +214,10 @@ fn main() {
#[cfg(feature = "io")]
fn add_input(input: &str, map: &mut BTreeMap<LanguageType, Language>) {
fn add_input(input: &str, languages: &mut Languages) {
use std::fs::File;
use std::io::Read;
let map = match File::open(input) {
Ok(mut file) => {
let contents = {
@ -242,13 +242,14 @@ fn add_input(input: &str, map: &mut BTreeMap<LanguageType, Language>) {
};
if let Some(map) = map {
languages += map;
*languages += map;
}
}
#[cfg(not(feature = "io"))]
fn add_input(input: &str, map: &mut BTreeMap<LanguageType, Language>) -> ! {
#[allow(unused_variables)]
fn add_input(input: &str, map: &mut Languages) -> ! {
panic!(OUTPUT_ERROR)
}
@ -263,12 +264,6 @@ pub fn convert_input(contents: String) -> Option<BTreeMap<LanguageType, Language
Some(result)
} else if let Ok(result) = serde_yaml::from_str(&*contents) {
Some(result)
} else if let Ok(hex) = contents.from_hex() {
if let Ok(result) = serde_cbor::from_slice(&*hex) {
Some(result)
} else {
None
}
} else if let Some(result) = toml::decode_str(&*contents) {
Some(result)
} else {
@ -277,7 +272,7 @@ pub fn convert_input(contents: String) -> Option<BTreeMap<LanguageType, Language
}
#[cfg(feature = "io")]
fn match_output(format: &str) {
fn match_output(format: &str, languages: &Languages) {
match format {
"cbor" => {
// let cbor: Vec<u8> = languages.to_cbor().unwrap();
@ -294,12 +289,14 @@ fn match_output(format: &str) {
}
#[cfg(not(feature = "io"))]
fn match_output(format: &str) -> ! {
#[allow(unused_variables)]
fn match_output(format: &str, languages: &Languages) -> ! {
panic!(OUTPUT_ERROR)
}
#[cfg(not(feature = "io"))]
#[allow(unused_variables)]
pub fn convert_input(contents: String) -> ! {
panic!(OUTPUT_ERROR);
}