Skip to content

Commit 12078a4

Browse files
author
Nichol Yip
committed
Moved crates to top level cargo.toml
Changed linting feature to return an UnknownKey object instead of a generated string Modified new method for UnknownKey to store values needed to generated lint message Added enum for lints to support different kinds of lints Removed commented code Refactored render method and renamed render_lint -> render_to_string Signed-off-by: Nichol Yip <[email protected]>
1 parent df77ab2 commit 12078a4

16 files changed

+99
-109
lines changed

Cargo.lock

+12-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ futures-core = "0.3.28"
5252
fuser = "0.12"
5353
glob = "0.3"
5454
indicatif = "0.17.5"
55+
itertools = "0.10"
5556
libc = "0.2.80"
5657
miette = "5.10"
58+
ngrammatic = "0.4.0"
5759
nix = "0.26.2"
5860
nom = "7.1"
5961
nom-supreme = "0.8"
@@ -72,6 +74,7 @@ serde_yaml = "0.9.25"
7274
shellexpand = "3.1.0"
7375
static_assertions = "1.1"
7476
strip-ansi-escapes = "0.1.1"
77+
struct-field-names-as-array = "0.3.0"
7578
strum = { version = "0.24", features = ["derive"] }
7679
thiserror = "1.0"
7780
tempfile = "3.3"

crates/spk-cli/group4/src/cmd_lint.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use colored::Colorize;
99
use miette::Result;
1010
use spk_cli_common::{flags, CommandArgs, Run};
1111
use spk_schema::v0::Spec;
12+
use spk_schema::Lint::Key;
1213
use spk_schema::{AnyIdent, LintedItem, SpecTemplate, Template, TemplateExt};
1314

1415
/// Validate spk yaml files
@@ -27,7 +28,7 @@ impl Run for Lint {
2728
let mut out = 0;
2829
let options = self.options.get_options()?;
2930
for spec in self.packages.iter() {
30-
let yaml = SpecTemplate::from_file(spec).and_then(|t| t.render_lint(&options))?;
31+
let yaml = SpecTemplate::from_file(spec).and_then(|t| t.render_to_string(&options))?;
3132
let lints: std::result::Result<LintedItem<Spec<AnyIdent>>, serde_yaml::Error> =
3233
serde_yaml::from_str(&yaml);
3334

@@ -37,7 +38,9 @@ impl Run for Lint {
3738
false => {
3839
println!("{} {}:", "Failed".red(), spec.display());
3940
for lint in s.lints {
40-
println!("{} {}", "----->".red(), lint);
41+
match lint {
42+
Key(k) => println!("{} {}", "----->".red(), k.generate_message()),
43+
}
4144
}
4245
out = 1;
4346
}

crates/spk-schema/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ format_serde_error = { version = "0.3", default_features = false, features = [
2020
] }
2121
ignore = "0.4.18"
2222
indexmap = "1.7"
23-
itertools = "0.10"
24-
ngrammatic = "0.4.0"
23+
itertools = { workspace = true }
24+
ngrammatic = { workspace = true }
2525
nom = { workspace = true }
2626
regex = "1.5"
2727
relative-path = "1.3"
@@ -35,7 +35,7 @@ spk-schema-foundation = { path = "./crates/foundation" }
3535
spk-schema-ident = { path = "./crates/ident" }
3636
spk-schema-validators = { path = "./crates/validators" }
3737
spk-schema-liquid = { path = "./crates/liquid" }
38-
struct-field-names-as-array = "*"
38+
struct-field-names-as-array = { workspace = true }
3939
sys-info = "0.9.0"
4040
tempfile = { workspace = true }
4141
thiserror = { workspace = true }

crates/spk-schema/src/build_spec.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use struct_field_names_as_array::FieldNamesAsArray;
1010

1111
use super::foundation::option_map::OptionMap;
1212
use super::{v0, Opt, ValidationSpec};
13-
use crate::{LintedItem, Lints, Result, UnknownKey, Variant};
13+
use crate::{Lint, LintedItem, Lints, Result, UnknownKey, Variant};
1414

1515
#[cfg(test)]
1616
#[path = "./build_spec_test.rs"]
@@ -161,11 +161,11 @@ impl UncheckedBuildSpec {
161161
#[derive(Default)]
162162
struct BuildSpecVisitor {
163163
build_spec: UncheckedBuildSpec,
164-
lints: Vec<String>,
164+
lints: Vec<Lint>,
165165
}
166166

167167
impl Lints for BuildSpecVisitor {
168-
fn lints(&mut self) -> Vec<String> {
168+
fn lints(&mut self) -> Vec<Lint> {
169169
std::mem::take(&mut self.lints)
170170
}
171171
}
@@ -232,9 +232,10 @@ impl<'de> serde::de::Visitor<'de> for BuildSpecVisitor {
232232
}
233233
"validation" => unchecked.validation = map.next_value::<ValidationSpec>()?,
234234
unknown_key => {
235-
let lint =
236-
UnknownKey::new(unknown_key, BuildSpec::FIELD_NAMES_AS_ARRAY.to_vec());
237-
self.lints.push(lint.message.to_string());
235+
self.lints.push(Lint::Key(UnknownKey::new(
236+
unknown_key,
237+
BuildSpec::FIELD_NAMES_AS_ARRAY.to_vec(),
238+
)));
238239
map.next_value::<serde::de::IgnoredAny>()?;
239240
}
240241
}

crates/spk-schema/src/environ.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use spk_schema_foundation::option_map::Stringified;
99
use struct_field_names_as_array::FieldNamesAsArray;
1010

11-
use crate::{LintedItem, Lints, UnknownKey};
11+
use crate::{Lint, LintedItem, Lints, UnknownKey};
1212

1313
#[cfg(test)]
1414
#[path = "./environ_test.rs"]
@@ -191,11 +191,11 @@ struct EnvOpVisitor {
191191
value: Option<String>,
192192
separator: Option<String>,
193193
#[field_names_as_array(skip)]
194-
lints: Vec<String>,
194+
lints: Vec<Lint>,
195195
}
196196

197197
impl Lints for EnvOpVisitor {
198-
fn lints(&mut self) -> Vec<String> {
198+
fn lints(&mut self) -> Vec<Lint> {
199199
std::mem::take(&mut self.lints)
200200
}
201201
}
@@ -304,8 +304,8 @@ impl<'de> serde::de::Visitor<'de> for EnvOpVisitor {
304304
field_names.extend(EnvPriority::FIELD_NAMES_AS_ARRAY.to_vec());
305305
field_names.extend(PrependEnv::FIELD_NAMES_AS_ARRAY.to_vec());
306306
field_names.extend(SetEnv::FIELD_NAMES_AS_ARRAY.to_vec());
307-
let lint = UnknownKey::new(unknown_key, field_names);
308-
self.lints.push(lint.message.to_string());
307+
self.lints
308+
.push(Lint::Key(UnknownKey::new(unknown_key, field_names)));
309309
map.next_value::<serde::de::IgnoredAny>()?;
310310
}
311311
}

crates/spk-schema/src/install_spec.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use struct_field_names_as_array::FieldNamesAsArray;
1212

1313
use super::{ComponentSpecList, EmbeddedPackagesList, EnvOp, OpKind, RequirementsList};
1414
use crate::foundation::option_map::OptionMap;
15-
use crate::{LintedItem, Lints, Result, UnknownKey};
15+
use crate::{Lint, LintedItem, Lints, Result, UnknownKey};
1616

1717
#[cfg(test)]
1818
#[path = "./install_spec_test.rs"]
@@ -35,7 +35,7 @@ impl<D> Lints for InstallSpecVisitor<D>
3535
where
3636
D: Default,
3737
{
38-
fn lints(&mut self) -> Vec<String> {
38+
fn lints(&mut self) -> Vec<Lint> {
3939
for env in self.environment.iter_mut() {
4040
self.lints.extend(std::mem::take(&mut env.lints));
4141
}
@@ -54,7 +54,7 @@ where
5454
components: ComponentSpecList,
5555
environment: Vec<LintedItem<EnvOp>>,
5656
#[field_names_as_array(skip)]
57-
lints: Vec<String>,
57+
lints: Vec<Lint>,
5858
#[field_names_as_array(skip)]
5959
_phantom: PhantomData<D>,
6060
}
@@ -139,11 +139,10 @@ where
139139
"components" => self.components = map.next_value::<ComponentSpecList>()?,
140140
"environment" => self.environment = map.next_value::<Vec<LintedItem<EnvOp>>>()?,
141141
unknown_key => {
142-
let lint = UnknownKey::new(
142+
self.lints.push(Lint::Key(UnknownKey::new(
143143
unknown_key,
144144
InstallSpecVisitor::<D>::FIELD_NAMES_AS_ARRAY.to_vec(),
145-
);
146-
self.lints.push(lint.message.to_string());
145+
)));
147146
map.next_value::<serde::de::IgnoredAny>()?;
148147
}
149148
}

crates/spk-schema/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use environ::{AppendEnv, EnvComment, EnvOp, EnvPriority, OpKind, PrependEnv,
3535
pub use error::{Error, Result};
3636
pub use input_variant::InputVariant;
3737
pub use install_spec::InstallSpec;
38-
pub use lints::{LintedItem, Lints, UnknownKey};
38+
pub use lints::{Lint, LintedItem, Lints, UnknownKey};
3939
pub use option::{Inheritance, Opt};
4040
pub use package::{Package, PackageMut};
4141
pub use recipe::{BuildEnv, Recipe};

crates/spk-schema/src/lints.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,55 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// https://github.com/imageworks/spk
44

5+
use std::sync::Arc;
6+
57
use ngrammatic::CorpusBuilder;
6-
use serde::Serialize;
78

8-
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Serialize)]
9+
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
910
pub struct UnknownKey {
10-
pub key: String,
11-
pub message: String,
11+
unknown_key: String,
12+
struct_fields: Vec<Arc<str>>,
1213
}
1314

1415
impl UnknownKey {
1516
pub fn new(unknown_key: &str, struct_fields: Vec<&str>) -> Self {
16-
let mut message = format!("Unrecognized key: {unknown_key}. ");
17+
Self {
18+
unknown_key: unknown_key.to_string(),
19+
struct_fields: struct_fields.iter().map(|v| Arc::from(*v)).collect(),
20+
}
21+
}
22+
23+
pub fn generate_message(&self) -> String {
24+
let mut message = format!("Unrecognized key: {}. ", self.unknown_key);
1725
let mut corpus = CorpusBuilder::new().finish();
1826

19-
for field in struct_fields.iter() {
27+
for field in self.struct_fields.iter() {
2028
corpus.add_text(field);
2129
}
2230

23-
match corpus.search(unknown_key, 0.6).first() {
31+
match corpus.search(&self.unknown_key, 0.6).first() {
2432
Some(s) => message.push_str(format!("(Did you mean: '{}'?)", s.text).as_str()),
25-
None => {
26-
message.push_str(format!("(No similar keys found for: {}.)", unknown_key).as_str())
27-
}
33+
None => message
34+
.push_str(format!("(No similar keys found for: {}.)", self.unknown_key).as_str()),
2835
};
2936

30-
Self {
31-
key: std::mem::take(&mut unknown_key.to_string()),
32-
message: message.to_string(),
33-
}
37+
message.to_string()
3438
}
3539
}
3640

37-
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd, Serialize)]
41+
#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
42+
pub enum Lint {
43+
Key(UnknownKey),
44+
}
45+
46+
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
3847
pub struct LintedItem<T> {
3948
pub item: T,
40-
pub lints: Vec<String>,
49+
pub lints: Vec<Lint>,
4150
}
4251

4352
pub trait Lints {
44-
fn lints(&mut self) -> Vec<String>;
53+
fn lints(&mut self) -> Vec<Lint>;
4554
}
4655

4756
impl<T, V> From<V> for LintedItem<T>

crates/spk-schema/src/metadata/meta.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use spk_config::Metadata;
1010
use spk_schema_foundation::option_map::Stringified;
1111
use struct_field_names_as_array::FieldNamesAsArray;
1212

13-
use crate::{Error, LintedItem, Lints, Result, UnknownKey};
13+
use crate::{Error, Lint, LintedItem, Lints, Result, UnknownKey};
1414

1515
#[cfg(test)]
1616
#[path = "./meta_test.rs"]
@@ -103,11 +103,11 @@ struct MetaVisitor {
103103
license: Option<String>,
104104
labels: Option<BTreeMap<String, String>>,
105105
#[field_names_as_array(skip)]
106-
lints: Vec<String>,
106+
lints: Vec<Lint>,
107107
}
108108

109109
impl Lints for MetaVisitor {
110-
fn lints(&mut self) -> Vec<String> {
110+
fn lints(&mut self) -> Vec<Lint> {
111111
std::mem::take(&mut self.lints)
112112
}
113113
}
@@ -159,9 +159,10 @@ impl<'de> serde::de::Visitor<'de> for MetaVisitor {
159159
"license" => self.license = Some(map.next_value::<Stringified>()?.0),
160160
"labels" => self.labels = Some(map.next_value::<BTreeMap<String, String>>()?),
161161
unknown_key => {
162-
let lint =
163-
UnknownKey::new(unknown_key, MetaVisitor::FIELD_NAMES_AS_ARRAY.to_vec());
164-
self.lints.push(lint.message.to_string());
162+
self.lints.push(Lint::Key(UnknownKey::new(
163+
unknown_key,
164+
MetaVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
165+
)));
165166
map.next_value::<serde::de::IgnoredAny>()?;
166167
}
167168
}

crates/spk-schema/src/requirements_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod requirements_list_test;
2424
/// same name, requiring instead that they be combined into a single
2525
/// request as needed.
2626
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
27-
// #[serde(transparent)]
27+
#[serde(transparent)]
2828
pub struct RequirementsList(Vec<Request>);
2929

3030
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]

crates/spk-schema/src/source_spec.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
1111
use spk_schema_foundation::option_map::Stringified;
1212
use struct_field_names_as_array::FieldNamesAsArray;
1313

14-
use crate::{Error, LintedItem, Lints, Result, Script, UnknownKey};
14+
use crate::{Error, Lint, LintedItem, Lints, Result, Script, UnknownKey};
1515

1616
#[cfg(test)]
1717
#[path = "./source_spec_test.rs"]
@@ -74,11 +74,11 @@ struct SourceSpecVisitor {
7474
depth: Option<u32>,
7575
subdir: Option<String>,
7676
#[field_names_as_array(skip)]
77-
lints: Vec<String>,
77+
lints: Vec<Lint>,
7878
}
7979

8080
impl Lints for SourceSpecVisitor {
81-
fn lints(&mut self) -> Vec<String> {
81+
fn lints(&mut self) -> Vec<Lint> {
8282
std::mem::take(&mut self.lints)
8383
}
8484
}
@@ -174,11 +174,10 @@ impl<'de> serde::de::Visitor<'de> for SourceSpecVisitor {
174174
"depth" => self.depth = Some(map.next_value::<u32>()?),
175175
"subdir" => self.subdir = Some(map.next_value::<Stringified>()?.0),
176176
unknown_key => {
177-
let lint = UnknownKey::new(
177+
self.lints.push(Lint::Key(UnknownKey::new(
178178
unknown_key,
179179
SourceSpecVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
180-
);
181-
self.lints.push(lint.message.to_string());
180+
)));
182181
map.next_value::<serde::de::IgnoredAny>()?;
183182
}
184183
}

crates/spk-schema/src/spec.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,7 @@ impl Template for SpecTemplate {
157157
&self.file_path
158158
}
159159

160-
fn render(&self, options: &OptionMap) -> Result<Self::Output> {
161-
let data = super::TemplateData::new(options);
162-
let rendered = spk_schema_liquid::render_template(&self.template, &data)
163-
.map_err(Error::InvalidTemplate)?;
164-
Ok(SpecRecipe::from_yaml(rendered)?)
165-
}
166-
167-
fn render_lint(&self, options: &OptionMap) -> Result<String> {
160+
fn render_to_string(&self, options: &OptionMap) -> Result<String> {
168161
let data = super::TemplateData::new(options);
169162
spk_schema_liquid::render_template(&self.template, &data).map_err(Error::InvalidTemplate)
170163
}

0 commit comments

Comments
 (0)