-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: validate variable data locations (#149)
* feat: add VarKind * feat: validate variable data locations * tests * note * tests
- Loading branch information
Showing
28 changed files
with
897 additions
and
214 deletions.
There are no files selected for viewing
This file contains 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 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 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,82 @@ | ||
use std::{cell::Cell, fmt}; | ||
|
||
/// Wrapper for [`fmt::from_fn`]. | ||
#[cfg(feature = "nightly")] | ||
pub fn from_fn<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>( | ||
f: F, | ||
) -> impl fmt::Debug + fmt::Display { | ||
fmt::from_fn(f) | ||
} | ||
|
||
/// Polyfill for [`fmt::from_fn`]. | ||
#[cfg(not(feature = "nightly"))] | ||
pub fn from_fn<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>( | ||
f: F, | ||
) -> impl fmt::Debug + fmt::Display { | ||
struct FromFn<F>(F); | ||
|
||
impl<F> fmt::Debug for FromFn<F> | ||
where | ||
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
(self.0)(f) | ||
} | ||
} | ||
|
||
impl<F> fmt::Display for FromFn<F> | ||
where | ||
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
(self.0)(f) | ||
} | ||
} | ||
|
||
FromFn(f) | ||
} | ||
|
||
/// Returns `list` formatted as a comma-separated list with "or" before the last item. | ||
pub fn or_list<I>(list: I) -> impl fmt::Display | ||
where | ||
I: IntoIterator<IntoIter: ExactSizeIterator, Item: fmt::Display>, | ||
{ | ||
let list = Cell::new(Some(list.into_iter())); | ||
from_fn(move |f| { | ||
let list = list.take().expect("or_list called twice"); | ||
let len = list.len(); | ||
for (i, t) in list.enumerate() { | ||
if i > 0 { | ||
let is_last = i == len - 1; | ||
f.write_str(if len > 2 && is_last { | ||
", or " | ||
} else if len == 2 && is_last { | ||
" or " | ||
} else { | ||
", " | ||
})?; | ||
} | ||
write!(f, "{t}")?; | ||
} | ||
Ok(()) | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_or_list() { | ||
let tests: &[(&[&str], &str)] = &[ | ||
(&[], ""), | ||
(&["`<eof>`"], "`<eof>`"), | ||
(&["integer", "identifier"], "integer or identifier"), | ||
(&["path", "string literal", "`&&`"], "path, string literal, or `&&`"), | ||
(&["`&&`", "`||`", "`&&`", "`||`"], "`&&`, `||`, `&&`, or `||`"), | ||
]; | ||
for &(tokens, expected) in tests { | ||
assert_eq!(or_list(tokens).to_string(), expected, "{tokens:?}"); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.