-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out genetic algorithm harness
- Loading branch information
Showing
9 changed files
with
378 additions
and
277 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::{ | ||
fs::File, | ||
io::{self, stderr, Stderr}, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum FileOrStderr { | ||
FilePath(String), | ||
Stderr, | ||
} | ||
|
||
impl From<String> for FileOrStderr { | ||
fn from(value: String) -> Self { | ||
if &value == "-" { | ||
Self::Stderr | ||
} else { | ||
Self::FilePath(value) | ||
} | ||
} | ||
} | ||
|
||
impl FileOrStderr { | ||
fn create_with<'a, F>(&'a self, f: F) -> io::Result<FileOrStderrWriter> | ||
where | ||
F: FnOnce(&'a str) -> io::Result<File>, | ||
{ | ||
match self { | ||
FileOrStderr::FilePath(ref fp) => Ok(FileOrStderrWriter::File(f(fp)?)), | ||
FileOrStderr::Stderr => Ok(FileOrStderrWriter::Stderr(stderr())), | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn open(&self) -> io::Result<FileOrStderrWriter> { | ||
self.create_with(File::open) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn create(&self) -> io::Result<FileOrStderrWriter> { | ||
self.create_with(File::create) | ||
} | ||
} | ||
|
||
pub enum FileOrStderrWriter { | ||
File(File), | ||
Stderr(Stderr), | ||
} | ||
|
||
impl io::Write for FileOrStderrWriter { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
match self { | ||
FileOrStderrWriter::File(f) => f.write(buf), | ||
FileOrStderrWriter::Stderr(stderr) => { | ||
let mut lock = stderr.lock(); | ||
lock.write(buf) | ||
} | ||
} | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
match self { | ||
FileOrStderrWriter::File(f) => f.flush(), | ||
FileOrStderrWriter::Stderr(stderr) => { | ||
let mut lock = stderr.lock(); | ||
lock.flush() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.