Skip to content

Commit e903423

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 <nyip@imageworks.com>
1 parent 0189a9c commit e903423

File tree

16 files changed

+120
-100
lines changed

16 files changed

+120
-100
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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"
@@ -36,6 +36,7 @@ spk-schema-ident = { path = "./crates/ident" }
3636
spk-schema-validators = { path = "./crates/validators" }
3737
spk-schema-liquid = { path = "./crates/liquid" }
3838
strum = { workspace = true }
39+
struct-field-names-as-array = { workspace = true }
3940
sys-info = "0.9.0"
4041
tempfile = { workspace = true }
4142
thiserror = { workspace = true }

crates/spk-schema/src/build_spec.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::{v0, Opt, ValidationSpec};
1313
use crate::name::{OptName, OptNameBuf};
1414
use crate::option::VarOpt;
1515

16-
use crate::{LintedItem, Lints, Result, UnknownKey, Variant};
16+
use crate::{Lint, LintedItem, Lints, Result, UnknownKey, Variant};
1717

1818
#[cfg(test)]
1919
#[path = "./build_spec_test.rs"]
@@ -309,6 +309,24 @@ impl UncheckedBuildSpec {
309309
}
310310
}
311311

312+
#[derive(Default)]
313+
struct BuildSpecVisitor {
314+
build_spec: UncheckedBuildSpec,
315+
lints: Vec<Lint>,
316+
}
317+
318+
impl Lints for BuildSpecVisitor {
319+
fn lints(&mut self) -> Vec<Lint> {
320+
std::mem::take(&mut self.lints)
321+
}
322+
}
323+
324+
impl From<BuildSpecVisitor> for UncheckedBuildSpec {
325+
fn from(value: BuildSpecVisitor) -> Self {
326+
value.build_spec
327+
}
328+
}
329+
312330
impl<'de> Deserialize<'de> for UncheckedBuildSpec {
313331
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
314332
where

crates/spk-schema/src/environ.rs

Lines changed: 5 additions & 5 deletions
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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 26 additions & 17 deletions
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

Lines changed: 7 additions & 6 deletions
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
}

0 commit comments

Comments
 (0)