This commit is contained in:
Josh Thomas 2025-01-06 23:20:43 -06:00
parent 1bf4f8390e
commit 54f19d0c37
2 changed files with 23 additions and 11 deletions

View file

@ -109,11 +109,12 @@ impl From<Token> for Span {
#[derive(Clone, Debug, Serialize)]
pub enum Node {
Text {
Block(Block),
Comment {
content: String,
span: Span,
},
Comment {
Text {
content: String,
span: Span,
},
@ -122,16 +123,15 @@ pub enum Node {
filters: Vec<DjangoFilter>,
span: Span,
},
Block(Block),
}
impl Node {
pub fn span(&self) -> Option<&Span> {
match self {
Node::Text { span, .. } => Some(span),
Node::Comment { span, .. } => Some(span),
Node::Variable { span, .. } => Some(span),
Node::Block(block) => Some(&block.tag().span),
Node::Comment { span, .. } => Some(span),
Node::Text { span, .. } => Some(span),
Node::Variable { span, .. } => Some(span),
}
}
@ -169,18 +169,18 @@ pub enum Block {
impl Block {
pub fn tag(&self) -> &Tag {
match self {
Self::Container { tag, .. }
| Self::Branch { tag, .. }
| Self::Single { tag }
Self::Branch { tag, .. }
| Self::Container { tag, .. }
| Self::Closing { tag }
| Self::Inclusion { tag, .. }
| Self::Closing { tag } => tag,
| Self::Single { tag } => tag,
}
}
pub fn nodes(&self) -> Option<&Vec<Node>> {
match self {
Block::Container { nodes, .. } => Some(nodes),
Block::Branch { nodes, .. } => Some(nodes),
Block::Container { nodes, .. } => Some(nodes),
_ => None,
}
}

View file

@ -68,6 +68,10 @@ impl Worker {
}
}
/// Attempts to execute a task immediately without waiting.
/// Returns an error if the worker's channel is full.
///
/// Best for non-critical tasks where backpressure is desired.
pub fn execute<T>(&self, task: T) -> Result<()>
where
T: Task + 'static,
@ -78,6 +82,10 @@ impl Worker {
.map_err(|e| anyhow::anyhow!("Failed to execute task: {}", e))
}
/// Submits a task asynchronously, waiting if the channel is full.
///
/// Good for tasks that must be processed but where you don't need
/// the result immediately.
pub async fn submit<T>(&self, task: T) -> Result<()>
where
T: Task + 'static,
@ -89,6 +97,10 @@ impl Worker {
.map_err(|e| anyhow::anyhow!("Failed to submit task: {}", e))
}
/// Submits a task and waits for its result.
///
/// Best when you need the output of the task. This method will
/// wait both for space to submit the task and for its completion.
pub async fn wait_for<T>(&self, task: T) -> Result<T::Output>
where
T: Task + 'static,