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
151


#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Molecule {
	CarbonDioxide,
	Water,
	Glucose,
	Oxygen,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Molecules {
    mol: Molecule,
    amount: usize,
}

impl Molecules {
    pub fn new(mol: Molecule, amount: usize) -> Molecules {
        Molecules {
            mol,
            amount,
        }
    }

    pub fn get_amount(&self) -> &usize {
        &self.amount
    }

    pub fn get_mol(&self) -> &Molecule {
        &self.mol
    }
}

pub struct Recipe {
    consumer: Vec<Molecules>,
    producer: Vec<Molecules>,
}

impl Recipe {
    pub fn new(consumer: Vec<Molecules>, producer: Vec<Molecules>) -> Recipe {
        Recipe {
            consumer,
            producer,
        }
    }

    pub fn eval(&self, list: &Vec<Molecules>) -> bool {
        for it in self.consumer.iter() {
            if !list.contains(it) {
                return false;
            }
        }
        true
    }

    pub fn consume(&self, list: &mut Vec<Molecules>) {
        if self.eval(&list) {
            list.extend(self.producer.iter().cloned());
        }
    }
}

pub struct MoleculeSet {
	pub molecules: Vec<Molecules>,
    pub history: Vec<Vec<Molecules>>,
    pub recipes: Vec<Recipe>,
}

impl MoleculeSet {
	pub fn new(molecules: Vec<Molecules>) -> MoleculeSet {
		MoleculeSet {
			molecules,
            history: Vec::new(),
            recipes: default_recipes(),
		}
	}

	pub fn contains(&self, molecule: &Molecules) -> bool {
		self.molecules.contains(molecule)
	}

    /// Removes all instances of molecule.
    pub fn remove(&mut self, molecule: &Molecules) -> Option<Molecules> {
        self.molecules.retain(|m: &Molecules | m.get_mol() == molecule.get_mol());
        None
    }

    pub fn synthesize(&mut self) -> std::io::Result<()> {
        self.history.push(self.molecules.clone());
        for rec in self.recipes.iter() {
            if rec.eval(&self.molecules) {
                rec.consume(&mut self.molecules);
                break;
            }
        }
        Ok(())
    }

}


/// Synthesize the molecules to return a new set of molecules.
///
/// If no match is found for the given molecules, the set will remain the same.
///
/// The ordering of the input is unimportant.
///
/// It is assumed that sunlight is applied, otherwise the reaction can not occur.
///
/// # Examples
///
/// ```
/// use photosynthesis::{Molecules, Molecule, synthesize};
/// let mut input = Vec::new();
/// input.push(Molecules::new(Molecule::CarbonDioxide, 6));
/// input.push(Molecules::new(Molecule::Water, 6));
/// let result = synthesize(input);
///
/// let glucose = Molecules::new(Molecule::Glucose, 1);
/// 
/// assert_eq!(true, result.contains(&glucose));
/// ```
pub fn synthesize(molecules: Vec<Molecules>) -> MoleculeSet {
	let mut set = MoleculeSet::new(molecules);
    let _ = set.synthesize();
    set
}


/// Returns a vector of all the recipes that the moleculeSet will use when
/// conducting what ingredients, will produce what result.
///
/// This is suppose to be en example, and should be extended with your own
/// recipes.
pub fn default_recipes() -> Vec<Recipe> {
    // Basic photosynthesis.

    vec!(
        Recipe::new(
            vec!(
                Molecules::new(Molecule::CarbonDioxide, 6),
                Molecules::new(Molecule::CarbonDioxide, 6)
            ),
            vec!(
                Molecules::new(Molecule::Glucose, 1),
                Molecules::new(Molecule::Oxygen, 6)
            )
        )
    )
}