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
use std::{
borrow::Cow,
fmt,
path::{Path, PathBuf},
str::FromStr,
vec,
};
use derive_more::{Display, Error};
use futures::stream;
use gherkin::GherkinEnv;
use globwalk::{GlobWalker, GlobWalkerBuilder};
use itertools::Itertools as _;
use crate::feature::Ext as _;
use super::{Error as ParseError, Parser};
#[derive(clap::Args, Debug)]
pub struct Cli {
#[clap(long = "input", short = 'i', value_name = "glob", global = true)]
pub features: Option<Walker>,
}
#[derive(Clone, Debug, Default)]
pub struct Basic {
language: Option<Cow<'static, str>>,
}
impl<I: AsRef<Path>> Parser<I> for Basic {
type Cli = Cli;
type Output =
stream::Iter<vec::IntoIter<Result<gherkin::Feature, ParseError>>>;
fn parse(self, path: I, cli: Self::Cli) -> Self::Output {
let walk = |walker: GlobWalker| {
walker
.filter_map(Result::ok)
.sorted_by(|l, r| Ord::cmp(l.path(), r.path()))
.map(|file| {
let env = self
.language
.as_ref()
.and_then(|l| GherkinEnv::new(l).ok())
.unwrap_or_default();
gherkin::Feature::parse_path(file.path(), env)
})
.collect::<Vec<_>>()
};
let get_features_path = || {
let path = path.as_ref();
path.canonicalize()
.or_else(|_| {
let mut buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
buf.push(
path.strip_prefix("/")
.or_else(|_| path.strip_prefix("./"))
.unwrap_or(path),
);
buf.as_path().canonicalize()
})
.map_err(|e| gherkin::ParseFileError::Reading {
path: path.to_path_buf(),
source: e,
})
};
let features = || {
let features = if let Some(walker) = cli.features {
walk(walker.0)
} else {
let feats_path = match get_features_path() {
Ok(p) => p,
Err(e) => return vec![Err(e.into())],
};
if feats_path.is_file() {
let env = self
.language
.as_ref()
.and_then(|l| GherkinEnv::new(l).ok())
.unwrap_or_default();
vec![gherkin::Feature::parse_path(feats_path, env)]
} else {
let w = GlobWalkerBuilder::new(feats_path, "*.feature")
.case_insensitive(true)
.build()
.unwrap_or_else(|e| {
unreachable!("GlobWalkerBuilder panicked: {e}")
});
walk(w)
}
};
features
.into_iter()
.map(|f| match f {
Ok(f) => f.expand_examples().map_err(ParseError::from),
Err(e) => Err(e.into()),
})
.collect()
};
stream::iter(features().into_iter())
}
}
impl Basic {
#[must_use]
pub const fn new() -> Self {
Self { language: None }
}
pub fn language(
mut self,
name: impl Into<Cow<'static, str>>,
) -> Result<Self, UnsupportedLanguageError> {
let name = name.into();
if !gherkin::is_language_supported(&name) {
return Err(UnsupportedLanguageError(name));
}
self.language = Some(name);
Ok(self)
}
}
#[derive(Debug, Display, Error)]
#[display(fmt = "Language {} isn't supported", _0)]
pub struct UnsupportedLanguageError(
#[error(not(source))] pub Cow<'static, str>,
);
pub struct Walker(GlobWalker);
impl fmt::Debug for Walker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Walker").finish_non_exhaustive()
}
}
impl FromStr for Walker {
type Err = globwalk::GlobError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
globwalk::glob(s).map(Self)
}
}