Skip to main content

NodeRule

Struct NodeRule 

Source
pub struct NodeRule { /* private fields */ }
Expand description

Layout rules for one node kind.

A node rule can make the node a group (laid out on one line if it fits, broken at its line opportunities otherwise), indent its contents, ask for spacing before and after the node, name the delimiters and separator of a list, cap the blank lines kept inside it, and carry token rules for its direct child tokens.

§Examples

use fmt_lang::{Indent, NodeRule, Space, Trailing};

// A JSON object: `{ "a": 1, "b": 2 }` when it fits, one member per line
// (indented) when it does not, and no trailing comma.
let object = NodeRule::new("object")
    .group()
    .indent(Indent::Block)
    .delimiters("{", "}", Space::Line)
    .separator(",", Space::Line, Trailing::Never);

Implementations§

Source§

impl NodeRule

Source

pub fn new(kind: impl Into<String>) -> Self

A rule for nodes of the kind named kind, with nothing set yet.

§Examples
use fmt_lang::NodeRule;

let stmt = NodeRule::new("stmt");
Source

pub fn group(self) -> Self

Makes the node a group: its Line and SoftLine gaps all stay flat if the whole node fits in the remaining width, and all break otherwise.

§Examples
use fmt_lang::NodeRule;

let call_args = NodeRule::new("args").group();
Source

pub fn indent(self, indent: Indent) -> Self

Sets how the node indents its contents (by Rules::indent columns).

§Examples
use fmt_lang::{Indent, NodeRule};

let binary = NodeRule::new("binary").indent(Indent::Hanging);
Source

pub fn before(self, space: Space) -> Self

Spacing in the gap before the node (before its first token).

§Examples
use fmt_lang::{NodeRule, Space};

// Every statement starts a new line.
let stmt = NodeRule::new("stmt").before(Space::Hard);
Source

pub fn after(self, space: Space) -> Self

Spacing in the gap after the node (after its last token).

§Examples
use fmt_lang::{NodeRule, Space};

let stmt = NodeRule::new("stmt").after(Space::Hard);
Source

pub fn delimiters( self, open: impl Into<String>, close: impl Into<String>, inner: Space, ) -> Self

Names the node’s delimiter tokens and the spacing just inside them (after open and before close). An empty body (open immediately followed by close) gets Space::None unless empty says otherwise.

A delimiter is recognised only as the node’s first (for open) or last (for close) significant direct child, so the same kinds may appear elsewhere inside the node without confusion.

§Examples
use fmt_lang::{NodeRule, Space};

// `[1, 2]` flat, or one element per line when broken.
let array = NodeRule::new("array").group().delimiters("[", "]", Space::SoftLine);
Source

pub fn empty(self, space: Space) -> Self

Spacing between the delimiters when the body is empty. Has no effect unless delimiters is set (before or after this call).

§Examples
use fmt_lang::{NodeRule, Space};

// `{ }` rather than `{}` for an empty block.
let block = NodeRule::new("block").delimiters("{", "}", Space::Hard).empty(Space::Single);
Source

pub fn separator( self, kind: impl Into<String>, after: Space, trailing: Trailing, ) -> Self

Names the list separator among the node’s direct child tokens, the spacing after it (before it is Space::None), and what to do with a trailing one.

§Examples
use fmt_lang::{NodeRule, Space, Trailing};

let args = NodeRule::new("args").separator(",", Space::Line, Trailing::Preserve);
Source

pub fn separator_text(self, text: impl Into<String>) -> Self

The text written when Trailing::Always adds a separator. Defaults to the separator’s kind name, which is its text in languages (such as those forged by lang-forge) that name symbol tokens by their text. Has no effect without a separator.

§Examples
use fmt_lang::{NodeRule, Space, Trailing};

let list = NodeRule::new("list")
    .delimiters("(", ")", Space::SoftLine)
    .separator("COMMA", Space::Line, Trailing::Always)
    .separator_text(",");
Source

pub fn blank_lines(self, max: u8) -> Self

Caps the blank lines kept between lines inside this node (overrides Rules::max_blank_lines here).

§Examples
use fmt_lang::NodeRule;

// No blank lines inside an argument list.
let args = NodeRule::new("args").blank_lines(0);
Source

pub fn token(self, rule: TokenRule) -> Self

Adds a token rule for the node’s direct child tokens. It wins over a top-level Rules::token rule for the same side.

§Examples
use fmt_lang::{NodeRule, Space, TokenRule};

let member = NodeRule::new("member").token(TokenRule::new(":").before(Space::None).after(Space::Single));
Source

pub fn optional(self) -> Self

Marks the rule optional: dropped by Rules::compile if the language lacks any kind it names, instead of an error.

§Examples
use fmt_lang::{NodeRule, Rules, Space};

let rules = Rules::new().node(NodeRule::new("block").before(Space::Hard).optional());
let style = rules.compile(|_| None::<u8>)?; // no `block` here: dropped

Trait Implementations§

Source§

impl Clone for NodeRule

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 Debug for NodeRule

Source§

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

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

impl Eq for NodeRule

Source§

impl Hash for NodeRule

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for NodeRule

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for NodeRule

Auto Trait Implementations§

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.