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
use std::env::current_dir;
use std::path::{Path, PathBuf};

use diagnostic_quick::{QError, QResult};
use diagnostic_quick::error_3rd::{Glob, GlobSet, GlobSetBuilder};

pub fn ensure_workspace<P: AsRef<Path>>(path: P) -> QResult<PathBuf> {
    let path = path.as_ref();
    if !path.is_dir() {
        Err(QError::runtime_error(format!("workspace must a dir")))?;
    }
    Ok(path.canonicalize()?)
}

pub fn resolve_workspace(path: &Option<String>) -> QResult<PathBuf> {
    let path = match path {
        None => { current_dir()? }
        Some(s) => { PathBuf::from(s) }
    };
    ensure_workspace(path)
}


pub fn build_glob_set(include: &Option<String>) -> QResult<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    match include {
        None => {
            builder.add(Glob::new("*")?);
        }
        Some(s) => {
            for line in s.trim().lines() {
                builder.add(Glob::new(line)?);
            }
        }
    }
    Ok(builder.build()?)
}