Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-log

Structured event logging for Pudu: message templates, enrichment, filtering, formatting, and sinks

0.1.0Apache-2.01

InstallClose

Value.pudu

Pudu129 lines4.0 KB

GitHub ↗
1/** @Log.Value.Builders — constructors for captured property values */2module PuduLangLog.Value34import PuduLangLog.Domain.Display as Display5import PuduLangLog.Domain.Json as Json6import PuduLangLog as Log78/** @Log.Value.Capturable — a type that knows its captured form */9export trait Capturable {10  fn capture(self: &Self) -> Log.Value11}1213impl Capturable for Log.Value {14  /// The value itself.15  fn capture(self: &Self) -> Log.Value { *self }16}1718impl Capturable for Int {19  /// An integer scalar.20  fn capture(self: &Self) -> Log.Value { int(*self) }21}2223impl Capturable for Str {24  /// A text scalar.25  fn capture(self: &Self) -> Log.Value { text(*self) }26}2728impl Capturable for Bool {29  /// A boolean scalar.30  fn capture(self: &Self) -> Log.Value { bool(*self) }31}3233impl Capturable for Float64 {34  /// A float scalar.35  fn capture(self: &Self) -> Log.Value { float(*self) }36}3738impl Capturable for Char {39  /// A one-character text scalar.40  fn capture(self: &Self) -> Log.Value { text(self.toText()) }41}4243impl Capturable for Bytes {44  /// A byte-string scalar.45  fn capture(self: &Self) -> Log.Value { bytes(*self) }46}4748impl Capturable for Log.Timestamp {49  /// A moment scalar.50  fn capture(self: &Self) -> Log.Value { moment(*self) }51}5253impl [T: Capturable] Capturable for Array[T] {54  /// A sequence of each element's captured form.55  fn capture(self: &Self) -> Log.Value { Log.Sequence(self.map(|item: T| item.capture())) }56}5758impl [T: Capturable] Capturable for Option[T] {59  /// The held value's captured form, or null when absent.60  fn capture(self: &Self) -> Log.Value {61    match self {62      case Some(item) => item.capture()63      case None => nothing()64    }65  }66}6768/// Any capturable value in its captured form.69export fn of[T: Capturable](held: T) -> Log.Value { held.capture() }7071/// A text value.72export fn text(held: Str) -> Log.Value { Log.Scalar(Log.Text(held)) }7374/// An integer value.75export fn int(held: Int) -> Log.Value { Log.Scalar(Log.Integer(held)) }7677/// A float value.78export fn float(held: Float64) -> Log.Value { Log.Scalar(Log.Real(held)) }7980/// An exact decimal value.81export fn decimal(held: Decimal) -> Log.Value { Log.Scalar(Log.Exact(held)) }8283/// A boolean value.84export fn bool(held: Bool) -> Log.Value { Log.Scalar(Log.Boolean(held)) }8586/// The null value.87export fn nothing() -> Log.Value { Log.Scalar(Log.Null) }8889/// A moment value.90export fn moment(held: Log.Timestamp) -> Log.Value { Log.Scalar(Log.Moment(held)) }9192/// A duration value in milliseconds.93export fn duration(millis: Int) -> Log.Value { Log.Scalar(Log.Span(millis)) }9495/// A byte-string value.96export fn bytes(held: Bytes) -> Log.Value { Log.Scalar(Log.Binary(held)) }9798/// A sequence of values.99export fn list(items: Array[Log.Value]) -> Log.Value { Log.Sequence(items) }100101/// A structure with a type tag; destructure it with `@` in a template to keep its shape.102export fn structure(tag: Str, members: Array[(Str, Log.Value)]) -> Log.Value {103  Log.Structure(Log.Structure{tag: Some(tag), properties: members.map(|member: (Str, Log.Value)| property(member[0], member[1]))})104}105106/// A structure without a type tag.107export fn object(members: Array[(Str, Log.Value)]) -> Log.Value {108  Log.Structure(Log.Structure{tag: None, properties: members.map(|member: (Str, Log.Value)| property(member[0], member[1]))})109}110111/// A dictionary keyed by text.112export fn dictionary(entries: Array[(Str, Log.Value)]) -> Log.Value {113  Log.Dictionary(entries.map(|entry: (Str, Log.Value)| Log.Entry{key: Log.Text(entry[0]), value: entry[1]}))114}115116/// A dictionary with scalar keys of any kind.117export fn keyed(entries: Array[(Log.Scalar, Log.Value)]) -> Log.Value {118  Log.Dictionary(entries.map(|entry: (Log.Scalar, Log.Value)| Log.Entry{key: entry[0], value: entry[1]}))119}120121/// A named property.122export fn property(name: Str, held: Log.Value) -> Log.Property { Log.Property{name: name, value: held} }123124/// The value as display text, the way a message shows it.125export fn render(held: &Log.Value) -> Str { Display.plain(&Display.value(held, &None)) }126127/// The value as JSON.128export fn json(held: &Log.Value) -> Str { Json.value(held, &Some(Display.TYPE_TAG)) }129