Repository: cobalt-org/liquid-rust (rust)

Issue:
`truncate` has overflow problems
`thread 'conformance_ruby::standard_filter_test::test_truncate' panicked at 'attempt to subtract with overflow', src/filters/mod.rs:218:22`

- [Rust implementation](https://github.com/cobalt-org/liquid-rust/blob/master/src/filters/mod.rs)
- [Ruby implementation](https://github.com/Shopify/liquid/blob/master/lib/liquid/standardfilters.rb)
- [Relevant tests](https://github.com/cobalt-org/liquid-rust/search?q=liquid+264&unscoped_q=liquid+264)
  - Once this is working, ideally the test will start failing.  We would just need to remove the `#[should_panic]` on it
  - If the test doesn't start failing, please temporarily remove `#[should_panic]`, and find or create an issue for the new failure

Pull request description:
fix(std): Overflow in `truncate`
Fixes #264


Patch:
diff --git a/crates/lib/src/stdlib/filters/string/truncate.rs b/crates/lib/src/stdlib/filters/string/truncate.rs
index dca97e5..cd33010 100644
--- a/crates/lib/src/stdlib/filters/string/truncate.rs
+++ b/crates/lib/src/stdlib/filters/string/truncate.rs
@@ -72,13 +72,15 @@ impl Filter for TruncateFilter {
         let args = self.args.evaluate(runtime)?;
 
         let lenght = args.lenght.unwrap_or(50) as usize;
-
         let truncate_string = args.ellipsis.unwrap_or_else(|| "...".into());
-
-        let l = cmp::max(lenght - truncate_string.len(), 0);
+        let diff = if lenght >= truncate_string.len() {
+            lenght - truncate_string.len()
+        } else {
+            0
+        };
+        let l = cmp::max(diff, 0);
 
         let input_string = input.to_kstr();
-
         let result = if lenght < input_string.len() {
             let result = UnicodeSegmentation::graphemes(input_string.as_str(), true)
                 .take(l)


Test patch:
diff --git a/tests/conformance_ruby/standard_filter_test.rs b/tests/conformance_ruby/standard_filter_test.rs
index 1682e04..703af94 100644
--- a/tests/conformance_ruby/standard_filter_test.rs
+++ b/tests/conformance_ruby/standard_filter_test.rs
@@ -142,7 +142,6 @@ fn test_slice_on_arrays() {
 }
 
 #[test]
-#[should_panic] // liquid-rust#264
 fn test_truncate() {
     assert_eq!(
         v!("1234..."),