1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Module containing the voltage sensors and their related functionality.

use super::*;
use crate::hwmon::*;
use crate::units::Voltage;
use crate::{Parseable, ParsingResult};

#[cfg(feature = "writable")]
use std::convert::TryFrom;
use std::path::{Path, PathBuf};

/// Trait implemented by all voltage sensors.
pub trait VoltSensor: SensorBase {}

impl<S: VoltSensor> Sensor<Voltage> for S {}
impl<S: VoltSensor> Min<Voltage> for S {}
impl<S: VoltSensor> Max<Voltage> for S {}
impl<S: VoltSensor> LowCrit<Voltage> for S {}
impl<S: VoltSensor> Crit<Voltage> for S {}
impl<S: VoltSensor> Average<Voltage> for S {}
impl<S: VoltSensor> Lowest<Voltage> for S {}
impl<S: VoltSensor> Highest<Voltage> for S {}

/// Struct that represents a read only voltage sensor.
#[derive(Debug, Clone)]
pub struct ReadOnlyVolt {
    hwmon_path: PathBuf,
    index: u16,
}

#[cfg(feature = "writable")]
impl ReadOnlyVolt {
    /// Try converting this sensor into a read-write version of itself.
    pub fn try_into_read_write(self) -> Result<ReadWriteVolt> {
        let read_write = ReadWriteVolt {
            hwmon_path: self.hwmon_path,
            index: self.index,
        };

        if read_write.supported_write_sub_functions().is_empty() {
            return Err(Error::InsufficientRights {
                path: read_write.hwmon_path.join(format!(
                    "{}{}",
                    read_write.base(),
                    read_write.index(),
                )),
            });
        }

        Ok(read_write)
    }
}

impl SensorBase for ReadOnlyVolt {
    fn base(&self) -> &'static str {
        "in"
    }

    fn index(&self) -> u16 {
        self.index
    }

    fn hwmon_path(&self) -> &Path {
        self.hwmon_path.as_path()
    }
}

impl Parseable for ReadOnlyVolt {
    type Parent = ReadOnlyHwmon;

    fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
        let volt = Self {
            hwmon_path: parent.path().to_path_buf(),
            index,
        };

        inspect_sensor(volt)
    }
}

impl VoltSensor for ReadOnlyVolt {}

#[cfg(feature = "writable")]
impl From<ReadWriteVolt> for ReadOnlyVolt {
    fn from(write_voltage: ReadWriteVolt) -> ReadOnlyVolt {
        write_voltage.into_read_only()
    }
}

/// Struct that represents a read/write voltage sensor.
#[cfg(feature = "writable")]
#[derive(Debug, Clone)]
pub struct ReadWriteVolt {
    hwmon_path: PathBuf,
    index: u16,
}

#[cfg(feature = "writable")]
impl ReadWriteVolt {
    /// Converts this sensor into a read-only version of itself.
    pub fn into_read_only(self) -> ReadOnlyVolt {
        ReadOnlyVolt {
            hwmon_path: self.hwmon_path,
            index: self.index,
        }
    }
}

#[cfg(feature = "writable")]
impl SensorBase for ReadWriteVolt {
    fn base(&self) -> &'static str {
        "in"
    }

    fn index(&self) -> u16 {
        self.index
    }

    fn hwmon_path(&self) -> &Path {
        self.hwmon_path.as_path()
    }
}

#[cfg(feature = "writable")]
impl Parseable for ReadWriteVolt {
    type Parent = ReadWriteHwmon;

    fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
        let volt = Self {
            hwmon_path: parent.path().to_path_buf(),
            index,
        };

        inspect_sensor(volt)
    }
}

#[cfg(feature = "writable")]
impl VoltSensor for ReadWriteVolt {}
#[cfg(feature = "writable")]
impl WritableSensorBase for ReadWriteVolt {}

#[cfg(feature = "writable")]
impl TryFrom<ReadOnlyVolt> for ReadWriteVolt {
    type Error = Error;

    fn try_from(read_only: ReadOnlyVolt) -> std::result::Result<Self, Self::Error> {
        read_only.try_into_read_write()
    }
}