
Value.pudu
Pudu129 lines4.0 KB
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 15 fn capture(self: &Self) -> Log.Value { *self }16}1718impl Capturable for Int {19 20 fn capture(self: &Self) -> Log.Value { int(*self) }21}2223impl Capturable for Str {24 25 fn capture(self: &Self) -> Log.Value { text(*self) }26}2728impl Capturable for Bool {29 30 fn capture(self: &Self) -> Log.Value { bool(*self) }31}3233impl Capturable for Float64 {34 35 fn capture(self: &Self) -> Log.Value { float(*self) }36}3738impl Capturable for Char {39 40 fn capture(self: &Self) -> Log.Value { text(self.toText()) }41}4243impl Capturable for Bytes {44 45 fn capture(self: &Self) -> Log.Value { bytes(*self) }46}4748impl Capturable for Log.Timestamp {49 50 fn capture(self: &Self) -> Log.Value { moment(*self) }51}5253impl [T: Capturable] Capturable for Array[T] {54 55 fn capture(self: &Self) -> Log.Value { Log.Sequence(self.map(|item: T| item.capture())) }56}5758impl [T: Capturable] Capturable for Option[T] {59 60 fn capture(self: &Self) -> Log.Value {61 match self {62 case Some(item) => item.capture()63 case None => nothing()64 }65 }66}676869export fn of[T: Capturable](held: T) -> Log.Value { held.capture() }707172export fn text(held: Str) -> Log.Value { Log.Scalar(Log.Text(held)) }737475export fn int(held: Int) -> Log.Value { Log.Scalar(Log.Integer(held)) }767778export fn float(held: Float64) -> Log.Value { Log.Scalar(Log.Real(held)) }798081export fn decimal(held: Decimal) -> Log.Value { Log.Scalar(Log.Exact(held)) }828384export fn bool(held: Bool) -> Log.Value { Log.Scalar(Log.Boolean(held)) }858687export fn nothing() -> Log.Value { Log.Scalar(Log.Null) }888990export fn moment(held: Log.Timestamp) -> Log.Value { Log.Scalar(Log.Moment(held)) }919293export fn duration(millis: Int) -> Log.Value { Log.Scalar(Log.Span(millis)) }949596export fn bytes(held: Bytes) -> Log.Value { Log.Scalar(Log.Binary(held)) }979899export fn list(items: Array[Log.Value]) -> Log.Value { Log.Sequence(items) }100101102export 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}105106107export 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}110111112export 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}115116117export 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}120121122export fn property(name: Str, held: Log.Value) -> Log.Property { Log.Property{name: name, value: held} }123124125export fn render(held: &Log.Value) -> Str { Display.plain(&Display.value(held, &None)) }126127128export fn json(held: &Log.Value) -> Str { Json.value(held, &Some(Display.TYPE_TAG)) }129