Skip to content

Commit

Permalink
Rename: s/Command/ImageCommand/
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Sep 24, 2023
1 parent d50e9d1 commit 002036b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 72 deletions.
16 changes: 10 additions & 6 deletions examples/gen_copic_palette.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use copic_colors::{Family, Group, Value};
use orfail::OrFail;
use pati::{Color, Command, Point};
use pati::{Color, ImageCommand, Point};
use std::{
collections::{BTreeMap, HashMap},
io::Write,
Expand All @@ -14,14 +14,18 @@ fn main() -> pagurus::Result<()> {
add_background(&mut pixels);
add_colors(&mut pixels);

write_command(Command::draw_pixels(pixels.into_iter())).or_fail()?;
write_command(Command::anchor("palette.start", Some(Point::new(0, 0)))).or_fail()?;
write_command(Command::anchor(
write_command(ImageCommand::draw_pixels(pixels.into_iter())).or_fail()?;
write_command(ImageCommand::anchor(
"palette.start",
Some(Point::new(0, 0)),
))
.or_fail()?;
write_command(ImageCommand::anchor(
"palette.end",
Some(Point::new(PALETTE_WIDTH - 1, PALETTE_HEIGHT - 1)),
))
.or_fail()?;
write_command(Command::anchor(
write_command(ImageCommand::anchor(
"origin",
Some(Point::new(PALETTE_WIDTH / 2, PALETTE_HEIGHT / 2)),
))
Expand All @@ -30,7 +34,7 @@ fn main() -> pagurus::Result<()> {
Ok(())
}

fn write_command(command: Command) -> orfail::Result<()> {
fn write_command(command: ImageCommand) -> orfail::Result<()> {
let mut stdout = std::io::stdout();
serde_json::to_writer(&mut stdout, &command).or_fail()?;
writeln!(&mut stdout).or_fail()?;
Expand Down
38 changes: 19 additions & 19 deletions pati/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::{
io::{BufRead, Write},
};

/// [`Canvas`][crate::Canvas] command.
/// [`Image`][crate::Image] command.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Command {
pub enum ImageCommand {
/// Patch command.
Patch(PatchCommand),
Patch(PatchImageCommand),

/// Anchor command.
Anchor {
Expand All @@ -31,13 +31,13 @@ pub enum Command {
},
}

impl Command {
impl ImageCommand {
/// Make a patch command from the given patch entries.
pub const fn patch(entries: Vec<PatchEntry>) -> Self {
Self::Patch(PatchCommand::new(entries))
Self::Patch(PatchImageCommand::new(entries))
}

/// Makes a patch command to draw the given pixels.
/// Makes a patch command to draw the given pi xels.
pub fn draw_pixels(pixels: impl Iterator<Item = (Point, Color)>) -> Self {
let mut entries = BTreeMap::new();
for (point, color) in pixels {
Expand Down Expand Up @@ -72,10 +72,10 @@ impl Command {

/// Patch command that is used to draw or erase pixels.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatchCommand(Vec<PatchEntry>);
pub struct PatchImageCommand(Vec<PatchEntry>);

impl PatchCommand {
/// Makes a new [`PatchCommand`] instance.
impl PatchImageCommand {
/// Makes a new [`PatchImageCommand`] instance.
pub const fn new(entries: Vec<PatchEntry>) -> Self {
Self(entries)
}
Expand Down Expand Up @@ -116,36 +116,36 @@ impl PatchEntry {
}
}

/// [`Command`] writer.
/// [`ImageCommand`] writer.
#[derive(Debug)]
pub struct CommandWriter<W> {
pub struct ImageCommandWriter<W> {
inner: W,
}

impl<W: Write> CommandWriter<W> {
/// Makes a new [`CommandWriter`] instance.
impl<W: Write> ImageCommandWriter<W> {
/// Makes a new [`ImageCommandWriter`] instance.
pub const fn new(inner: W) -> Self {
Self { inner }
}

/// Writes the given command.
pub fn write_command(&mut self, command: &Command) -> std::io::Result<()> {
pub fn write_command(&mut self, command: &ImageCommand) -> std::io::Result<()> {
serde_json::to_writer(&mut self.inner, command)?;
writeln!(self.inner)?;
self.inner.flush()?;
Ok(())
}
}

/// [`Command`] reader.
/// [`ImageCommand`] reader.
#[derive(Debug)]
pub struct CommandReader<R> {
pub struct ImageCommandReader<R> {
inner: R,
line: String,
}

impl<R: BufRead> CommandReader<R> {
/// Makes a new [`CommandReader`] instance.
impl<R: BufRead> ImageCommandReader<R> {
/// Makes a new [`ImageCommandReader`] instance.
pub const fn new(inner: R) -> Self {
Self {
inner,
Expand All @@ -154,7 +154,7 @@ impl<R: BufRead> CommandReader<R> {
}

/// Reads a command.
pub fn read_command(&mut self) -> std::io::Result<Option<Command>> {
pub fn read_command(&mut self) -> std::io::Result<Option<ImageCommand>> {
if 0 == self.inner.read_line(&mut self.line)? {
Ok(None)
} else if self.line.ends_with('\n') {
Expand Down
24 changes: 12 additions & 12 deletions pati/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{log::Log, Color, Command, PatchCommand, PatchEntry, Point, Version};
use crate::{log::Log, Color, ImageCommand, PatchEntry, PatchImageCommand, Point, Version};
use std::{
cmp::Ordering,
collections::BTreeMap,
ops::{Bound, RangeBounds},
};

/// [`Image`] with a log of applied [`Command`]s.
/// [`Image`] with a log of applied [`ImageCommand`]s.
#[derive(Debug, Default, Clone)]
pub struct VersionedImage {
image: Image,
Expand Down Expand Up @@ -55,7 +55,7 @@ impl VersionedImage {
///
/// Returns `true` if the image is changed, otherwise `false`.
/// If the command is applied, it is appended to the log.
pub fn apply(&mut self, command: &Command) -> bool {
pub fn apply(&mut self, command: &ImageCommand) -> bool {
let applied = self.image.apply(command);
if applied {
self.log
Expand All @@ -65,13 +65,13 @@ impl VersionedImage {
}

/// Gets the applied commands since the given version.
pub fn applied_commands(&self, since: Version) -> &[Command] {
pub fn applied_commands(&self, since: Version) -> &[ImageCommand] {
let i = (since.0 as usize).min(self.log.commands().len());
&self.log.commands()[i..]
}

/// Calculates the diff between the current image and the image at the given version.
pub fn diff(&self, version: Version) -> Option<PatchCommand> {
pub fn diff(&self, version: Version) -> Option<PatchImageCommand> {
let image = self.log.restore_image(version)?;
Some(self.image.diff(&image))
}
Expand Down Expand Up @@ -122,17 +122,17 @@ impl Image {
/// Applies the given command to this image.
///
/// Returns `true` if the image is changed, otherwise `false`.
pub fn apply(&mut self, command: &Command) -> bool {
pub fn apply(&mut self, command: &ImageCommand) -> bool {
match command {
Command::Patch(c) => self.handle_patch_command(c),
Command::Anchor { name, point } => {
ImageCommand::Patch(c) => self.handle_patch_command(c),
ImageCommand::Anchor { name, point } => {
if let Some(point) = *point {
self.anchors.insert(name.clone(), point) != Some(point)
} else {
self.anchors.remove(name).is_some()
}
}
Command::Put { name, value } => {
ImageCommand::Put { name, value } => {
if value.is_null() {
self.metadata.remove(name).is_some()
} else {
Expand All @@ -142,7 +142,7 @@ impl Image {
}
}

fn handle_patch_command(&mut self, command: &PatchCommand) -> bool {
fn handle_patch_command(&mut self, command: &PatchImageCommand) -> bool {
let mut applied = false;
for entry in command.entries() {
for point in &entry.points {
Expand All @@ -156,7 +156,7 @@ impl Image {
applied
}

fn diff(&self, other: &Self) -> PatchCommand {
fn diff(&self, other: &Self) -> PatchImageCommand {
let mut old_pixels = self.pixels.iter().map(|(p, c)| (*p, *c));
let mut new_pixels = other.pixels.iter().map(|(p, c)| (*p, *c));

Expand Down Expand Up @@ -211,7 +211,7 @@ impl Image {
points,
});
}
PatchCommand::new(entries)
PatchImageCommand::new(entries)
}
}

Expand Down
4 changes: 3 additions & 1 deletion pati/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod image;
mod log;
mod pixel;

pub use self::command::{Command, CommandReader, CommandWriter, PatchCommand, PatchEntry};
pub use self::command::{
ImageCommand, ImageCommandReader, ImageCommandWriter, PatchEntry, PatchImageCommand,
};
pub use self::image::{Image, VersionedImage};
pub use self::log::Version;
pub use self::pixel::{Color, Point};
12 changes: 6 additions & 6 deletions pati/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Command;
use crate::Image;
use crate::ImageCommand;
use serde::{Deserialize, Serialize};

/// Number of applied commands.
Expand All @@ -26,7 +26,7 @@ impl std::ops::Sub<u32> for Version {

#[derive(Debug, Clone)]
pub struct Log {
commands: Vec<Command>,
commands: Vec<ImageCommand>,
snapshots: Vec<Snapshot>,
}

Expand All @@ -35,7 +35,7 @@ impl Log {
Version(self.commands.len() as u32)
}

pub fn append_applied_command(&mut self, command: Command, image: &Image) {
pub fn append_applied_command(&mut self, command: ImageCommand, image: &Image) {
self.commands.push(command);
if self.commands.len() % 1000 == 0 {
self.snapshots.push(Snapshot {
Expand All @@ -45,7 +45,7 @@ impl Log {
}
}

pub fn commands(&self) -> &[Command] {
pub fn commands(&self) -> &[ImageCommand] {
&self.commands
}

Expand Down Expand Up @@ -85,7 +85,7 @@ struct Snapshot {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Color, Image, PatchCommand, PatchEntry, Point};
use crate::{Color, Image, PatchEntry, PatchImageCommand, Point};

#[test]
fn restore_image_works() {
Expand All @@ -98,7 +98,7 @@ mod tests {
color: Some(color),
points: vec![Point::new(1, 3)],
};
let command = Command::Patch(PatchCommand::new(vec![entry]));
let command = ImageCommand::Patch(PatchImageCommand::new(vec![entry]));
assert!(image.apply(&command));
log.append_applied_command(command, &image);
assert_eq!(log.latest_image_version(), Version(1));
Expand Down
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use pagurus::{failure::OrFail, Game};
use pagurus_tui::{TuiSystem, TuiSystemOptions};
use pati::{CommandReader, CommandWriter, Point, VersionedImage};
use pati::{ImageCommandReader, ImageCommandWriter, Point, VersionedImage};
use std::io::Write;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -68,8 +68,8 @@ impl OpenCommand {
.create(true)
.open(&self.path)
.or_fail()?;
let mut reader = CommandReader::new(BufReader::new(file.try_clone().or_fail()?));
let mut writer = CommandWriter::new(BufWriter::new(file));
let mut reader = ImageCommandReader::new(BufReader::new(file.try_clone().or_fail()?));
let mut writer = ImageCommandWriter::new(BufWriter::new(file));
while let Some(command) = reader.read_command().or_fail()? {
game.model_mut().canvas_mut().apply(&command);
}
Expand Down Expand Up @@ -152,13 +152,13 @@ impl OpenCommand {
struct EmbeddedCanvas {
path: PathBuf,
canvas: VersionedImage,
reader: CommandReader<BufReader<std::fs::File>>,
reader: ImageCommandReader<BufReader<std::fs::File>>,
}

impl EmbeddedCanvas {
fn new(path: &PathBuf) -> orfail::Result<Self> {
let file = std::fs::File::open(path).or_fail()?;
let reader = CommandReader::new(BufReader::new(file));
let reader = ImageCommandReader::new(BufReader::new(file));
let canvas = VersionedImage::default(); // TODO: use Canvas
Ok(Self {
path: path.clone(),
Expand Down Expand Up @@ -346,7 +346,7 @@ impl ExportCommand {

fn load_canvas<P: AsRef<Path>>(path: &P) -> orfail::Result<pati::Image> {
let file = std::fs::File::open(path).or_fail()?;
let mut reader = CommandReader::new(BufReader::new(file));
let mut reader = ImageCommandReader::new(BufReader::new(file));
let mut canvas = pati::Image::new();
while let Some(command) = reader.read_command().or_fail()? {
canvas.apply(&command);
Expand Down Expand Up @@ -383,7 +383,7 @@ impl GetCommand {

fn load_model(&self) -> orfail::Result<Model> {
let file = std::fs::File::open(self.path()).or_fail()?;
let mut reader = CommandReader::new(BufReader::new(file));
let mut reader = ImageCommandReader::new(BufReader::new(file));
let mut model = Model::default();
while let Some(command) = reader.read_command().or_fail()? {
model.canvas_mut().apply(&command);
Expand Down
2 changes: 1 addition & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Game {
self.model
.apply(&Command::BackgroundColor(config.initial.background_color));
for (name, point) in &config.initial.anchors {
let command = pati::Command::anchor(name.clone(), Some(*point));
let command = pati::ImageCommand::anchor(name.clone(), Some(*point));
self.model.canvas_mut().apply(&command);
}
}
Expand Down
Loading

0 comments on commit 002036b

Please sign in to comment.