Skip to content

Commit

Permalink
fix: csv writer consider special chars (#16)
Browse files Browse the repository at this point in the history
* Close #14, csv writer consider special chars

* fix CI
  • Loading branch information
jiacai2050 committed Apr 18, 2023
1 parent 70fb95d commit 33ff382
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUSTTARGET: ${{ matrix.target }}
EXTRA_FILES: "README.org"

- name: Publish on crates.io
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "onehistory"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Jiacai Liu <jiacai2050+1history@gmail.com>"]
authors = ["Jiacai Liu <dev@liujiacai.net>"]
description = "All your history in one file"
homepage = "https://github.com/1History/1History"
repository = "https://github.com/1History/1History"
Expand All @@ -28,4 +28,5 @@ minijinja = { version = "0.12.0", features = ["builtins", "urlencode"]}
glob = "0.3.0"
chrono = "0.4"
regex = "1"
indicatif = "0.16.2"
indicatif = "0.16.2"
csv = "1.2.1"
4 changes: 2 additions & 2 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct Database {

impl Database {
pub fn open(sqlite_datafile: String) -> Result<Database> {
let conn = Connection::open(&sqlite_datafile)?;
let conn = Connection::open(sqlite_datafile)?;
let db = Self {
conn: Mutex::new(conn),
persist_batch: DEFAULT_BATCH_NUM,
Expand Down Expand Up @@ -122,7 +122,7 @@ INSERT INTO onehistory_visits (item_id, visit_time, visit_type)
visit_type,
} in batch
{
match tx.execute(sql, &[&item_id, &visit_time, &visit_type]) {
match tx.execute(sql, [&item_id, &visit_time, &visit_type]) {
Ok(ret) => affected += ret,
Err(e) => {
if let sqlError::SqliteFailure(ffi_err, _msg) = &e {
Expand Down
25 changes: 9 additions & 16 deletions src/export.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use anyhow::{Context, Result};
use log::{debug, info};
use std::{
fs::OpenOptions,
io::{BufWriter, Write},
};
use std::{fs::OpenOptions, io::BufWriter};

use crate::{
database::Database,
Expand All @@ -21,22 +18,18 @@ pub fn export_csv(csv_file: String, db_file: String) -> Result<()> {
.truncate(true)
.open(&csv_file)
.context(csv_file.clone())?;
let mut buf_writer = BufWriter::new(f);
let mut csv_writer = csv::Writer::from_writer(BufWriter::new(f));

buf_writer.write_all(b"time,title,url,visit_type\n")?;
csv_writer.write_record(["time", "title", "url", "visit_type"])?;
let visits = db.select_visits(start, end, None)?;
let len = visits.len();
for visit in visits {
buf_writer.write_all(
format!(
"{},{},{},{}\n",
unixepoch_as_ymdhms(visit.visit_time),
visit.title.replace(',', ""),
visit.url,
visit.visit_type
)
.as_bytes(),
)?;
csv_writer.write_record(vec![
unixepoch_as_ymdhms(visit.visit_time),
visit.title,
visit.url,
visit.visit_type.to_string(),
])?;
}
info!("Export {len} histories in {csv_file}.");

Expand Down
2 changes: 1 addition & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Source {
impl Source {
pub fn open(path: &str) -> Result<Source> {
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE;
let conn = Connection::open_with_flags(&path, flags).context(path.to_string())?;
let conn = Connection::open_with_flags(path, flags).context(path.to_string())?;
let name = Self::detect_name(&conn).context(format!("detect {path}"))?;
Ok(Source {
path: path.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Server {
ymd_ts => start,
visit_details => visit_details,
version => clap::crate_version!(),
keyword => keyword.unwrap_or_else(|| "".to_string()),
keyword => keyword.unwrap_or_default(),
))
.map_err(|e| ServerError::from(Error::from(e)))?;

Expand Down Expand Up @@ -145,7 +145,7 @@ impl Server {
daily_counts => daily_counts,
title_top100 => title_top100,
domain_top100 => domain_top100,
keyword => keyword.unwrap_or_else(|| "".to_string()),
keyword => keyword.unwrap_or_default(),
version => clap::crate_version!(),
))
.map_err(|e| ServerError::from(Error::from(e)))?;
Expand Down

0 comments on commit 33ff382

Please sign in to comment.