Skip to content

Commit

Permalink
Code improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
KmolYuan committed Oct 6, 2023
1 parent 6ec740e commit 224a612
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
23 changes: 11 additions & 12 deletions four-bar-ui/src/app/proj/impl_proj.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use four_bar::{csv, efd, fb, NormFourBar, SNormFourBar};
use std::path::Path;

macro_rules! hotkey {
($ui:ident, $mod1:ident + $key:ident) => {
Expand Down Expand Up @@ -88,7 +89,7 @@ impl Project {
fn name(self: &Self) -> String;
fn preload(self: &mut Self);
fn set_path(self: &mut Self, path: PathBuf);
fn path(self: &Self) -> Option<&PathBuf>;
fn path(self: &Self) -> Option<&Path>;
fn is_unsaved(self: &Self) -> bool;
fn mark_saved(self: &mut Self);
}
Expand Down Expand Up @@ -286,7 +287,7 @@ where
io::save_csv_ask(&get_curve(*pivot, &self.fb, cfg.res));
}
if small_btn(ui, "🗐", "Copy") {
let t = csv::csv_string(get_curve(*pivot, &self.fb, cfg.res)).unwrap();
let t = csv::to_string(get_curve(*pivot, &self.fb, cfg.res)).unwrap();
ui.output_mut(|s| s.copied_text = t);
}
});
Expand Down Expand Up @@ -358,24 +359,22 @@ where
if cfg!(target_arch = "wasm32") {
return;
}
if let Some(fb) = self
.path
.as_ref()
.and_then(|p| std::fs::File::open(p).ok())
.and_then(|r| ron::de::from_reader(r).ok())
{
if self.fb != fb {
// FIXME: Try block, ignore errors
(|| {
let r = std::fs::File::open(self.path.as_ref()?).ok()?;
if self.fb != ron::de::from_reader(r).ok()? {
self.unsaved = true;
}
}
Some(())
})();
}

fn set_path(&mut self, path: PathBuf) {
self.path.replace(path);
}

fn path(&self) -> Option<&PathBuf> {
self.path.as_ref()
fn path(&self) -> Option<&Path> {
self.path.as_deref()
}

fn is_unsaved(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions four-bar-ui/src/app/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl Synthesis {
ui.horizontal_wrapped(|ui| {
if ui.button("🗐 Copy CSV").clicked() {
let text = match &self.target {
io::Curve::P(t) => csv::csv_string(t).unwrap(),
io::Curve::S(t) => csv::csv_string(t).unwrap(),
io::Curve::P(t) => csv::to_string(t).unwrap(),
io::Curve::S(t) => csv::to_string(t).unwrap(),
};
ui.output_mut(|s| s.copied_text = text);
}
Expand Down
8 changes: 4 additions & 4 deletions four-bar-ui/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@ where

pub(crate) fn save_csv_ask<S>(c: &[S])
where
S: serde::Serialize + Clone,
S: serde::Serialize,
{
save_ask(
"curve.csv",
CSV_FMT,
CSV_EXT,
|w| csv::dump_csv(w, c),
|w| csv::to_writer(w, c),
|_| (),
);
}
Expand Down Expand Up @@ -372,11 +372,11 @@ impl Curve {
where
R: std::io::Read + std::io::Seek,
{
if let Ok(c) = csv::parse_csv(&mut r) {
if let Ok(c) = csv::from_reader(&mut r) {
Ok(Self::S(c))
} else {
r.rewind()?;
Ok(Self::P(csv::parse_csv(r)?))
Ok(Self::P(csv::from_reader(r)?))
}
}

Expand Down
12 changes: 6 additions & 6 deletions four-bar/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use csv::{ReaderBuilder, Writer};
use serde::{de::DeserializeOwned, Serialize};

/// Parse CSV from string.
pub fn parse_csv<D, R>(r: R) -> Result<Vec<D>, Error>
pub fn from_reader<R, D>(r: R) -> Result<Vec<D>, Error>
where
R: std::io::Read,
D: DeserializeOwned,
Expand All @@ -23,11 +23,11 @@ where
}

/// Dump CSV to a writer.
pub fn dump_csv<'a, W, C, S>(w: W, c: C) -> Result<(), csv::Error>
pub fn to_writer<W, C, S>(w: W, c: C) -> Result<(), csv::Error>
where
W: std::io::Write,
C: AsRef<[S]>,
S: Serialize + Clone + 'a,
S: Serialize,
{
let mut w = Writer::from_writer(w);
c.as_ref().iter().try_for_each(|c| w.serialize(c))?;
Expand All @@ -36,12 +36,12 @@ where
}

/// Dump CSV to string.
pub fn csv_string<'a, C, S>(c: C) -> Result<String, csv::Error>
pub fn to_string<C, S>(c: C) -> Result<String, csv::Error>
where
C: AsRef<[S]>,
S: Serialize + Clone + 'a,
S: Serialize,
{
let mut w = Vec::new();
dump_csv(&mut w, c)?;
to_writer(&mut w, c)?;
Ok(String::from_utf8(w).unwrap())
}
5 changes: 1 addition & 4 deletions four-bar/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,7 @@ impl<'a, 'b, M: Clone, C: Clone> FigureBase<'a, 'b, M, C> {
#[inline]
fn get_family(&self) -> &str {
const DEFAULT_FONT: &str = "Times New Roman";
self.font_family
.as_ref()
.map(|s| s.as_ref())
.unwrap_or(DEFAULT_FONT)
self.font_family.as_deref().unwrap_or(DEFAULT_FONT)
}

pub(crate) fn get_font(&self) -> TextStyle {
Expand Down

0 comments on commit 224a612

Please sign in to comment.