Skip to main content

Module parser

Module parser 

Source
Expand description

Single-pass content parser for Nostr text-bearing events.

Nostr clients render kind:1 notes (and a long tail of similar kinds) by walking the .content string and recognising four syntactic affordances:

  • NIP-21 nostr: URIs (NIP-21) — embedded references to profiles, events, addressable coordinates, …;
  • HTTP(S) and other scheme URLs — to be rendered as hyperlinks;
  • #hashtags (NIP-12) — folksonomy markers that the t filter key indexes;
  • \n line breaks — for layout.

NostrParser is a single-pass tokeniser that yields the Token stream representing those affordances plus the interspersed plain-text spans. Callers configure which affordances to recognise via NostrParserOptions; a disabled affordance falls through to plain text untouched.

§Why this exists

Earlier nula releases shipped two narrowly-scoped scanners: crate::nips::nip27::references_in (NIP-21 only) and crate::nips::nip27::tags_from_content (tag harvester). Renderers had to layer their own URL / hashtag detection on top. This module unifies those concerns behind a single iterator so a UI layer can drive its rendering loop with one walk over the source string.

§Token kinds

VariantBorrow scopeSpec
Token::Text&'a strn/a
Token::Nostrowned Nip21NIP-21
Token::Urlowned Urln/a (RFC-3986)
Token::Hashtag&'a str (without leading #)NIP-12
Token::LineBreakn/an/a

§Example

use nula_core::parser::{NostrParser, NostrParserOptions, Token};

let parser = NostrParser::new();
let opts = NostrParserOptions::default();
let mut tokens = parser.parse("Visit https://example.com #rust", opts);
assert!(matches!(tokens.next(), Some(Token::Text(_))));
assert!(matches!(tokens.next(), Some(Token::Url(_))));

Structs§

NostrParser
Stateless factory that turns a &str into a NostrParserIter.
NostrParserIter
Iterator yielded by NostrParser::parse.
NostrParserOptions
Knobs that control which affordances NostrParser recognises.

Enums§

Token
One unit produced by NostrParser::parse.