Skip to content
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

New filters #74

Merged
merged 28 commits into from
Mar 4, 2017
Merged

New filters #74

merged 28 commits into from
Mar 4, 2017

Conversation

djwf
Copy link
Contributor

@djwf djwf commented Feb 18, 2017

I'm putting all the filters I create from the list in issue #11 here, as they are part of the standard, and the filter in pull request #73 is not.

Let me know if I should merge these two or split all filters into their own pull requests. Also, note that I'll use this to add more filters, and will leave a note when I've finished adding filters. If you'd prefer I not work that way, let me know and I'll change it up.

@djwf
Copy link
Contributor Author

djwf commented Feb 18, 2017

Whoops! My apologies: I need to rebase this off of master, rather than off of my other pull request. I'll do that after I get some sleep.

@djwf
Copy link
Contributor Author

djwf commented Feb 18, 2017

@johannhof: I am confused as to which tests would go in the src/filters.rs file, and which would go in tests/filters.rs. Can you give me some guidance on that?

@djwf
Copy link
Contributor Author

djwf commented Feb 20, 2017

@johannhof: I'm working on truncate, and think liquid-rust would be well served by using proper unicode parsing of strings, thus grapheme clusters rather than characters. I have added a dependency to an external crate for unicode segmentation for that purpose. Please let me know if I should back that change out.

@johannhof
Copy link
Contributor

@djwf Thanks so much for your work on this. Just wanted to let you know it's not been forgotten, I'm simply traveling between continents for work. I'll get to reviewing this today or tomorrow. :)

@djwf
Copy link
Contributor Author

djwf commented Feb 21, 2017

@johannhof No worries at all: I'll just keep plugging away at them. Note that I'm using the list of filters at the liquid markup web site as the authoritative list, as I noticed some of those missing from your list in #11 .

@fiji-flo fiji-flo mentioned this pull request Feb 25, 2017
@johannhof
Copy link
Contributor

Ok, looking at this now. The re-ordering makes it a bit hard to see what exactly changed, but I'll manage. As to your questions:

  • src/filters.rs is probably the best place to put tests. tests/filters.rs is intended to test cases where you want the code to go through the whole lexer, parser, etc. before running the filter (although it includes "normal" tests because I was sloppy with review)
  • grapheme clusters: without having read the code yet, I agree that this is a good idea in general. I'm not sure how the Ruby version does it (as we'd ideally want to produce the same output at the end of they day), but I guess that shouldn't stop us from doing it the right way. I wonder if there are places in the lexer that could benefit from this, too. But that's irrelevant to this already pretty big PR.

Cheers

Copy link
Contributor

@johannhof johannhof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good. Just a couple of nits. Thanks so much!

Note that I did not very thoroughly look at the reordered functions (GitHubs diff sucks for that). I'll just assume you didn't plant a trojan horse here.

src/filters.rs Outdated
pub fn replace_first(input: &Value, args: &[Value]) -> FilterResult {
if args.len() != 2 {
return Err(InvalidArgumentCount(format!("expected 2, {} given", args.len())));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're at it, you can replace this with check_args_len

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, done.

@@ -670,6 +670,23 @@ pub fn truncatewords(input: &Value, args: &[Value]) -> FilterResult {
}
}

/// Removes any duplicate elements in an array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave a comment that this operates in O(n^2) worst-case, which should be fine IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, done.

@@ -1,10 +1,44 @@
use Renderable;
use context::Context;
use filters::{size, upcase, downcase, capitalize, minus, plus, times, divided_by, ceil, floor,
round, prepend, append, first, last, pluralize, replace_first, replace, date, sort,
slice, modulo, escape, escape_once, remove_first, remove, strip_html, truncatewords};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change made because of rustfmt? I liked the grouped version a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was just because it makes it match the list of filters in the impl Renderable ... section, and it made it easier to see where/what was added. I don't have strong feelings about it either way, so I'll put it back to a grouped version (ordered alphabetically).

try!(check_args_len(args, 0));
match *input {
Value::Array(ref array) => {
let mut reversed = array.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: Look if we can reduce allocation in filters (by modifying output.rs) before 1.0

src/filters.rs Outdated
let ellipsis = "...".to_string();
let append = match args.get(1) {
Some(&Str(ref x)) => x,
_ => &ellipsis,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace this with just "..."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn I just noticed you took that from truncatewords. Can you please fix this up in truncatewords, too? Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep: fixed both truncate and truncatewords.

@djwf
Copy link
Contributor Author

djwf commented Feb 28, 2017

Thanks for the answers and the review: note that the most recent nightly of clippy is complaining about the five uses of lazy_static! (four in lexer.rs, one in filters.rs) so (on my end, at least) the rustup run nightly cargo clippy fails.

Also, my rustfmt (0.7.1 ()) formats the grouped filter use statement as follows:

use filters::{abs, append, capitalize, ceil, date, divided_by, downcase, escape, escape_once, first,
              floor, join, last, lstrip, minus, modulo, newline_to_br, pluralize, plus, prepend,
              remove, remove_first, replace, replace_first, reverse, round, rstrip, size, slice,
              sort, split, strip, strip_html, strip_newlines, times, truncate, truncatewords, uniq,
              upcase};

The rustfmt for the Travis CI builds, however, formats the grouped filter use statement differently:

use filters::{abs, append, capitalize, ceil, date, divided_by, downcase, escape, escape_once,
              first, floor, join, last, lstrip, minus, modulo, newline_to_br, pluralize, plus,
              prepend, remove, remove_first, replace, replace_first, reverse, round, rstrip, size,
              slice, sort, split, strip, strip_html, strip_newlines, times, truncate,
              truncatewords, uniq, upcase};

No big deal, but I figured you'd want to know.

@johannhof
Copy link
Contributor

note that the most recent nightly of clippy is complaining about the five uses of lazy_static! (four in lexer.rs, one in filters.rs) so (on my end, at least) the rustup run nightly cargo clippy fails.

Seems like there's an issue around that recently added rule: https://github.com/Manishearth/rust-clippy/issues/1580

You can ignore it by adding #![cfg_attr(feature="cargo-clippy", allow(zero_ptr))] to lib.rs.

The rustfmt for the Travis CI builds, however, formats the grouped filter use statement differently:

Feel free to update the version to v0.7.1-20170120 here: https://github.com/cobalt-org/liquid-rust/blob/master/.travis.yml#L10

@johannhof
Copy link
Contributor

You can ignore it by adding #![cfg_attr(feature="cargo-clippy", allow(zero_ptr))] to lib.rs.

Nevermind, I just merged #80 so you'll just have to rebase :)

This reverts commit 10f5e84.
Apparently the new rustfmt requires GLIBC_2.18, to fix later.
@johannhof
Copy link
Contributor

Looks great!

@johannhof johannhof merged commit 323ecd6 into cobalt-org:master Mar 4, 2017
@djwf djwf deleted the new-filters branch March 19, 2017 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants