MagicString

Struct MagicString 

Source
pub struct MagicString<'s> { /* private fields */ }

Implementations§

Source§

impl<'text> MagicString<'text>

Source

pub fn append(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self

Source

pub fn append_left( &mut self, text_index: usize, content: impl Into<Cow<'text, str>>, ) -> &mut Self

§Example
 use string_wizard::MagicString;
 let mut s = MagicString::new("01234");
 s.append_left(2, "a");
 s.append_left(2, "b");
 assert_eq!(s.to_string(), "01ab234")
Source

pub fn append_right( &mut self, text_index: usize, content: impl Into<Cow<'text, str>>, ) -> &mut Self

§Example
use string_wizard::MagicString;
let mut s = MagicString::new("01234");
s.append_right(2, "A");
s.append_right(2, "B");
s.append_left(2, "a");
s.append_left(2, "b");
assert_eq!(s.to_string(), "01abAB234")
Source§

impl MagicString<'_>

Source

pub fn indent(&mut self) -> &mut Self

Source

pub fn indent_with(&mut self, opts: IndentOptions<'_, '_>) -> &mut Self

Source§

impl MagicString<'_>

Source

pub fn remove(&mut self, start: usize, end: usize) -> Result<&mut Self, String>

Source

pub fn relocate( &mut self, start: usize, end: usize, to: usize, ) -> Result<&mut Self, String>

Moves the characters from start and end to index. Returns this.

Source

pub fn snip(&self, start: usize, end: usize) -> Result<Self, String>

Returns a clone with content outside the specified range removed. This is equivalent to clone().remove(0, start).remove(end, original.len()).

Source§

impl<'text> MagicString<'text>

Source

pub fn prepend(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self

Source

pub fn prepend_left( &mut self, text_index: usize, content: impl Into<Cow<'text, str>>, ) -> &mut Self

Source

pub fn prepend_right( &mut self, text_index: usize, content: impl Into<Cow<'text, str>>, ) -> &mut Self

Source§

impl<'text> MagicString<'text>

Source

pub fn replace( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>

Source

pub fn replace_all( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>

Source

pub fn replace_with( &mut self, from: &str, to: impl Into<Cow<'text, str>>, options: ReplaceOptions, ) -> Result<&mut Self, String>

Source§

impl<'text> MagicString<'text>

Source

pub fn reset(&mut self, start: usize, end: usize) -> Result<&mut Self, String>

Resets the portion of the string from start to end to its original content. This undoes any modifications (updates, overwrites, intro/outro additions) made to that range.

§Errors
  • If start is greater than to end
  • If the range is out of bounds
Source§

impl<'text> MagicString<'text>

Source

pub fn slice(&self, start: usize, end: Option<usize>) -> Result<String, String>

Returns the content of the generated string that corresponds to the original positions from start to end.

If end is None, it defaults to the original string length.

Source§

impl MagicString<'_>

Source§

impl<'text> MagicString<'text>

Source

pub fn trim(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the start and end of the string.

Source

pub fn trim_start(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the start of the string.

Source

pub fn trim_end(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the end of the string.

Source

pub fn trim_lines(&mut self) -> &mut Self

Trims newlines from the start and end of the string.

Source§

impl<'text> MagicString<'text>

Source

pub fn update( &mut self, start: usize, end: usize, content: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>

A shorthand for update_with(start, end, content, Default::default());

Source

pub fn update_with( &mut self, start: usize, end: usize, content: impl Into<Cow<'text, str>>, opts: UpdateOptions, ) -> Result<&mut Self, String>

Source§

impl<'text> MagicString<'text>

Source

pub fn new(source: impl Into<Cow<'text, str>>) -> Self

Source

pub fn with_options( source: impl Into<Cow<'text, str>>, options: MagicStringOptions, ) -> Self

Source

pub fn source(&self) -> &str

Source

pub fn filename(&self) -> Option<&str>

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn has_changed(&self) -> bool

Indicates if the string has been changed.

Source

pub fn last_char(&self) -> Option<char>

Returns the last character of the generated string, or None if empty.

Source

pub fn last_line(&self) -> String

Returns the content after the last newline in the generated string.

Trait Implementations§

Source§

impl<'s> Clone for MagicString<'s>

Source§

fn clone(&self) -> MagicString<'s>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'s> Debug for MagicString<'s>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MagicString<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ToString for MagicString<'_>

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more

Auto Trait Implementations§

§

impl<'s> !Freeze for MagicString<'s>

§

impl<'s> RefUnwindSafe for MagicString<'s>

§

impl<'s> Send for MagicString<'s>

§

impl<'s> Sync for MagicString<'s>

§

impl<'s> Unpin for MagicString<'s>

§

impl<'s> UnwindSafe for MagicString<'s>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.