-
Notifications
You must be signed in to change notification settings - Fork 13.4k
jsondocck: Refactor directive handling #141709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
use std::borrow::Cow; | ||
|
||
use serde_json::Value; | ||
|
||
use crate::cache::Cache; | ||
|
||
#[derive(Debug)] | ||
pub struct Directive { | ||
pub kind: DirectiveKind, | ||
pub path: String, | ||
pub lineno: usize, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum DirectiveKind { | ||
/// `//@ has <path>` | ||
/// | ||
/// Checks the path exists. | ||
HasPath, | ||
|
||
/// `//@ has <path> <value>` | ||
/// | ||
/// Check one thing at the path is equal to the value. | ||
HasValue { value: String }, | ||
|
||
/// `//@ !has <path>` | ||
/// | ||
/// Checks the path doesn't exist. | ||
HasNotPath, | ||
|
||
/// `//@ !has <path> <value>` | ||
/// | ||
/// Checks the path exists, but doesn't have the given value. | ||
HasNotValue { value: String }, | ||
|
||
/// `//@ is <path> <value>` | ||
/// | ||
/// Check the path is the given value. | ||
Is { value: String }, | ||
|
||
/// `//@ is <path> <value> <value>...` | ||
/// | ||
/// Check that the path matches to exactly every given value. | ||
IsMany { values: Vec<String> }, | ||
|
||
/// `//@ !is <path> <value>` | ||
/// | ||
/// Check the path isn't the given value. | ||
IsNot { value: String }, | ||
|
||
/// `//@ count <path> <value>` | ||
/// | ||
/// Check the path has the expected number of matches. | ||
CountIs { expected: usize }, | ||
|
||
/// `//@ set <name> = <path>` | ||
Set { variable: String }, | ||
} | ||
|
||
impl DirectiveKind { | ||
/// Returns both the kind and the path. | ||
/// | ||
/// Returns `None` if the directive isn't from jsondocck (e.g. from compiletest). | ||
pub fn parse<'a>( | ||
directive_name: &str, | ||
negated: bool, | ||
args: &'a [String], | ||
) -> Option<(Self, &'a str)> { | ||
let kind = match (directive_name, negated) { | ||
("count", false) => { | ||
assert_eq!(args.len(), 2); | ||
let expected = args[1].parse().expect("invalid number for `count`"); | ||
Self::CountIs { expected } | ||
} | ||
|
||
("ismany", false) => { | ||
// FIXME: Make this >= 3, and migrate len(values)==1 cases to @is | ||
assert!(args.len() >= 2, "Not enough args to `ismany`"); | ||
let values = args[1..].to_owned(); | ||
Self::IsMany { values } | ||
} | ||
|
||
("is", false) => { | ||
assert_eq!(args.len(), 2); | ||
Self::Is { value: args[1].clone() } | ||
} | ||
("is", true) => { | ||
assert_eq!(args.len(), 2); | ||
Self::IsNot { value: args[1].clone() } | ||
} | ||
|
||
("set", false) => { | ||
assert_eq!(args.len(), 3); | ||
assert_eq!(args[1], "="); | ||
return Some((Self::Set { variable: args[0].clone() }, &args[2])); | ||
} | ||
|
||
("has", false) => match args { | ||
[_path] => Self::HasPath, | ||
[_path, value] => Self::HasValue { value: value.clone() }, | ||
_ => panic!("`//@ has` must have 2 or 3 arguments, but got {args:?}"), | ||
}, | ||
("has", true) => match args { | ||
[_path] => Self::HasNotPath, | ||
[_path, value] => Self::HasNotValue { value: value.clone() }, | ||
_ => panic!("`//@ !has` must have 2 or 3 arguments, but got {args:?}"), | ||
}, | ||
|
||
(_, false) if KNOWN_DIRECTIVE_NAMES.contains(&directive_name) => { | ||
GuillaumeGomez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None; | ||
} | ||
_ => { | ||
panic!("Invalid directive `//@ {}{directive_name}`", if negated { "!" } else { "" }) | ||
} | ||
}; | ||
|
||
Some((kind, &args[0])) | ||
} | ||
} | ||
|
||
impl Directive { | ||
/// Performs the actual work of ensuring a directive passes. | ||
pub fn check(&self, cache: &mut Cache) -> Result<(), String> { | ||
let matches = cache.select(&self.path); | ||
match &self.kind { | ||
DirectiveKind::HasPath => { | ||
if matches.is_empty() { | ||
return Err("matched to no values".to_owned()); | ||
} | ||
} | ||
DirectiveKind::HasNotPath => { | ||
if !matches.is_empty() { | ||
return Err(format!("matched to {matches:?}, but wanted no matches")); | ||
} | ||
} | ||
DirectiveKind::HasValue { value } => { | ||
let want_value = string_to_value(value, cache); | ||
if !matches.contains(&want_value.as_ref()) { | ||
return Err(format!( | ||
"matched to {matches:?}, which didn't contain {want_value:?}" | ||
)); | ||
} | ||
} | ||
DirectiveKind::HasNotValue { value } => { | ||
let wantnt_value = string_to_value(value, cache); | ||
if matches.contains(&wantnt_value.as_ref()) { | ||
return Err(format!( | ||
"matched to {matches:?}, which contains unwanted {wantnt_value:?}" | ||
)); | ||
} else if matches.is_empty() { | ||
return Err(format!( | ||
"got no matches, but expected some matched (not containing {wantnt_value:?}" | ||
)); | ||
} | ||
} | ||
|
||
DirectiveKind::Is { value } => { | ||
let want_value = string_to_value(value, cache); | ||
let matched = get_one(&matches)?; | ||
if matched != want_value.as_ref() { | ||
return Err(format!("matched to {matched:?} but want {want_value:?}")); | ||
} | ||
} | ||
DirectiveKind::IsNot { value } => { | ||
let wantnt_value = string_to_value(value, cache); | ||
let matched = get_one(&matches)?; | ||
if matched == wantnt_value.as_ref() { | ||
return Err(format!("got value {wantnt_value:?}, but want anything else")); | ||
} | ||
} | ||
|
||
DirectiveKind::IsMany { values } => { | ||
// Serde json doesn't implement Ord or Hash for Value, so we must | ||
// use a Vec here. While in theory that makes setwize equality | ||
// O(n^2), in practice n will never be large enough to matter. | ||
let expected_values = | ||
values.iter().map(|v| string_to_value(v, cache)).collect::<Vec<_>>(); | ||
if expected_values.len() != matches.len() { | ||
return Err(format!( | ||
"Expected {} values, but matched to {} values ({:?})", | ||
expected_values.len(), | ||
matches.len(), | ||
matches | ||
)); | ||
}; | ||
for got_value in matches { | ||
if !expected_values.iter().any(|exp| &**exp == got_value) { | ||
return Err(format!("has match {got_value:?}, which was not expected",)); | ||
} | ||
} | ||
} | ||
DirectiveKind::CountIs { expected } => { | ||
if *expected != matches.len() { | ||
return Err(format!( | ||
"matched to `{matches:?}` with length {}, but expected length {expected}", | ||
matches.len(), | ||
)); | ||
} | ||
} | ||
DirectiveKind::Set { variable } => { | ||
let value = get_one(&matches)?; | ||
let r = cache.variables.insert(variable.to_owned(), value.clone()); | ||
assert!(r.is_none(), "name collision: {variable:?} is duplicated"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn get_one<'a>(matches: &[&'a Value]) -> Result<&'a Value, String> { | ||
match matches { | ||
[] => Err("matched to no values".to_owned()), | ||
[matched] => Ok(matched), | ||
_ => Err(format!("matched to multiple values {matches:?}, but want exactly 1")), | ||
} | ||
} | ||
|
||
// FIXME: This setup is temporary until we figure out how to improve this situation. | ||
// See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>. | ||
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../compiletest/src/directive-list.rs")); | ||
|
||
fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> { | ||
if s.starts_with("$") { | ||
Cow::Borrowed(&cache.variables.get(&s[1..]).unwrap_or_else(|| { | ||
// FIXME(adotinthevoid): Show line number | ||
panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables) | ||
})) | ||
} else { | ||
Cow::Owned(serde_json::from_str(s).expect(&format!("Cannot convert `{}` to json", s))) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use crate::Command; | ||
use crate::Directive; | ||
|
||
#[derive(Debug)] | ||
pub struct CkError { | ||
pub message: String, | ||
pub command: Command, | ||
pub directive: Directive, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.