Skip to main content

Crate caddyfile_rs

Crate caddyfile_rs 

Source
Expand description

Caddyfile lexer, parser, formatter, and builder.

A typed AST for Caddy’s configuration file format with tools to parse Caddyfiles from text, build them programmatically, and format them back to valid syntax.

§Quick start

§Parse and re-format a Caddyfile

use caddyfile_rs::{tokenize, parse, format};

let input = "example.com {\n\treverse_proxy app:3000\n}\n";
let tokens = tokenize(input).unwrap();
let caddyfile = parse(&tokens).unwrap();
let output = format(&caddyfile);
assert_eq!(output, input);

§Build a Caddyfile programmatically

use caddyfile_rs::{Caddyfile, SiteBlock, Directive, format};

let cf = Caddyfile::new()
    .site(SiteBlock::new("example.com")
        .reverse_proxy("app:3000")
        .encode_gzip()
        .log());

let output = format(&cf);
assert!(output.contains("reverse_proxy app:3000"));

Re-exports§

pub use ast::Address;
pub use ast::Argument;
pub use ast::Caddyfile;
pub use ast::Directive;
pub use ast::GlobalOptions;
pub use ast::Matcher;
pub use ast::NamedRoute;
pub use ast::Scheme;
pub use ast::SiteBlock;
pub use ast::Snippet;
pub use ast::parse_address;
pub use formatter::format;
pub use lexer::LexError;
pub use lexer::LexErrorKind;
pub use lexer::tokenize;
pub use parser::ParseError;
pub use parser::ParseErrorKind;
pub use parser::parse;
pub use token::Span;
pub use token::Token;
pub use token::TokenKind;

Modules§

ast
Abstract syntax tree types representing a parsed Caddyfile.
builder
Builder-pattern methods for constructing Caddyfile AST nodes.
formatter
Pretty-printer that serializes a Caddyfile AST back into canonical text.
lexer
Tokenizer that converts raw Caddyfile source text into a stream of tokens.
parser
Parser that transforms a token stream into a Caddyfile AST.
token
Token types and source spans produced by the lexer.

Enums§

Error
Unified error type covering both lexing and parsing.

Functions§

parse_str
Tokenize and parse a Caddyfile source string in one step.