Skip to content

Commit

Permalink
add more context in linted message.
Browse files Browse the repository at this point in the history
Signed-off-by: Nichol Yip <[email protected]>
  • Loading branch information
Nichol Yip committed Dec 3, 2024
1 parent 1d3dd1d commit 77fbd44
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/spk-schema/src/build_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl<'de> serde::de::Visitor<'de> for BuildSpecVisitor {
"auto_host_vars" => unchecked.auto_host_vars = map.next_value::<AutoHostVars>()?,
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("build.{unknown_key}"),
BuildSpec::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/src/environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,4 @@ impl SetEnv {
pub fn tcsh_source(&self) -> String {
format!("setenv {} \"{}\"", self.set, self.value)
}
}
}
9 changes: 4 additions & 5 deletions crates/spk-schema/src/install_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use std::marker::PhantomData;

use serde::{Deserialize, Serialize};
use spk_schema_foundation::ident_component::Component;
use spk_schema_foundation::option_map::Stringified;
use spk_schema_foundation::spec_ops::Named;
use spk_schema_foundation::IsDefault;
use spk_schema_ident::{BuildIdent, OptVersionIdent};
use spk_schema_foundation::option_map::Stringified;
use struct_field_names_as_array::FieldNamesAsArray;

use crate::component_embedded_packages::ComponentEmbeddedPackage;
Expand All @@ -19,8 +19,8 @@ use crate::{
EmbeddedPackagesList,
EnvOp,
EnvOpList,
LintedItem,
Lint,
LintedItem,
Lints,
OpKind,
Package,
Expand Down Expand Up @@ -271,15 +271,14 @@ where
deserializer.deserialize_seq(EnvConfVisitor)
}


impl<'de, D> serde::de::Visitor<'de> for InstallSpecVisitor<D>
where
D: Default + From<InstallSpecVisitor<D>>,
{
type Value = D;

fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("a package specification")
f.write_str("an install spec")
}

fn visit_map<A>(mut self, mut map: A) -> std::result::Result<Self::Value, A::Error>
Expand All @@ -294,7 +293,7 @@ where
"environment" => self.environment = map.next_value::<EnvOpList>()?,
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("install.{unknown_key}"),
InstallSpecVisitor::<D>::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down
6 changes: 3 additions & 3 deletions crates/spk-schema/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ impl UnknownKey {
}

pub fn generate_message(&self) -> String {
let key: &str = self.unknown_key.split('.').last().unwrap_or_default();
let mut message = format!("Unrecognized key: {}. ", self.unknown_key);
let mut corpus = CorpusBuilder::new().finish();

for field in self.struct_fields.iter() {
corpus.add_text(field);
}

match corpus.search(&self.unknown_key, 0.6).first() {
match corpus.search(key, 0.6).first() {
Some(s) => message.push_str(format!("(Did you mean: '{}'?)", s.text).as_str()),
None => message
.push_str(format!("(No similar keys found for: {}.)", self.unknown_key).as_str()),
None => message.push_str(format!("(No similar keys found for: {}.)", key).as_str()),
};

message.to_string()
Expand Down
4 changes: 2 additions & 2 deletions crates/spk-schema/src/metadata/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::process::{Command, Stdio};

use serde::{Deserialize, Serialize};
use spk_config::Metadata;
use spk_schema_foundation::IsDefault;
use spk_schema_foundation::option_map::Stringified;
use spk_schema_foundation::IsDefault;
use struct_field_names_as_array::FieldNamesAsArray;

use crate::{Error, Lint, LintedItem, Lints, Result, UnknownKey};
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'de> serde::de::Visitor<'de> for MetaVisitor {
"labels" => self.labels = Some(map.next_value::<BTreeMap<String, String>>()?),
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("meta.{unknown_key}"),
MetaVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl<'de> serde::de::Visitor<'de> for UncheckedOptVisitor {
}
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("options.{unknown_key}"),
UncheckedOptVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/src/source_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl<'de> serde::de::Visitor<'de> for SourceSpecVisitor {
"subdir" => self.subdir = Some(map.next_value::<Stringified>()?.0),
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("sources.{unknown_key}"),
SourceSpecVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down
4 changes: 2 additions & 2 deletions crates/spk-schema/src/v0/test_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// https://github.com/spkenv/spk

use serde::{Deserialize, Serialize};
use spk_schema_ident::{RequestedBy, VersionIdent};
use spk_schema_foundation::option_map::Stringified;
use spk_schema_ident::{RequestedBy, VersionIdent};
use struct_field_names_as_array::FieldNamesAsArray;

use crate::ident::Request;
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<'de> serde::de::Visitor<'de> for TestSpecVisitor {
"requirements" => self.requirements = map.next_value::<Vec<Request>>()?,
unknown_key => {
self.lints.push(Lint::Key(UnknownKey::new(
unknown_key,
&format!("sources.{unknown_key}"),
TestSpecVisitor::FIELD_NAMES_AS_ARRAY.to_vec(),
)));
map.next_value::<serde::de::IgnoredAny>()?;
Expand Down

0 comments on commit 77fbd44

Please sign in to comment.