-
Notifications
You must be signed in to change notification settings - Fork 0
Ruma client test pr #6
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
Open
DevinR528
wants to merge
12
commits into
main
Choose a base branch
from
ruma-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eb73daf
Add readme and open PR to test webhook
DevinR528 75f058e
Add installation event and move common to a folder
DevinR528 4c3cd64
Before adding lifetimes to everything
DevinR528 b7cb27b
Before adding lifetimes to EVERTHING
DevinR528 b724696
All github webhooks api types have lifetime!
DevinR528 fb1fe12
Fix failing fields and add push and release
DevinR528 87833d8
Add CI for nightly and stable
DevinR528 11b608e
Use thiserror for response errors, use main/lib for testing
DevinR528 cfd6497
Add more events and add real responses to tests, organize common
DevinR528 5dd0c57
Add dynamic string formatting macro str_fmt!
DevinR528 e24092b
Move routes tests to out of crate
DevinR528 44eeedc
Ruma client impl ish...
DevinR528 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| //! All credit goes to https://github.com/A1-Triard/dyn-fmt | ||
|
|
||
| use std::fmt::{self, Display}; | ||
|
|
||
| /// This is the dynamic equivalent to the `format!` macro. | ||
| /// | ||
| /// This macro takes a format string and any number of the same type arguments. | ||
| /// The format arguments must implement [`Display`](std::fmt::Arguments) and match the | ||
| /// number of positional arguments in the format string. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ```rust | ||
| /// use magit::str_fmt; | ||
| /// let mut format_string = "Some user input with braces {} {} {}"; | ||
| /// assert_eq!( | ||
| /// "Some user input with braces 1 2 3", | ||
| /// str_fmt!(format_string, 1, 2, 3) | ||
| /// ); | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! str_fmt { | ||
| ($fmt:expr, $( $args:expr ),*) => { | ||
| format!("{}", $crate::strfmt::Arguments::new($fmt, &[ $( $args ),* ])) | ||
| }; | ||
| } | ||
|
|
||
| /// This structure represents a format string combined with its arguments. | ||
| /// In contrast with [`fmt::Arguments`](std::fmt::Arguments) this structure can be easily | ||
| /// and safely created at runtime. | ||
| #[derive(Clone, Debug)] | ||
| pub struct Arguments< | ||
| 'a, | ||
| F: AsRef<str>, | ||
| T: Display + ?Sized + 'a, | ||
| I: IntoIterator<Item = &'a T>, | ||
| > { | ||
| fmt: F, | ||
| args: I, | ||
| } | ||
|
|
||
| impl<'a, F: AsRef<str>, T: Display + ?Sized + 'a, I: IntoIterator<Item = &'a T> + Copy> | ||
| Arguments<'a, F, T, I> | ||
| { | ||
| /// Creates a new instance of a [`Display`] able structure, | ||
| /// representing formatted arguments. A runtime analog of | ||
| /// [`format_args!`](std::format_args) macro. Extra arguments are ignored, missing | ||
| /// arguments are replaced by empty string. # Examples: | ||
| /// ```rust | ||
| /// magit::strfmt::Arguments::new("{}a{}b{}c", &[1, 2, 3]); // "1a2b3c" | ||
| /// magit::strfmt::Arguments::new("{}a{}b{}c", &[1, 2, 3, 4]); // "1a2b3c" | ||
| /// magit::strfmt::Arguments::new("{}a{}b{}c", &[1, 2]); // "1a2bc" | ||
| /// magit::strfmt::Arguments::new("{{}}{}", &[1, 2]); // Error! braces cannot be used at all | ||
| /// ``` | ||
| pub fn new(fmt: F, args: I) -> Self { Arguments { fmt, args } } | ||
| } | ||
|
|
||
| impl<'a, F: AsRef<str>, T: Display + ?Sized + 'a, I: IntoIterator<Item = &'a T> + Copy> | ||
| Display for Arguments<'a, F, T, I> | ||
| { | ||
| fn fmt(&self, std_fmt: &mut fmt::Formatter) -> fmt::Result { | ||
| #[derive(Debug, Eq, PartialEq)] | ||
| enum State { | ||
| Piece, | ||
| Arg, | ||
| } | ||
| #[derive(Debug, Eq, PartialEq)] | ||
| enum Brace { | ||
| Left(usize), | ||
| Right(usize), | ||
| } | ||
| impl Brace { | ||
| fn index(&self) -> usize { | ||
| match self { | ||
| Self::Left(idx) => *idx, | ||
| Self::Right(idx) => *idx, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let mut args = self.args.into_iter(); | ||
| let fmt_str = self.fmt.as_ref(); | ||
| let mut braces = self | ||
| .fmt | ||
| .as_ref() | ||
| .chars() | ||
| .enumerate() | ||
| .filter_map(|(idx, c)| match c { | ||
| '{' => Some(Brace::Left(idx)), | ||
| '}' => Some(Brace::Right(idx)), | ||
| _ => None, | ||
| }) | ||
| .peekable(); | ||
|
|
||
| let mut state = State::Piece; | ||
| let mut start = 0; | ||
|
|
||
| while let Some(brace) = braces.next() { | ||
| match state { | ||
| State::Piece => { | ||
| let to = match braces.peek() { | ||
| Some(Brace::Left(_)) => return Err(fmt::Error), | ||
| Some(Brace::Right(_)) => brace.index(), | ||
| None => { | ||
| todo!("{:?} {} {:?}", state, start, brace) | ||
| } | ||
| }; | ||
|
|
||
| fmt_iter(fmt_str.chars().skip(start).take(to - start), std_fmt)?; | ||
| state = State::Arg; | ||
| } | ||
| State::Arg => match args.next() { | ||
| Some(arg) => { | ||
| arg.fmt(std_fmt)?; | ||
|
|
||
| start = brace.index() + 1; | ||
| state = State::Piece; | ||
| } | ||
| None => return Err(fmt::Error), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fmt_iter(fmt_str.chars().skip(start), std_fmt)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn fmt_iter<'a>( | ||
| iter: impl Iterator<Item = char> + 'a, | ||
| fmt: &mut fmt::Formatter, | ||
| ) -> fmt::Result { | ||
| for item in iter { | ||
| item.fmt(fmt)? | ||
| } | ||
| Ok(()) | ||
| } | ||
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,34 @@ | ||
| use magit::str_fmt; | ||
|
|
||
| #[test] | ||
| fn simple() { | ||
| assert_eq!("a b", str_fmt!("{} {}", "a", "b")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn with_named() { | ||
| let thing = "user"; | ||
| assert_eq!( | ||
| "[user] foobar goodbye", | ||
| str_fmt!("[{doesnotmatter}] foobar {hello}", thing, "goodbye") | ||
| ); | ||
| assert_eq!("[🎉user]", str_fmt!("[🎉{}]", thing)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn many() { | ||
| assert_eq!("a1b2c3xxx321zzz", str_fmt!("a{}b{}c{n}xxx{yyy}zzz", 1, 2, 3, 321)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn even_more() { | ||
| assert_eq!( | ||
| "a1b2c3xxx321zzz111222333444", | ||
| str_fmt!("a{}b{}c{n}xxx{yyy}zzz{}{}{}{}", 1, 2, 3, 321, 111, 222, 333, 444) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn str_fmt_macro() { | ||
| assert_eq!("a1b2c3xxx321zzz", str_fmt!("a{}b{}c{n}xxx{yyy}zzz", 1, 2, 3, 321)) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blah blah blah