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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! # Hyperpolyglot
//! `hyperpolyglot` is a fast programming language detector.

use ignore::{overrides::OverrideBuilder, WalkBuilder};
use std::{
    collections::HashMap,
    convert::TryFrom,
    env, fmt,
    fs::File,
    io::{BufReader, Read, Seek, SeekFrom},
    path::{Path, PathBuf},
    sync::mpsc,
};

mod detectors;
mod filters;

// Include the map that stores language info
// static LANGUAGE_INFO: phf::Map<&'static str, Language> = ...;
include!("codegen/language-info-map.rs");

const MAX_CONTENT_SIZE_BYTES: usize = 51200;

/// The language struct that contains the name and other interesting information about a
/// language.
///
/// # Examples
/// ```
/// use hyperpolyglot::{Language, LanguageType};
/// use std::convert::TryFrom;
///
/// let language = Language::try_from("Rust").unwrap();
/// let expected = Language {
///     name: "Rust",
///     language_type: LanguageType::Programming,
///     color: Some("#dea584"),
///     group: None,
/// };
/// assert_eq!(language, expected)
/// ```
///
/// # Errors
/// `try_from` will error if the langauge name is not one of the known languages
///
/// If try_from is called with a language returned from [`detect`] or [`get_language_breakdown`]
/// the value is guaranteed to be there and can be unwrapped
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Language {
    /// The name of the language
    pub name: &'static str,
    /// Type of language. ex/ Data, Programming, Markup, Prose
    pub language_type: LanguageType,
    /// The css hex color used to represent the language on github. ex/ #dea584
    pub color: Option<&'static str>,
    /// Name of the parent language. ex/ The group for TSX would be TypeScript
    pub group: Option<&'static str>,
}

impl TryFrom<&str> for Language {
    type Error = &'static str;
    fn try_from(name: &str) -> Result<Self, Self::Error> {
        LANGUAGE_INFO
            .get(name)
            .map(|l| *l)
            .ok_or("Language not found")
    }
}

/// The set of possible language types
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum LanguageType {
    Data,
    Markup,
    Programming,
    Prose,
}

impl fmt::Display for LanguageType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LanguageType::Data => write!(f, "Data"),
            LanguageType::Markup => write!(f, "Markup"),
            LanguageType::Programming => write!(f, "Programming"),
            LanguageType::Prose => write!(f, "Prose"),
        }
    }
}

/// An enum where the variant is the streategy that detected the language and the value is the name
/// of the language
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Detection {
    Filename(&'static str),
    Extension(&'static str),
    Shebang(&'static str),
    Heuristics(&'static str),
    Classifier(&'static str),
}

impl Detection {
    /// Returns the language detected
    pub fn language(&self) -> &'static str {
        match self {
            Detection::Filename(language)
            | Detection::Extension(language)
            | Detection::Shebang(language)
            | Detection::Heuristics(language)
            | Detection::Classifier(language) => language,
        }
    }

    /// Returns the strategy used to detect the langauge
    pub fn variant(&self) -> &str {
        match self {
            Detection::Filename(_) => "Filename",
            Detection::Extension(_) => "Extension",
            Detection::Shebang(_) => "Shebang",
            Detection::Heuristics(_) => "Heuristics",
            Detection::Classifier(_) => "Classifier",
        }
    }
}

/// Detects the programming language of the file at a given path
///
/// If the language cannot be determined, None will be returned.
/// `detect` will error on an io error or if the parser returns an error when tokenizing the
/// contents of the file
///
/// # Examples
/// ```
/// use std::path::Path;
/// use hyperpolyglot::{detect, Detection};
///
/// let path = Path::new("src/bin/main.rs");
/// let language = detect(path).unwrap().unwrap();
/// assert_eq!(Detection::Heuristics("Rust"), language);
/// ```
pub fn detect(path: &Path) -> Result<Option<Detection>, std::io::Error> {
    let filename = path.file_name().and_then(|filename| filename.to_str());

    let candidate = filename.and_then(|filename| detectors::get_language_from_filename(filename));
    if let Some(candidate) = candidate {
        return Ok(Some(Detection::Filename(candidate)));
    };

    let extension = filename.and_then(|filename| detectors::get_extension(filename));

    let candidates = extension
        .map(|ext| detectors::get_languages_from_extension(ext))
        .unwrap_or(vec![]);

    if candidates.len() == 1 {
        return Ok(Some(Detection::Extension(candidates[0])));
    };

    let file = File::open(path)?;
    let mut reader = BufReader::new(file);

    let candidates = filter_candidates(
        candidates,
        detectors::get_languages_from_shebang(&mut reader)?,
    );
    if candidates.len() == 1 {
        return Ok(Some(Detection::Shebang(candidates[0])));
    };
    reader.seek(SeekFrom::Start(0))?;

    let mut content = String::new();
    reader.read_to_string(&mut content)?;
    let content = truncate_to_char_boundary(&content, MAX_CONTENT_SIZE_BYTES);

    // using heuristics is only going to be useful if we have more than one candidate
    // if the extension didn't result in candidate languages then the heuristics won't either
    let candidates = if candidates.len() > 1 {
        if let Some(extension) = extension {
            let languages =
                detectors::get_languages_from_heuristics(&extension[..], &candidates, &content);
            filter_candidates(candidates, languages)
        } else {
            candidates
        }
    } else {
        candidates
    };

    match candidates.len() {
        0 => Ok(None),
        1 => Ok(Some(Detection::Heuristics(candidates[0]))),
        _ => Ok(Some(Detection::Classifier(detectors::classify(
            &content,
            &candidates,
        )))),
    }
}

// function stolen from from https://doc.rust-lang.org/nightly/src/core/str/mod.rs.html
fn truncate_to_char_boundary(s: &str, mut max: usize) -> &str {
    if max >= s.len() {
        s
    } else {
        while !s.is_char_boundary(max) {
            max -= 1;
        }
        &s[..max]
    }
}

/// Walks the path provided and tallies the programming languages detected in the given path
///
/// Returns a map from the programming languages to a Vec of the files that were detected and the
/// strategy used
///
/// # Examples
/// ```
/// use hyperpolyglot::get_language_breakdown;
/// let breakdown = get_language_breakdown("src/");
/// let total_detections = breakdown.iter().fold(0, |sum, (language, detections)| sum + detections.len());
/// println!("Total files detected: {}", total_detections);
/// ```
pub fn get_language_breakdown<P: AsRef<Path>>(
    path: P,
) -> HashMap<&'static str, Vec<(Detection, PathBuf)>> {
    let override_builder = OverrideBuilder::new(&path);
    let override_builder = filters::add_documentation_override(override_builder);
    let override_builder = filters::add_vendor_override(override_builder);

    let num_threads = env::var_os("HYPLY_THREADS")
        .and_then(|threads| threads.into_string().ok())
        .and_then(|threads| threads.parse().ok())
        .unwrap_or(num_cpus::get());

    let (tx, rx) = mpsc::channel::<(Detection, PathBuf)>();
    let walker = WalkBuilder::new(path)
        .threads(num_threads)
        .overrides(override_builder.build().unwrap())
        .build_parallel();
    walker.run(|| {
        let tx = tx.clone();
        Box::new(move |result| {
            use ignore::WalkState::*;

            if let Ok(path) = result {
                let path = path.into_path();
                if let Ok(Some(detection)) = detect(&path) {
                    tx.send((detection, path)).unwrap();
                }
            }
            Continue
        })
    });
    drop(tx);

    let mut language_breakdown = HashMap::new();
    for (detection, file) in rx {
        let files = language_breakdown
            .entry(detection.language())
            .or_insert(vec![]);
        files.push((detection, file));
    }

    language_breakdown
}

fn filter_candidates(
    previous_candidates: Vec<&'static str>,
    new_candidates: Vec<&'static str>,
) -> Vec<&'static str> {
    if previous_candidates.len() == 0 {
        return new_candidates;
    }

    if new_candidates.len() == 0 {
        return previous_candidates;
    }

    let filtered_candidates: Vec<&'static str> = previous_candidates
        .iter()
        .filter(|l| new_candidates.contains(l))
        .map(|l| *l)
        .collect();

    match filtered_candidates.len() {
        0 => previous_candidates,
        _ => filtered_candidates,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::io::prelude::*;
    use std::iter;

    #[test]
    fn test_detect_filename() {
        let path = Path::new("APKBUILD");
        let detected_language = detect(path).unwrap().unwrap();

        assert_eq!(detected_language, Detection::Filename("Alpine Abuild"));
    }

    #[test]
    fn test_detect_extension() {
        let path = Path::new("pizza.purs");
        let detected_language = detect(path).unwrap().unwrap();

        assert_eq!(detected_language, Detection::Extension("PureScript"));
    }

    #[test]
    fn test_detect_shebang() {
        let path = Path::new("a");
        let mut file = File::create(path).unwrap();
        file.write(b"#!/usr/bin/python").unwrap();
        file.flush().unwrap();

        let detected_language = detect(path).unwrap().unwrap();

        fs::remove_file(path).unwrap();

        assert_eq!(detected_language, Detection::Shebang("Python"));
    }

    #[test]
    fn test_detect_heuristics() {
        let path = Path::new("a.es");
        let mut file = File::create(path).unwrap();
        file.write(b"'use strict'").unwrap();
        file.flush().unwrap();

        let detected_language = detect(path).unwrap().unwrap();

        fs::remove_file(path).unwrap();

        assert_eq!(detected_language, Detection::Heuristics("JavaScript"));
    }

    #[test]
    fn test_detect_classify() {
        let path = Path::new("peep.rs");
        let mut file = File::create(path).unwrap();
        file.write(
            b"
            match optional {
                Some(pattern) => println!(\"Hello World\"),
                None => println!(\"u missed\")
            }
            ",
        )
        .unwrap();
        file.flush().unwrap();

        let detected_language = detect(path).unwrap().unwrap();

        fs::remove_file(path).unwrap();
        assert_eq!(detected_language, Detection::Classifier("Rust"));
    }

    #[test]
    fn test_detect_none() {
        let path = Path::new("y");
        let mut file = File::create(path).unwrap();
        file.write(
            b"
            use std::io;
            fn main() {
                println!(\"{}\", \"Hello World\");
            }",
        )
        .unwrap();
        file.flush().unwrap();

        let detected_language = detect(path).unwrap();

        fs::remove_file(path).unwrap();

        assert_eq!(detected_language, None);
    }

    #[test]
    fn test_detect_accuracy() {
        let mut total = 0;
        let mut correct = 0;
        fs::read_dir("samples")
            .unwrap()
            .map(|entry| entry.unwrap())
            .filter(|entry| entry.path().is_dir())
            .map(|language_dir| {
                let path = language_dir.path();
                let language = path.file_name().unwrap();
                let language = language.to_string_lossy().into_owned();

                let file_paths = fs::read_dir(language_dir.path())
                    .unwrap()
                    .map(|entry| entry.unwrap().path())
                    .filter(|path| path.is_file());

                let language_iter = iter::repeat(language);
                file_paths.zip(language_iter)
            })
            .flatten()
            .for_each(|(file, language)| {
                // Skip the files we can't detect. The reason the detect function fails on these is
                // because of a heuristic added to .h files that defaults to C if none of the
                // Objective-C or C++ rules match. This makes us fail on two of the sample files
                // but tends to perform better on non training data
                if file.file_name().unwrap() == "rpc.h" || file.file_name().unwrap() == "Field.h" {
                    return;
                }
                // F* uses the name Fstar in the file system
                let language = match &language[..] {
                    "Fstar" => "F*",
                    l => l,
                };
                if let Ok(Some(detection)) = detect(&file) {
                    total += 1;
                    if detection.language() == language {
                        correct += 1;
                    } else {
                        println!("Incorrect detection: {:?} {:?}", file, detection)
                    }
                }
            });

        let accuracy = (correct as f64) / (total as f64);
        assert_eq!(accuracy, 1.0);
    }

    #[test]
    fn test_filter_candidates() {
        let previous_candidates = vec!["JavaScript", "Python"];
        let new_candidates = vec!["Python", "Bibbity"];
        assert_eq!(
            filter_candidates(previous_candidates, new_candidates),
            vec!["Python"]
        );
    }

    #[test]
    fn test_filter_candidates_no_new() {
        let previous_candidates = vec!["JavaScript", "Python"];
        let new_candidates = vec![];
        assert_eq!(
            filter_candidates(previous_candidates, new_candidates),
            vec!["JavaScript", "Python"]
        );
    }

    #[test]
    fn test_filter_candidates_no_prev() {
        let previous_candidates = vec![];
        let new_candidates = vec!["JavaScript", "Erlang"];
        assert_eq!(
            filter_candidates(previous_candidates, new_candidates),
            vec!["JavaScript", "Erlang"]
        );
    }

    #[test]
    fn test_filter_candidates_no_matches() {
        let previous_candidates = vec!["Python"];
        let new_candidates = vec!["JavaScript", "Erlang"];
        assert_eq!(
            filter_candidates(previous_candidates, new_candidates),
            vec!["Python"]
        );
    }

    #[test]
    fn test_get_language_breakdown_ignores_overrides_documentation() {
        fs::create_dir_all("temp-testing-dir").unwrap();
        fs::File::create("temp-testing-dir/README.md").unwrap();
        assert!(get_language_breakdown("temp-testing-dir").is_empty());

        fs::remove_dir_all("temp-testing-dir").unwrap();
    }

    #[test]
    fn test_get_language_breakdown_ignores_overrides_vendor() {
        fs::create_dir_all("temp-testing-dir2/node_modules").unwrap();
        fs::File::create("temp-testing-dir2/node_modules/hello.go").unwrap();
        assert!(get_language_breakdown("temp-testing-dir2").is_empty());

        fs::remove_dir_all("temp-testing-dir2").unwrap();
    }
}