Skip to content

Commit

Permalink
Document resolvers in the fluent-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatum committed Oct 27, 2022
1 parent 1f74f26 commit 03af84f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions fluent-bundle/src/resolver/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use fluent_syntax::ast::InlineExpression;
use std::error::Error;

/// Maps an [`InlineExpression`] into the kind of reference, with owned strings
/// that identify the expression. This makes it so that the [`InlineExpression`] can
/// be used to generate an error string.
#[derive(Debug, PartialEq, Clone)]
pub enum ReferenceKind {
Function {
Expand Down Expand Up @@ -44,6 +47,8 @@ where
}
}

/// Errors generated during the process of resolving a fluent message into a string.
/// This process takes place in the `write` method of the `WriteValue` trait.
#[derive(Debug, PartialEq, Clone)]
pub enum ResolverError {
Reference(ReferenceKind),
Expand Down
12 changes: 11 additions & 1 deletion fluent-bundle/src/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! The `resolver` module contains the definitions and implementations for the internal
//! `ResolveValue` and `WriteValue` traits. The former converts AST nodes to a
//! [`FluentValue`], and the latter converts them to a string that is written to an
//! implementor of the [`std::fmt::Write`] trait.

pub mod errors;
mod expression;
mod inline_expression;
Expand All @@ -14,8 +19,9 @@ use crate::memoizer::MemoizerKind;
use crate::resource::FluentResource;
use crate::types::FluentValue;

// Converts an AST node to a `FluentValue`.
/// Resolves an AST node to a [`FluentValue`].
pub(crate) trait ResolveValue {
/// Resolves an AST node to a [`FluentValue`].
fn resolve<'source, 'errors, R, M>(
&'source self,
scope: &mut Scope<'source, 'errors, R, M>,
Expand All @@ -25,7 +31,9 @@ pub(crate) trait ResolveValue {
M: MemoizerKind;
}

/// Resolves and AST node to a string that is written to source `W`.
pub(crate) trait WriteValue {
/// Resolves and AST node to a string that is written to source `W`.
fn write<'source, 'errors, W, R, M>(
&'source self,
w: &mut W,
Expand All @@ -36,6 +44,8 @@ pub(crate) trait WriteValue {
R: Borrow<FluentResource>,
M: MemoizerKind;

/// Writes error information to `W`. This can be used to add FTL errors inline
/// to a message.
fn write_error<W>(&self, _w: &mut W) -> fmt::Result
where
W: fmt::Write;
Expand Down
11 changes: 5 additions & 6 deletions fluent-bundle/src/resolver/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ impl<'scope, 'errors, R, M> Scope<'scope, 'errors, R, M> {
}
}

// This method allows us to lazily add Pattern on the stack,
// only if the Pattern::resolve has been called on an empty stack.
//
// This is the case when pattern is called from Bundle and it
// allows us to fast-path simple resolutions, and only use the stack
// for placeables.
/// This method allows us to lazily add Pattern on the stack, only if the
/// Pattern::resolve has been called on an empty stack.
///
/// This is the case when pattern is called from Bundle and it allows us to fast-path
/// simple resolutions, and only use the stack for placeables.
pub fn maybe_track<W>(
&mut self,
w: &mut W,
Expand Down

0 comments on commit 03af84f

Please sign in to comment.