Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Config

The config module loads settings from .towl.toml, merges environment variables, and provides the init command for generating default configuration.

TowlConfig

#![allow(unused)]
fn main() {
pub struct TowlConfig {
    pub parsing: ParsingConfig,
    pub github: GitHubConfig,
}
}

TowlConfig::load

#![allow(unused)]
fn main() {
impl TowlConfig {
    pub fn load(path: Option<&PathBuf>) -> Result<Self, TowlConfigError>;
}
}

Loads configuration with this precedence:

  1. Built-in defaults
  2. .towl.toml file (or custom path via --path)
  3. Environment variable overrides

If no config file exists, defaults are used without error.

init

#![allow(unused)]
fn main() {
pub async fn init(path: &Path, force: bool) -> Result<(), TowlConfigError>
}

Creates a .towl.toml file at the given path. Auto-detects GitHub owner and repo from git remote get-url origin.

  • Fails if the file already exists (unless force is true)
  • Validates the path for traversal attacks
  • Serializes ParsingConfig defaults to TOML

ParsingConfig

#![allow(unused)]
fn main() {
pub struct ParsingConfig {
    pub file_extensions: HashSet<String>,
    pub exclude_patterns: Vec<String>,
    pub include_context_lines: usize,
    pub comment_prefixes: Vec<String>,
    pub todo_patterns: Vec<String>,
    pub function_patterns: Vec<String>,
}
}

All fields have defaults via #[serde(default)]:

FieldDefault
file_extensionsrs, toml, json, yaml, yml, sh, bash
exclude_patternstarget/*, .git/*
include_context_lines3
comment_prefixes//, ^\s*#, /\*, ^\s*\*
todo_patternsTODO:, FIXME:, HACK:, NOTE:, BUG: (case-insensitive)
function_patternsRust, Python, JS, Java/C#, Go patterns

Each pattern array is limited to MAX_CONFIG_PATTERNS (100) entries.

GitHubConfig

#![allow(unused)]
fn main() {
pub struct GitHubConfig {
    pub token: SecretString,
    pub owner: Owner,
    pub repo: Repo,
    pub rate_limit_delay_ms: u64,
}
}
  • token is stored as secrecy::SecretString and masked in debug/display output
  • owner and repo are validated newtype wrappers over String
  • rate_limit_delay_ms adds a delay between GitHub API calls (default: 100ms)

Environment Variable Overrides

VariableOverrides
TOWL_GITHUB_TOKENgithub.token
TOWL_GITHUB_OWNERgithub.owner
TOWL_GITHUB_REPOgithub.repo

Owner / Repo

Validated newtype wrappers providing type safety:

#![allow(unused)]
fn main() {
pub struct Owner(String);
pub struct Repo(String);
}

try_new

#![allow(unused)]
fn main() {
pub fn try_new(s: impl Into<String>) -> Result<Self, TowlConfigError>
}

Constructs a new Owner or Repo, rejecting values exceeding MAX_CONFIG_STRING_LENGTH (512 characters).

Errors:

  • ConfigValueTooLong -- Value exceeds 512 characters

Both also implement:

  • Display, Default, Debug, Clone, PartialEq, Eq
  • Serialize, Deserialize

GitRepoInfo (internal)

#![allow(unused)]
fn main() {
pub(crate) struct GitRepoInfo {
    pub owner: Owner,
    pub repo: Repo,
}
}

from_path

#![allow(unused)]
fn main() {
pub(crate) async fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, TowlConfigError>
}

Internal function that discovers the git remote URL by running git remote get-url origin and parses the owner and repo name. Supports both HTTPS and SSH URL formats. Not part of the public API.

Errors:

  • GitRepoNotFound -- Not inside a git repository
  • GitRemoteNotFound -- No origin remote configured
  • GitInvalidUrl -- Could not parse owner/repo from the URL

Errors

#![allow(unused)]
fn main() {
pub enum TowlConfigError {
    PathTraversalAttempt(PathBuf),
    ConfigAlreadyExists(PathBuf),
    WriteToFileError(PathBuf, std::io::Error),
    UnableToParseToml(toml::ser::Error),
    CouldNotCreateConfig(ConfigError),
    GitRepoNotFound { message: String },
    GitRemoteNotFound { message: String },
    GitInvalidUrl { url: String, message: String },
    TooManyConfigPatterns { field: String, count: usize, max_allowed: usize },
    ConfigValueTooLong { field: String, length: usize, max_length: usize },
    ContextLinesOutOfRange { value: usize, min: usize, max: usize },
}
}

Constants

ConstantValuePurpose
DEFAULT_CONFIG_PATH.towl.tomlDefault config file name
MAX_CONFIG_PATTERNS100Maximum entries per pattern array
MAX_CONFIG_STRING_LENGTH512Maximum length for any single config string
MIN_CONTEXT_LINES1Minimum include_context_lines value
MAX_CONTEXT_LINES50Maximum include_context_lines value