use crate::snapshot::*;
use crate::util;
use crate::{Descriptive, PutsSnapshot};
pub struct PollingInstrument<P> {
pub name: String,
pub title: Option<String>,
pub description: Option<String>,
pub poll: P,
pub create_group_with_name: bool,
}
impl<P> PollingInstrument<P>
where
P: PutsSnapshot,
{
pub fn new_with_defaults<T: Into<String>>(name: T, poll: P) -> PollingInstrument<P> {
PollingInstrument {
name: name.into(),
title: None,
description: None,
poll,
create_group_with_name: false,
}
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = name.into();
}
pub fn create_group_with_name(&self) -> bool {
self.create_group_with_name
}
pub fn set_create_group_with_name(&mut self, create_group: bool) {
self.create_group_with_name = create_group;
}
pub fn set_title<T: Into<String>>(&mut self, title: T) {
self.title = Some(title.into())
}
pub fn set_description<T: Into<String>>(&mut self, description: T) {
self.description = Some(description.into())
}
fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
self.poll.put_snapshot(into, descriptive);
}
}
impl<P> Descriptive for PollingInstrument<P> {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}
impl<P> PutsSnapshot for PollingInstrument<P>
where
P: PutsSnapshot,
{
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_postfixed_descriptives(self, &self.name, into, descriptive);
if self.create_group_with_name {
let mut new_level = Snapshot::default();
self.put_values_into_snapshot(&mut new_level, descriptive);
into.items
.push((self.name.clone(), ItemKind::Snapshot(new_level)));
} else {
self.put_values_into_snapshot(into, descriptive);
}
}
}
pub struct ConstantValue {
name: String,
title: Option<String>,
description: Option<String>,
value: ItemKind,
}
impl ConstantValue {
pub fn new<T: Into<String>, V: Into<ItemKind>>(name: T, value: V) -> Self {
ConstantValue {
name: name.into(),
title: None,
description: None,
value: value.into(),
}
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = name.into();
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn set_title<T: Into<String>>(&mut self, title: T) {
self.title = Some(title.into())
}
pub fn set_description<T: Into<String>>(&mut self, description: T) {
self.description = Some(description.into())
}
}
impl PutsSnapshot for ConstantValue {
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_postfixed_descriptives(self, &self.name, into, descriptive);
into.items.push((self.name.clone(), self.value.clone()));
}
}
impl Descriptive for ConstantValue {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}