pub trait StringAbbreviate {
// Required methods
fn abbreviate(&self, max_width: usize) -> Result<String, AbbreviateError>;
fn abbreviate_with_offset(
&self,
offset: usize,
max_width: usize,
) -> Result<String, AbbreviateError>;
fn abbreviate_with_marker(
&self,
abbrev_marker: &str,
max_width: usize,
) -> Result<String, AbbreviateError>;
fn abbreviate_with_marker_and_offset(
&self,
abbrev_marker: &str,
offset: usize,
max_width: usize,
) -> Result<String, AbbreviateError>;
fn abbreviate_middle(&self, middle: &str, length: usize) -> String;
fn overlay(&self, overlay: &str, start: usize, end: usize) -> String;
}Expand description
Extension trait for string abbreviation methods.
This trait is implemented for str, allowing you to call abbreviation
methods directly on &str, String, and other string types.
§Examples
use rstring::StringAbbreviate;
// Works with &str
assert_eq!("hello world".abbreviate(8).unwrap(), "hello...");
// Works with String
assert_eq!(String::from("hello world").abbreviate(8).unwrap(), "hello...");Required Methods§
Sourcefn abbreviate(&self, max_width: usize) -> Result<String, AbbreviateError>
fn abbreviate(&self, max_width: usize) -> Result<String, AbbreviateError>
Abbreviates a string using ellipses (“…”).
Specifically:
- If the number of characters in the string is less than or equal to
max_width, returns the original string. - Otherwise abbreviates to
(substring(0, max_width - 3) + "..."). - If
max_widthis less than 4, returns an error. - The result will never exceed
max_widthcharacters.
§Errors
Returns AbbreviateError if max_width is less than 4.
§Examples
use rstring::StringAbbreviate;
assert_eq!("".abbreviate(4).unwrap(), "");
assert_eq!("abcdefg".abbreviate(6).unwrap(), "abc...");
assert_eq!("abcdefg".abbreviate(7).unwrap(), "abcdefg");
assert_eq!("abcdefg".abbreviate(8).unwrap(), "abcdefg");
assert_eq!("abcdefg".abbreviate(4).unwrap(), "a...");
assert!("abcdefg".abbreviate(3).is_err());Sourcefn abbreviate_with_offset(
&self,
offset: usize,
max_width: usize,
) -> Result<String, AbbreviateError>
fn abbreviate_with_offset( &self, offset: usize, max_width: usize, ) -> Result<String, AbbreviateError>
Abbreviates a string using ellipses with a left edge offset.
Works like abbreviate, but allows you to specify
a “left edge” offset. Note that this left edge is not necessarily going to be the
leftmost character in the result, or the first character following the ellipses,
but it will appear somewhere in the result.
The result will never exceed max_width characters.
§Errors
Returns AbbreviateError if max_width is too small for the abbreviation.
§Examples
use rstring::StringAbbreviate;
assert_eq!("".abbreviate_with_offset(0, 4).unwrap(), "");
assert_eq!("abcdefghijklmno".abbreviate_with_offset(0, 10).unwrap(), "abcdefg...");
assert_eq!("abcdefghijklmno".abbreviate_with_offset(5, 10).unwrap(), "...fghi...");
assert_eq!("abcdefghijklmno".abbreviate_with_offset(10, 10).unwrap(), "...ijklmno");
assert!("abcdefghij".abbreviate_with_offset(0, 3).is_err());
assert!("abcdefghij".abbreviate_with_offset(5, 6).is_err());Sourcefn abbreviate_with_marker(
&self,
abbrev_marker: &str,
max_width: usize,
) -> Result<String, AbbreviateError>
fn abbreviate_with_marker( &self, abbrev_marker: &str, max_width: usize, ) -> Result<String, AbbreviateError>
Abbreviates a string using a custom marker.
Specifically:
- If the number of characters in the string is less than or equal to
max_width, returns the original string. - Otherwise abbreviates to
(substring(0, max_width - marker.len()) + marker). - If
max_widthis less thanmarker.len() + 1, returns an error. - The result will never exceed
max_widthcharacters.
§Errors
Returns AbbreviateError if max_width is less than marker.len() + 1.
§Examples
use rstring::StringAbbreviate;
assert_eq!("".abbreviate_with_marker("...", 4).unwrap(), "");
assert_eq!("abcdefg".abbreviate_with_marker(".", 5).unwrap(), "abcd.");
assert_eq!("abcdefg".abbreviate_with_marker(".", 7).unwrap(), "abcdefg");
assert_eq!("abcdefg".abbreviate_with_marker("..", 4).unwrap(), "ab..");
assert_eq!("abcdefg".abbreviate_with_marker("..", 3).unwrap(), "a..");
assert!("abcdefg".abbreviate_with_marker("..", 2).is_err());Sourcefn abbreviate_with_marker_and_offset(
&self,
abbrev_marker: &str,
offset: usize,
max_width: usize,
) -> Result<String, AbbreviateError>
fn abbreviate_with_marker_and_offset( &self, abbrev_marker: &str, offset: usize, max_width: usize, ) -> Result<String, AbbreviateError>
Abbreviates a string using a custom marker with a left edge offset.
Works like abbreviate_with_marker,
but allows you to specify a “left edge” offset. Note that this left edge is not
necessarily going to be the leftmost character in the result, or the first character
following the replacement marker, but it will appear somewhere in the result.
The result will never exceed max_width characters.
§Errors
Returns AbbreviateError if max_width is too small for the abbreviation.
§Examples
use rstring::StringAbbreviate;
assert_eq!("".abbreviate_with_marker_and_offset("...", 0, 4).unwrap(), "");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("---", 0, 10).unwrap(), "abcdefg---");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset(",", 0, 10).unwrap(), "abcdefghi,");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset(",", 1, 10).unwrap(), "abcdefghi,");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset(",", 2, 10).unwrap(), "abcdefghi,");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("::", 4, 10).unwrap(), "::efghij::");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("...", 6, 10).unwrap(), "...ghij...");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("*", 9, 10).unwrap(), "*ghijklmno");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("'", 10, 10).unwrap(), "'ghijklmno");
assert_eq!("abcdefghijklmno".abbreviate_with_marker_and_offset("!", 12, 10).unwrap(), "!ghijklmno");
assert!("abcdefghij".abbreviate_with_marker_and_offset("abra", 0, 4).is_err());
assert!("abcdefghij".abbreviate_with_marker_and_offset("...", 5, 6).is_err());Sourcefn abbreviate_middle(&self, middle: &str, length: usize) -> String
fn abbreviate_middle(&self, middle: &str, length: usize) -> String
Abbreviates a string to the specified length, replacing the middle characters with the supplied replacement string.
This abbreviation only occurs if the following criteria are met:
- The string is not empty
- The replacement string is not empty
- The length is less than the string length
- The length is greater than 0
- The abbreviated string will have enough room for the replacement string and at least one character from the start and end
Otherwise, the original string is returned unchanged.
§Examples
use rstring::StringAbbreviate;
assert_eq!("abc".abbreviate_middle(".", 0), "abc");
assert_eq!("abc".abbreviate_middle(".", 3), "abc");
assert_eq!("abcdef".abbreviate_middle(".", 4), "ab.f");
assert_eq!("abcdef".abbreviate_middle("..", 4), "a..f");
assert_eq!("abcdef".abbreviate_middle(".", 5), "ab.ef");Sourcefn overlay(&self, overlay: &str, start: usize, end: usize) -> String
fn overlay(&self, overlay: &str, start: usize, end: usize) -> String
Overlays part of a string with another string.
An index greater than the string length is treated as the string length. The start index is always the smaller of the two indices.
§Examples
use rstring::StringAbbreviate;
assert_eq!("".overlay("abc", 0, 0), "abc");
assert_eq!("abcdef".overlay("", 2, 4), "abef");
assert_eq!("abcdef".overlay("", 4, 2), "abef");
assert_eq!("abcdef".overlay("zzzz", 2, 4), "abzzzzef");
assert_eq!("abcdef".overlay("zzzz", 4, 2), "abzzzzef");
assert_eq!("abcdef".overlay("zzzz", 2, 8), "abzzzz");
assert_eq!("abcdef".overlay("zzzz", 8, 10), "abcdefzzzz");