Skip to content

Commit 7413b61

Browse files
authored
feat(encoding): impl EncodeLabelValue for bool (#237)
Signed-off-by: koushiro <[email protected]>
1 parent d3e3637 commit 7413b61

File tree

2 files changed

+99
-87
lines changed

2 files changed

+99
-87
lines changed

Diff for: CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Implement `Atomic<u64>` for `AtomicU64` for gauges.
1919
See [PR 226].
2020

21-
[PR 226]: https://github.com/prometheus/client_rust/pull/198
21+
- Implement `EnableLabelValue` for `bool`.
22+
See [PR 237]
23+
24+
[PR 173]: https://github.com/prometheus/client_rust/pull/173
2225
[PR 198]: https://github.com/prometheus/client_rust/pull/198
26+
[PR 226]: https://github.com/prometheus/client_rust/pull/226
27+
[PR 237]: https://github.com/prometheus/client_rust/pull/237
2328

2429
### Added
2530

Diff for: src/encoding.rs

+93-86
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ pub trait EncodeLabelSet {
206206
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error>;
207207
}
208208

209-
impl<'a> From<text::LabelSetEncoder<'a>> for LabelSetEncoder<'a> {
210-
fn from(e: text::LabelSetEncoder<'a>) -> Self {
211-
Self(LabelSetEncoderInner::Text(e))
212-
}
213-
}
214-
215209
/// Encoder for a label set.
216210
#[derive(Debug)]
217211
pub struct LabelSetEncoder<'a>(LabelSetEncoderInner<'a>);
@@ -223,6 +217,12 @@ enum LabelSetEncoderInner<'a> {
223217
Protobuf(protobuf::LabelSetEncoder<'a>),
224218
}
225219

220+
impl<'a> From<text::LabelSetEncoder<'a>> for LabelSetEncoder<'a> {
221+
fn from(e: text::LabelSetEncoder<'a>) -> Self {
222+
Self(LabelSetEncoderInner::Text(e))
223+
}
224+
}
225+
226226
#[cfg(feature = "protobuf")]
227227
impl<'a> From<protobuf::LabelSetEncoder<'a>> for LabelSetEncoder<'a> {
228228
fn from(e: protobuf::LabelSetEncoder<'a>) -> Self {
@@ -237,6 +237,42 @@ impl LabelSetEncoder<'_> {
237237
}
238238
}
239239

240+
impl<T: EncodeLabel, const N: usize> EncodeLabelSet for [T; N] {
241+
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
242+
self.as_ref().encode(encoder)
243+
}
244+
}
245+
246+
impl<T: EncodeLabel> EncodeLabelSet for &[T] {
247+
fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
248+
if self.is_empty() {
249+
return Ok(());
250+
}
251+
252+
for label in self.iter() {
253+
label.encode(encoder.encode_label())?
254+
}
255+
256+
Ok(())
257+
}
258+
}
259+
260+
impl<T: EncodeLabel> EncodeLabelSet for Vec<T> {
261+
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
262+
self.as_slice().encode(encoder)
263+
}
264+
}
265+
266+
/// Uninhabited type to represent the lack of a label set for a metric
267+
#[derive(Debug)]
268+
pub enum NoLabelSet {}
269+
270+
impl EncodeLabelSet for NoLabelSet {
271+
fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
272+
Ok(())
273+
}
274+
}
275+
240276
/// An encodable label.
241277
pub trait EncodeLabel {
242278
/// Encode oneself into the given encoder.
@@ -247,10 +283,6 @@ pub trait EncodeLabel {
247283
#[derive(Debug)]
248284
pub struct LabelEncoder<'a>(LabelEncoderInner<'a>);
249285

250-
/// Uninhabited type to represent the lack of a label set for a metric
251-
#[derive(Debug)]
252-
pub enum NoLabelSet {}
253-
254286
#[derive(Debug)]
255287
enum LabelEncoderInner<'a> {
256288
Text(text::LabelEncoder<'a>),
@@ -283,6 +315,21 @@ impl LabelEncoder<'_> {
283315
}
284316
}
285317

318+
impl<K: EncodeLabelKey, V: EncodeLabelValue> EncodeLabel for (K, V) {
319+
fn encode(&self, mut encoder: LabelEncoder) -> Result<(), std::fmt::Error> {
320+
let (key, value) = self;
321+
322+
let mut label_key_encoder = encoder.encode_label_key()?;
323+
key.encode(&mut label_key_encoder)?;
324+
325+
let mut label_value_encoder = label_key_encoder.encode_label_value()?;
326+
value.encode(&mut label_value_encoder)?;
327+
label_value_encoder.finish()?;
328+
329+
Ok(())
330+
}
331+
}
332+
286333
/// An encodable label key.
287334
pub trait EncodeLabelKey {
288335
/// Encode oneself into the given encoder.
@@ -330,52 +377,6 @@ impl<'a> LabelKeyEncoder<'a> {
330377
)
331378
}
332379
}
333-
impl<T: EncodeLabel, const N: usize> EncodeLabelSet for [T; N] {
334-
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
335-
self.as_ref().encode(encoder)
336-
}
337-
}
338-
339-
impl<T: EncodeLabel> EncodeLabelSet for &[T] {
340-
fn encode(&self, mut encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
341-
if self.is_empty() {
342-
return Ok(());
343-
}
344-
345-
for label in self.iter() {
346-
label.encode(encoder.encode_label())?
347-
}
348-
349-
Ok(())
350-
}
351-
}
352-
353-
impl<T: EncodeLabel> EncodeLabelSet for Vec<T> {
354-
fn encode(&self, encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
355-
self.as_slice().encode(encoder)
356-
}
357-
}
358-
359-
impl EncodeLabelSet for NoLabelSet {
360-
fn encode(&self, _encoder: LabelSetEncoder) -> Result<(), std::fmt::Error> {
361-
Ok(())
362-
}
363-
}
364-
365-
impl<K: EncodeLabelKey, V: EncodeLabelValue> EncodeLabel for (K, V) {
366-
fn encode(&self, mut encoder: LabelEncoder) -> Result<(), std::fmt::Error> {
367-
let (key, value) = self;
368-
369-
let mut label_key_encoder = encoder.encode_label_key()?;
370-
key.encode(&mut label_key_encoder)?;
371-
372-
let mut label_value_encoder = label_key_encoder.encode_label_value()?;
373-
value.encode(&mut label_value_encoder)?;
374-
label_value_encoder.finish()?;
375-
376-
Ok(())
377-
}
378-
}
379380

380381
impl EncodeLabelKey for &str {
381382
fn encode(&self, encoder: &mut LabelKeyEncoder) -> Result<(), std::fmt::Error> {
@@ -433,6 +434,13 @@ pub trait EncodeLabelValue {
433434
#[derive(Debug)]
434435
pub struct LabelValueEncoder<'a>(LabelValueEncoderInner<'a>);
435436

437+
#[derive(Debug)]
438+
enum LabelValueEncoderInner<'a> {
439+
Text(text::LabelValueEncoder<'a>),
440+
#[cfg(feature = "protobuf")]
441+
Protobuf(protobuf::LabelValueEncoder<'a>),
442+
}
443+
436444
impl<'a> From<text::LabelValueEncoder<'a>> for LabelValueEncoder<'a> {
437445
fn from(e: text::LabelValueEncoder<'a>) -> Self {
438446
LabelValueEncoder(LabelValueEncoderInner::Text(e))
@@ -446,11 +454,10 @@ impl<'a> From<protobuf::LabelValueEncoder<'a>> for LabelValueEncoder<'a> {
446454
}
447455
}
448456

449-
#[derive(Debug)]
450-
enum LabelValueEncoderInner<'a> {
451-
Text(text::LabelValueEncoder<'a>),
452-
#[cfg(feature = "protobuf")]
453-
Protobuf(protobuf::LabelValueEncoder<'a>),
457+
impl std::fmt::Write for LabelValueEncoder<'_> {
458+
fn write_str(&mut self, s: &str) -> std::fmt::Result {
459+
for_both_mut!(self, LabelValueEncoderInner, e, e.write_str(s))
460+
}
454461
}
455462

456463
impl LabelValueEncoder<'_> {
@@ -460,12 +467,6 @@ impl LabelValueEncoder<'_> {
460467
}
461468
}
462469

463-
impl std::fmt::Write for LabelValueEncoder<'_> {
464-
fn write_str(&mut self, s: &str) -> std::fmt::Result {
465-
for_both_mut!(self, LabelValueEncoderInner, e, e.write_str(s))
466-
}
467-
}
468-
469470
impl EncodeLabelValue for &str {
470471
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
471472
encoder.write_str(self)?;
@@ -536,6 +537,12 @@ where
536537
}
537538
}
538539

540+
impl EncodeLabelValue for bool {
541+
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
542+
encoder.write_str(if *self { "true" } else { "false" })
543+
}
544+
}
545+
539546
macro_rules! impl_encode_label_value_for_integer {
540547
($($t:ident),*) => {$(
541548
impl EncodeLabelValue for $t {
@@ -678,6 +685,19 @@ enum CounterValueEncoderInner<'a> {
678685
Protobuf(protobuf::CounterValueEncoder<'a>),
679686
}
680687

688+
impl<'a> From<text::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
689+
fn from(e: text::CounterValueEncoder<'a>) -> Self {
690+
CounterValueEncoder(CounterValueEncoderInner::Text(e))
691+
}
692+
}
693+
694+
#[cfg(feature = "protobuf")]
695+
impl<'a> From<protobuf::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
696+
fn from(e: protobuf::CounterValueEncoder<'a>) -> Self {
697+
CounterValueEncoder(CounterValueEncoderInner::Protobuf(e))
698+
}
699+
}
700+
681701
impl CounterValueEncoder<'_> {
682702
fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
683703
for_both_mut!(self, CounterValueEncoderInner, e, e.encode_f64(v))
@@ -718,19 +738,6 @@ impl EncodeExemplarValue for u32 {
718738
}
719739
}
720740

721-
impl<'a> From<text::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
722-
fn from(e: text::CounterValueEncoder<'a>) -> Self {
723-
CounterValueEncoder(CounterValueEncoderInner::Text(e))
724-
}
725-
}
726-
727-
#[cfg(feature = "protobuf")]
728-
impl<'a> From<protobuf::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
729-
fn from(e: protobuf::CounterValueEncoder<'a>) -> Self {
730-
CounterValueEncoder(CounterValueEncoderInner::Protobuf(e))
731-
}
732-
}
733-
734741
/// Encoder for an exemplar value.
735742
#[derive(Debug)]
736743
pub struct ExemplarValueEncoder<'a>(ExemplarValueEncoderInner<'a>);
@@ -742,12 +749,6 @@ enum ExemplarValueEncoderInner<'a> {
742749
Protobuf(protobuf::ExemplarValueEncoder<'a>),
743750
}
744751

745-
impl ExemplarValueEncoder<'_> {
746-
fn encode(&mut self, v: f64) -> Result<(), std::fmt::Error> {
747-
for_both_mut!(self, ExemplarValueEncoderInner, e, e.encode(v))
748-
}
749-
}
750-
751752
impl<'a> From<text::ExemplarValueEncoder<'a>> for ExemplarValueEncoder<'a> {
752753
fn from(e: text::ExemplarValueEncoder<'a>) -> Self {
753754
ExemplarValueEncoder(ExemplarValueEncoderInner::Text(e))
@@ -760,3 +761,9 @@ impl<'a> From<protobuf::ExemplarValueEncoder<'a>> for ExemplarValueEncoder<'a> {
760761
ExemplarValueEncoder(ExemplarValueEncoderInner::Protobuf(e))
761762
}
762763
}
764+
765+
impl ExemplarValueEncoder<'_> {
766+
fn encode(&mut self, v: f64) -> Result<(), std::fmt::Error> {
767+
for_both_mut!(self, ExemplarValueEncoderInner, e, e.encode(v))
768+
}
769+
}

0 commit comments

Comments
 (0)