[][src]Struct genco::Tokens

pub struct Tokens<L = ()> where
    L: Lang
{ /* fields omitted */ }

A stream of tokens.

Structural Requirements

While not strictly necessary, this structure does its best to maintain so-called structural requirements.

That means the following:

  • Only one [space()] may occur in sequence.
  • Only one [push()] may occur in sequence.
  • A [push()] may never be preceeded by a [line()], since it would have no effect.
  • Every [line()] must be preceeded by a [push()].
use genco::{Tokens, Item};

let mut tokens = Tokens::<()>::new();

tokens.push();
tokens.push();

assert_eq!(vec![Item::Push::<()>], tokens);

Implementations

impl<L> Tokens<L> where
    L: Lang
[src]

Generic methods.

pub fn new() -> Tokens<L>[src]

Create a new set of tokens.

pub fn iter(&self) -> Iter<L>[src]

Construct an iterator over this token stream.

pub fn into_iter(self) -> IntoIter<L>[src]

Construct an iterator that owns all items in token stream.

pub fn append<T>(&mut self, tokens: T) where
    T: FormatTokens<L>, 
[src]

Append the given element.

pub fn push_item(&mut self, item: Item<L>)[src]

Append a single item to the stream, while checking for structural guarantees.

pub fn extend<I>(&mut self, it: I) where
    I: IntoIterator<Item = Item<L>>, 
[src]

Extend with another set of tokens.

This respects the structural requirements of adding one element at a time, like you would get by calling [space()], [push()], or [line()].

pub fn walk_imports(&self) -> WalkImports<L>[src]

Walk over all imports.

Examples

use genco::prelude::*;

let debug = rust::imported("std::fmt", "Debug");

let ty = rust::imported("std::collections", "HashMap")
    .with_arguments((rust::U32, debug.into_dyn()));

let tokens = quote!(foo #ty baz);

for import in tokens.walk_imports() {
    println!("{:?}", import);
}

pub fn register<T>(&mut self, tokens: T) where
    T: RegisterTokens<L>, 
[src]

Add an registered custom element that is not rendered.

Registration can be used to generate imports that do not render a visible result.

Examples

The register functionality is available through the quote! macro by using the register function.


use genco::rust::{imported, Config};
use genco::quote;

let write_bytes_ext = imported("byteorder", "WriteBytesExt").alias("_");

let tokens = quote!(#@(write_bytes_ext));

assert_eq!("use byteorder::WriteBytesExt as _;\n\n", tokens.to_file_string().unwrap());

pub fn is_empty(&self) -> bool[src]

Check if tokens contain no items.

use genco::prelude::*;

let tokens: Tokens<()> = quote!();

assert!(tokens.is_empty());

pub fn space(&mut self)[src]

Add a single spacing to the token stream.

pub fn push(&mut self)[src]

Add a single push spacing operation.

pub fn line(&mut self)[src]

Assert that there's the necessary items to create one empty line at the end of the stream.

pub fn push_line(&mut self)[src]

👎 Deprecated:

use line function instead

Assert that there's the necessary items to create one empty line at the end of the stream.

pub fn indent(&mut self)[src]

Add a single indentation to the token stream.

pub fn unindent(&mut self)[src]

Add a single unindentation to the token stream.

pub fn format(
    &self,
    out: &mut Formatter,
    config: &mut L::Config,
    level: usize
) -> Result
[src]

Format the tokens.

pub fn to_file_string_with(
    self,
    config: L::Config,
    format_config: FormatterConfig
) -> Result<String, Error>
[src]

Format the token stream as a file for the given target language to a string. Using the specified config.

pub fn to_string_with(
    self,
    config: L::Config,
    format_config: FormatterConfig
) -> Result<String, Error>
[src]

Format only the current token stream as a string. Using the specified config.

pub fn to_file_vec_with(
    self,
    config: L::Config,
    format_config: FormatterConfig
) -> Result<Vec<String>, Error>
[src]

Format tokens into a vector, where each entry equals a line in the resulting file. Using the specified config.

pub fn to_fmt_writer_with<W>(
    self,
    writer: W,
    config: L::Config,
    format_config: FormatterConfig
) -> Result<(), Error> where
    W: Write
[src]

Format the token stream as a file for the given target language to the given writer. Using the specified config.

pub fn to_io_writer_with<W>(
    self,
    writer: W,
    config: L::Config,
    format_config: FormatterConfig
) -> Result<(), Error> where
    W: Write
[src]

Format the token stream as a file for the given target language to the given writer. Using the specified config.

impl<C: Default, L: Lang<Config = C>> Tokens<L>[src]

pub fn to_file_string(self) -> Result<String, Error>[src]

Format the token stream as a file for the given target language to a string. Using the default configuration.

pub fn to_string(self) -> Result<String, Error>[src]

Format only the current token stream as a string. Using the default configuration.

pub fn to_file_vec(self) -> Result<Vec<String>, Error>[src]

Format tokens into a vector, where each entry equals a line in the resulting file. Using the default configuration.

pub fn to_fmt_writer<W>(self, writer: W) -> Result<(), Error> where
    W: Write
[src]

Format the token stream as a file for the given target language to the given writer. Using the default configuration.

pub fn to_io_writer<W>(self, writer: W) -> Result<(), Error> where
    W: Write
[src]

Format the token stream as a file for the given target language to the given writer. Using the default configuration.

Trait Implementations

impl<L> Clone for Tokens<L> where
    L: Lang
[src]

impl<L> Debug for Tokens<L> where
    L: Lang
[src]

impl<L: Default> Default for Tokens<L> where
    L: Lang
[src]

impl<L> Eq for Tokens<L> where
    L: Lang
[src]

impl<L> FormatTokens<L> for Tokens<L> where
    L: Lang
[src]

impl<'a, L> FormatTokens<L> for &'a Tokens<L> where
    L: Lang
[src]

impl<'a, L: 'a> FromIterator<&'a Item<L>> for Tokens<L> where
    L: Lang
[src]

impl<L> FromIterator<Item<L>> for Tokens<L> where
    L: Lang
[src]

impl<L> IntoIterator for Tokens<L> where
    L: Lang
[src]

type Item = Item<L>

The type of the elements being iterated over.

type IntoIter = IntoIter<L>

Which kind of iterator are we turning this into?

impl<'a, L> IntoIterator for &'a Tokens<L> where
    L: Lang
[src]

type Item = &'a Item<L>

The type of the elements being iterated over.

type IntoIter = Iter<'a, L>

Which kind of iterator are we turning this into?

impl<'a, L> PartialEq<[Item<L>]> for Tokens<L> where
    L: Lang
[src]

impl<L> PartialEq<Tokens<L>> for Tokens<L> where
    L: Lang
[src]

impl<'a, L> PartialEq<Tokens<L>> for Vec<Item<L>> where
    L: Lang
[src]

impl<'a, L> PartialEq<Tokens<L>> for [Item<L>] where
    L: Lang
[src]

impl<'a, L> PartialEq<Vec<Item<L>>> for Tokens<L> where
    L: Lang
[src]

Auto Trait Implementations

impl<L = ()> !RefUnwindSafe for Tokens<L>

impl<L = ()> !Send for Tokens<L>

impl<L = ()> !Sync for Tokens<L>

impl<L> Unpin for Tokens<L>

impl<L = ()> !UnwindSafe for Tokens<L>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.