mirror of
https://github.com/XAMPPRocky/tokei
synced 2024-08-28 19:59:31 +00:00
added documentation
This commit is contained in:
parent
b48de870d6
commit
9a3dbf1a4a
|
@ -17,6 +17,8 @@ serializable_enum! {
|
||||||
ActionScript,
|
ActionScript,
|
||||||
/// Assembly
|
/// Assembly
|
||||||
Assembly,
|
Assembly,
|
||||||
|
/// Autoconf
|
||||||
|
Autoconf,
|
||||||
/// Bash
|
/// Bash
|
||||||
Bash,
|
Bash,
|
||||||
/// Batch
|
/// Batch
|
||||||
|
@ -159,7 +161,7 @@ serializable_enum! {
|
||||||
Yaml,
|
Yaml,
|
||||||
/// Zsh
|
/// Zsh
|
||||||
Zsh,
|
Zsh,
|
||||||
/// __Total
|
#[doc(hidden)]
|
||||||
__Total,
|
__Total,
|
||||||
}
|
}
|
||||||
LanguageTypeVisitor
|
LanguageTypeVisitor
|
||||||
|
@ -180,6 +182,7 @@ impl_as_ref_from_str! {
|
||||||
LanguageType {
|
LanguageType {
|
||||||
ActionScript => "ActionScript",
|
ActionScript => "ActionScript",
|
||||||
Assembly => "Assembly",
|
Assembly => "Assembly",
|
||||||
|
Autoconf => "Autoconf",
|
||||||
Bash => "BASH",
|
Bash => "BASH",
|
||||||
Batch => "Batch",
|
Batch => "Batch",
|
||||||
C => "C",
|
C => "C",
|
||||||
|
@ -258,10 +261,18 @@ impl_as_ref_from_str! {
|
||||||
|
|
||||||
|
|
||||||
impl LanguageType {
|
impl LanguageType {
|
||||||
|
/// Returns the display name of a language.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let bash = LanguageType::Bash;
|
||||||
|
///
|
||||||
|
/// assert_eq!(bash.name(), "BASH");
|
||||||
|
/// ```
|
||||||
pub fn name(&self) -> &'static str {
|
pub fn name(&self) -> &'static str {
|
||||||
match *self {
|
match *self {
|
||||||
ActionScript => "ActionScript",
|
ActionScript => "ActionScript",
|
||||||
Assembly => "Assembly",
|
Assembly => "Assembly",
|
||||||
|
Autoconf => "Autoconf",
|
||||||
Bash => "BASH",
|
Bash => "BASH",
|
||||||
Batch => "Batch",
|
Batch => "Batch",
|
||||||
C => "C",
|
C => "C",
|
||||||
|
@ -337,6 +348,13 @@ impl LanguageType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get language from it's file extension.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let rust = LanguageType::from_extension("rs");
|
||||||
|
///
|
||||||
|
/// assert_eq!(rust, LanguageType::Rust);
|
||||||
|
/// ```
|
||||||
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
|
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
|
||||||
if let Some(extension) = get_extension(entry) {
|
if let Some(extension) = get_extension(entry) {
|
||||||
match &*extension {
|
match &*extension {
|
||||||
|
@ -365,6 +383,7 @@ impl LanguageType {
|
||||||
"hs" => Some(Haskell),
|
"hs" => Some(Haskell),
|
||||||
"html" => Some(Html),
|
"html" => Some(Html),
|
||||||
"idr" | "lidr" => Some(Idris),
|
"idr" | "lidr" => Some(Idris),
|
||||||
|
"in" => Some(Autoconf),
|
||||||
"jai" => Some(Jai),
|
"jai" => Some(Jai),
|
||||||
"java" => Some(Java),
|
"java" => Some(Java),
|
||||||
"jl" => Some(Julia),
|
"jl" => Some(Julia),
|
||||||
|
@ -396,7 +415,7 @@ impl LanguageType {
|
||||||
"r" => Some(R),
|
"r" => Some(R),
|
||||||
"rake" | "rb" => Some(Ruby),
|
"rake" | "rb" => Some(Ruby),
|
||||||
"rhtml" => Some(RubyHtml),
|
"rhtml" => Some(RubyHtml),
|
||||||
"rs" | "in" => Some(Rust),
|
"rs" => Some(Rust),
|
||||||
"s" => Some(Assembly),
|
"s" => Some(Assembly),
|
||||||
"sass" | "scss" => Some(Sass),
|
"sass" | "scss" => Some(Sass),
|
||||||
"sc" | "scala" => Some(Scala),
|
"sc" | "scala" => Some(Scala),
|
||||||
|
@ -426,6 +445,7 @@ impl From<String> for LanguageType {
|
||||||
match &*from {
|
match &*from {
|
||||||
"ActionScript" => ActionScript,
|
"ActionScript" => ActionScript,
|
||||||
"Assembly" => Assembly,
|
"Assembly" => Assembly,
|
||||||
|
"Autoconf" => Autoconf,
|
||||||
"Bash" => Bash,
|
"Bash" => Bash,
|
||||||
"Batch" => Batch,
|
"Batch" => Batch,
|
||||||
"C" => C,
|
"C" => C,
|
||||||
|
|
|
@ -19,6 +19,7 @@ use super::{Language, LanguageType};
|
||||||
use super::LanguageType::*;
|
use super::LanguageType::*;
|
||||||
use stats::Stats;
|
use stats::Stats;
|
||||||
|
|
||||||
|
/// A collection of existing languages([_List of Languages_](https://github.com/Aaronepower/tokei#supported-languages))
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Languages {
|
pub struct Languages {
|
||||||
inner: BTreeMap<LanguageType, Language>,
|
inner: BTreeMap<LanguageType, Language>,
|
||||||
|
@ -26,6 +27,7 @@ pub struct Languages {
|
||||||
|
|
||||||
|
|
||||||
impl Languages {
|
impl Languages {
|
||||||
|
/// Creates a `Languages` struct from cbor.
|
||||||
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
|
pub fn from_cbor<'a, I: Into<&'a [u8]>>(cbor: I) -> serde_cbor::Result<Self> {
|
||||||
let map = try!(serde_cbor::from_slice(cbor.into()));
|
let map = try!(serde_cbor::from_slice(cbor.into()));
|
||||||
|
|
||||||
|
@ -33,12 +35,43 @@ impl Languages {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Creates a `Languages` struct from json.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#;
|
||||||
|
/// let languages = Languages::from_json(&json);
|
||||||
|
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12)
|
||||||
|
/// ```
|
||||||
pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> serde_json::Result<Self> {
|
pub fn from_json<'a, I: Into<&'a [u8]>>(json: I) -> serde_json::Result<Self> {
|
||||||
let map = try!(serde_json::from_slice(json.into()));
|
let map = try!(serde_json::from_slice(json.into()));
|
||||||
|
|
||||||
Ok(Self::from_previous(map))
|
Ok(Self::from_previous(map))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a `Languages` struct from json.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let yaml = r#"
|
||||||
|
/// ---
|
||||||
|
/// "Rust":
|
||||||
|
/// "blanks": 5
|
||||||
|
/// "code": 12
|
||||||
|
/// "comments": 0
|
||||||
|
/// "lines": 17
|
||||||
|
/// "stats":
|
||||||
|
/// -
|
||||||
|
/// "blanks": 5
|
||||||
|
/// "code": 12
|
||||||
|
/// "comments": 0
|
||||||
|
/// "lines": 17
|
||||||
|
/// "name": ".\\src\\lib\\build.rs"
|
||||||
|
/// "total_files": 1
|
||||||
|
/// "#;
|
||||||
|
///
|
||||||
|
/// let languages = Languages::from_yaml(&yaml);
|
||||||
|
///
|
||||||
|
/// assert!(languages.get_mut(&LanguageType::Rust).unwrap().code == 12)
|
||||||
|
/// ```
|
||||||
pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> serde_yaml::Result<Self> {
|
pub fn from_yaml<'a, I: Into<&'a [u8]>>(yaml: I) -> serde_yaml::Result<Self> {
|
||||||
let map = try!(serde_yaml::from_slice(yaml.into()));
|
let map = try!(serde_yaml::from_slice(yaml.into()));
|
||||||
|
|
||||||
|
@ -49,13 +82,22 @@ impl Languages {
|
||||||
let mut _self = Self::new();
|
let mut _self = Self::new();
|
||||||
|
|
||||||
for (name, input_language) in map {
|
for (name, input_language) in map {
|
||||||
if let Some(language) = _self.get_mut(&LanguageType::from(name)) {
|
if let Some(language) = _self.get_mut(&name) {
|
||||||
*language += input_language;
|
*language += input_language;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_self
|
_self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get statistics from the list of paths provided, and a list ignored keywords
|
||||||
|
/// to ignore paths containing them.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// let languages = Languages::new();
|
||||||
|
/// languages.get_statistics(&vec!["."], &vec![".git", "target"]);
|
||||||
|
///
|
||||||
|
/// println!("{:?}", languages);
|
||||||
|
/// ```
|
||||||
pub fn get_statistics<'a, I>(&mut self, paths: I, ignored: I)
|
pub fn get_statistics<'a, I>(&mut self, paths: I, ignored: I)
|
||||||
where I: Into<Cow<'a, [&'a str]>>
|
where I: Into<Cow<'a, [&'a str]>>
|
||||||
{
|
{
|
||||||
|
@ -157,11 +199,17 @@ impl Languages {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructs a new, blank `Languages`.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let languages = Languages::new();
|
||||||
|
/// ```
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
use super::LanguageType::*;
|
use super::LanguageType::*;
|
||||||
let map = btreemap! {
|
let map = btreemap! {
|
||||||
ActionScript => Language::new_c(),
|
ActionScript => Language::new_c(),
|
||||||
Assembly => Language::new_single(vec![";"]),
|
Assembly => Language::new_single(vec![";"]),
|
||||||
|
Autoconf => Language::new_single(vec!["#", "dnl"]),
|
||||||
Bash => Language::new_hash(),
|
Bash => Language::new_hash(),
|
||||||
Batch => Language::new_single(vec!["REM"]),
|
Batch => Language::new_single(vec!["REM"]),
|
||||||
C => Language::new_c(),
|
C => Language::new_c(),
|
||||||
|
@ -246,7 +294,7 @@ impl Languages {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_empty(&self) -> BTreeMap<LanguageType, Language> {
|
fn remove_empty(&self) -> BTreeMap<LanguageType, Language> {
|
||||||
let mut map: BTreeMap<LanguageType, Language> = BTreeMap::new();
|
let mut map = BTreeMap::new();
|
||||||
|
|
||||||
for (name, language) in &self.inner {
|
for (name, language) in &self.inner {
|
||||||
if !language.is_empty() {
|
if !language.is_empty() {
|
||||||
|
@ -256,12 +304,62 @@ impl Languages {
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts `Languages` to CBOR.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// extern crate rustc_serialize;
|
||||||
|
/// use rustc_serialize::ToHex;
|
||||||
|
///
|
||||||
|
/// let cbor = "a16452757374a666626c616e6b730564636f64650c68636f6d6d656e74730\
|
||||||
|
/// 065737461747381a566626c616e6b730564636f64650c68636f6d6d656e747300656c\
|
||||||
|
/// 696e657311646e616d65722e5c7372635c6c69625c6275696c642e7273656c696e657\
|
||||||
|
/// 3116b746f74616c5f66696c657301";
|
||||||
|
///
|
||||||
|
/// let languages = Languages::new();
|
||||||
|
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(cbor, languages.to_cbor().to_hex());
|
||||||
|
/// ```
|
||||||
pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
|
pub fn to_cbor(&self) -> Result<Vec<u8>, serde_cbor::Error> {
|
||||||
serde_cbor::to_vec(&self.remove_empty())
|
serde_cbor::to_vec(&self.remove_empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts `Languages` to JSON.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
///
|
||||||
|
/// let json = r#"{"Rust":{"blanks":5,"code":12,"comments":0,"stats":[{"blanks":5,"code":12,"comments":0,"lines":17,"name":".\\src\\lib\\build.rs"}],"lines":17,"total_files":1}}"#;
|
||||||
|
/// let languages = Languages::new();
|
||||||
|
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(json, languages.to_json());
|
||||||
|
/// ```
|
||||||
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
pub fn to_json(&self) -> Result<String, serde_json::Error> {
|
||||||
serde_json::to_string(&self.remove_empty())
|
serde_json::to_string(&self.remove_empty())
|
||||||
}
|
}
|
||||||
|
/// Converts `Languages` to YAML.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
///
|
||||||
|
/// let yaml = r#"
|
||||||
|
/// ---
|
||||||
|
/// "Rust":
|
||||||
|
/// "blanks": 5
|
||||||
|
/// "code": 12
|
||||||
|
/// "comments": 0
|
||||||
|
/// "lines": 17
|
||||||
|
/// "stats":
|
||||||
|
/// -
|
||||||
|
/// "blanks": 5
|
||||||
|
/// "code": 12
|
||||||
|
/// "comments": 0
|
||||||
|
/// "lines": 17
|
||||||
|
/// "name": ".\\src\\lib\\build.rs"
|
||||||
|
/// "total_files": 1"#;
|
||||||
|
/// let languages = Languages::new();
|
||||||
|
/// languages.get_statistics(&vec!["src/lib/build.rs"], &vec![".git"]);
|
||||||
|
///
|
||||||
|
/// assert_eq!(yaml, languages.to_yaml());
|
||||||
pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
|
pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
|
||||||
serde_yaml::to_string(&self.remove_empty())
|
serde_yaml::to_string(&self.remove_empty())
|
||||||
}
|
}
|
||||||
|
@ -306,6 +404,31 @@ impl AddAssign<BTreeMap<LanguageType, Language>> for Languages {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> AddAssign<&'a BTreeMap<LanguageType, Language>> for Languages {
|
||||||
|
fn add_assign(&mut self, rhs: &'a BTreeMap<LanguageType, Language>) {
|
||||||
|
|
||||||
|
for (name, language) in rhs {
|
||||||
|
|
||||||
|
if let Some(result) = self.inner.get_mut(&name) {
|
||||||
|
*result += language;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AddAssign<&'a mut BTreeMap<LanguageType, Language>> for Languages {
|
||||||
|
fn add_assign(&mut self, rhs: &'a mut BTreeMap<LanguageType, Language>) {
|
||||||
|
|
||||||
|
for (name, language) in rhs {
|
||||||
|
|
||||||
|
if let Some(result) = self.inner.get_mut(&name) {
|
||||||
|
*result += language;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Deref for Languages {
|
impl Deref for Languages {
|
||||||
type Target = BTreeMap<LanguageType, Language>;
|
type Target = BTreeMap<LanguageType, Language>;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
#![deny(missing_docs,
|
#![deny(missing_debug_implementations, missing_copy_implementations,
|
||||||
missing_debug_implementations, missing_copy_implementations,
|
trivial_casts, trivial_numeric_casts,
|
||||||
trivial_casts, trivial_numeric_casts,
|
unsafe_code,
|
||||||
unsafe_code,
|
unstable_features,
|
||||||
unstable_features,
|
unused_import_braces)]
|
||||||
unused_import_braces)]
|
|
||||||
|
|
||||||
//! # Tokei: Code Analysis Library(For the [binary](https://github.com/Aaronepower/tokei/))
|
//! # Tokei: Code Analysis Library([For the binary](https://github.com/Aaronepower/tokei/))
|
||||||
//!
|
//!
|
||||||
//! Tokei is the library powering the application of the same name.
|
//! Tokei is a code analysis library powering the application of the same name.
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate maplit;
|
extern crate maplit;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
#![allow(missing_docs)]
|
||||||
mod utils;
|
mod utils;
|
||||||
mod language;
|
mod language;
|
||||||
mod stats;
|
mod stats;
|
||||||
|
|
|
@ -1,16 +1,27 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
/// A struct representing the statistics of a file.
|
||||||
#[derive(Clone, Default, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
#[derive(Clone, Default, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
pub struct Stats {
|
pub struct Stats {
|
||||||
pub name: String,
|
/// Number of blank lines within the file.
|
||||||
pub code: usize,
|
|
||||||
pub blanks: usize,
|
pub blanks: usize,
|
||||||
pub lines: usize,
|
/// Number of lines of code within the file.
|
||||||
|
pub code: usize,
|
||||||
|
/// Number of comments within the file. (_includes both multi line, and single line comments_)
|
||||||
pub comments: usize,
|
pub comments: usize,
|
||||||
|
/// Total number of lines within the file.
|
||||||
|
pub lines: usize,
|
||||||
|
/// File name.
|
||||||
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Stats {
|
impl Stats {
|
||||||
|
/// Create a new `Stats` from a file path.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let stats = Stats::new("src/main.rs");
|
||||||
|
/// ```
|
||||||
pub fn new<S: Into<String>>(name: S) -> Self {
|
pub fn new<S: Into<String>>(name: S) -> Self {
|
||||||
Stats { name: name.into(), ..Self::default() }
|
Stats { name: name.into(), ..Self::default() }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue