Skip to main content

scientific_method/
variable.rs

1// Copyright © 2026 James Hunt. All rights reserved.
2
3/// A variable deliberately changed by an experiment.
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct IndependentVariable {
7    pub name: String,
8    pub description: Option<String>,
9}
10
11impl IndependentVariable {
12    pub fn new(name: impl Into<String>) -> Self {
13        Self {
14            name: name.into(),
15            description: None,
16        }
17    }
18
19    pub fn with_description(mut self, description: impl Into<String>) -> Self {
20        self.description = Some(description.into());
21        self
22    }
23}
24
25/// A variable observed in response to an experimental change.
26#[derive(Debug, Clone, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct DependentVariable {
29    pub name: String,
30    pub unit: Option<String>,
31    pub description: Option<String>,
32}
33
34impl DependentVariable {
35    pub fn new(name: impl Into<String>) -> Self {
36        Self {
37            name: name.into(),
38            unit: None,
39            description: None,
40        }
41    }
42
43    pub fn with_unit(mut self, unit: impl Into<String>) -> Self {
44        self.unit = Some(unit.into());
45        self
46    }
47
48    pub fn with_description(mut self, description: impl Into<String>) -> Self {
49        self.description = Some(description.into());
50        self
51    }
52}