Skip to main content

Style

Struct Style 

Source
pub struct Style<K> { /* private fields */ }
Expand description

Formatting rules compiled against one language, ready for format().

Build one with Rules::compile. Style::default is the empty style: every gap keeps its original whitespace.

§Examples

use fmt_lang::{Rules, Space, Style, TokenRule};

// Kinds here are plain strings; a real language resolves names to its own
// kind type (for lang-forge: `|name| lang.kind(name)`).
let style: Style<&str> = Rules::new()
    .token(TokenRule::new("+").around(Space::Single))
    .compile(|name| ["+", "num"].into_iter().find(|k| *k == name))?;

Implementations§

Source§

impl<K> Style<K>

Source

pub fn with_touch(self, touch: fn(&str, &str) -> bool) -> Self

Replaces the test that decides whether two tokens may be written with nothing between them (default: can_touch).

The formatter calls it only where a rule would remove whitespace the source had between two tokens; if it answers false, one space is kept. A language whose lexer would read the touching texts differently (for example one with a -1 literal, where - and 1 must not touch) supplies its own test here.

§Examples
use fmt_lang::{can_touch, Style};

fn no_minus_digit(left: &str, right: &str) -> bool {
    let digit = right.starts_with(|c: char| c.is_ascii_digit());
    !(left.ends_with('-') && digit) && can_touch(left, right)
}
let style = Style::<u8>::default().with_touch(no_minus_digit);

Trait Implementations§

Source§

impl<K: Clone> Clone for Style<K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: Debug> Debug for Style<K>

Source§

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

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

impl<K> Default for Style<K>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K> Freeze for Style<K>
where Vec<K>: Freeze, TokenTable<K>: Freeze, Vec<NodeStyle<K>>: Freeze,

§

impl<K> RefUnwindSafe for Style<K>
where Vec<K>: RefUnwindSafe, TokenTable<K>: RefUnwindSafe, Vec<NodeStyle<K>>: RefUnwindSafe,

§

impl<K> Send for Style<K>
where Vec<K>: Send, TokenTable<K>: Send, Vec<NodeStyle<K>>: Send,

§

impl<K> Sync for Style<K>
where Vec<K>: Sync, TokenTable<K>: Sync, Vec<NodeStyle<K>>: Sync,

§

impl<K> Unpin for Style<K>
where Vec<K>: Unpin, TokenTable<K>: Unpin, Vec<NodeStyle<K>>: Unpin,

§

impl<K> UnsafeUnpin for Style<K>
where Vec<K>: UnsafeUnpin, TokenTable<K>: UnsafeUnpin, Vec<NodeStyle<K>>: UnsafeUnpin,

§

impl<K> UnwindSafe for Style<K>
where Vec<K>: UnwindSafe, TokenTable<K>: UnwindSafe, Vec<NodeStyle<K>>: UnwindSafe,

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.