Skip to content

Commit 6cdf924

Browse files
committed
chore: update timestamp type option
1 parent 59c5f99 commit 6cdf924

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

collab-database/src/fields/type_option/timestamp_type_option.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::fields::date_type_option::{DateFormat, TimeFormat};
33
use crate::fields::{
44
TypeOptionCellReader, TypeOptionCellWriter, TypeOptionData, TypeOptionDataBuilder,
55
};
6-
use crate::rows::{new_cell_builder, Cell};
7-
use crate::template::entity::CELL_DATA;
6+
use crate::rows::Cell;
7+
use crate::template::timestamp_parse::TimestampCellData;
88
use chrono::{DateTime, Local, Offset, TimeZone};
99
use chrono_tz::Tz;
1010
use collab::util::AnyMapExt;
@@ -26,17 +26,19 @@ pub struct TimestampTypeOption {
2626
impl TypeOptionCellReader for TimestampTypeOption {
2727
/// Return formated date and time string for the cell
2828
fn json_cell(&self, cell: &Cell) -> Value {
29-
let s = self.stringify_cell(cell);
30-
json!(s)
29+
json!(self.stringify_cell(cell))
3130
}
3231

3332
fn numeric_cell(&self, _cell: &Cell) -> Option<f64> {
3433
None
3534
}
3635

3736
fn convert_raw_cell_data(&self, text: &str) -> String {
38-
let (date_string, time_string) =
39-
self.formatted_date_time_from_timestamp(&text.parse::<i64>().ok());
37+
let cell_data = TimestampCellData {
38+
timestamp: text.parse::<i64>().ok(),
39+
};
40+
41+
let (date_string, time_string) = self.formatted_date_time_from_timestamp(&cell_data.timestamp);
4042
if self.include_time {
4143
format!("{} {}", date_string, time_string)
4244
} else {
@@ -47,15 +49,13 @@ impl TypeOptionCellReader for TimestampTypeOption {
4749

4850
impl TypeOptionCellWriter for TimestampTypeOption {
4951
fn write_json(&self, json_value: Value) -> Cell {
50-
let mut cell = new_cell_builder(FieldType::Time);
51-
if let Some(data) = match json_value {
52+
let filed_type = FieldType::from(self.field_type);
53+
let data = match json_value {
5254
Value::String(s) => s.parse::<i64>().ok(),
5355
Value::Number(n) => n.as_i64(),
5456
_ => None,
55-
} {
56-
cell.insert(CELL_DATA.into(), data.into());
57-
}
58-
cell
57+
};
58+
TimestampCellData::new(data).to_cell(filed_type)
5959
}
6060
}
6161

@@ -150,6 +150,7 @@ impl From<TimestampTypeOption> for TypeOptionData {
150150
#[cfg(test)]
151151
mod tests {
152152
use super::*;
153+
use crate::template::entity::CELL_DATA;
153154
use serde_json::json;
154155
use std::i64;
155156

@@ -253,25 +254,14 @@ mod tests {
253254
}
254255

255256
#[test]
256-
fn test_write_json_valid_number() {
257-
let option = TimestampTypeOption::default();
258-
let json_value = json!(1672531200);
259-
260-
let cell = option.write_json(json_value);
261-
let data: i64 = cell.get_as(CELL_DATA).unwrap();
262-
263-
assert_eq!(data, 1672531200);
264-
}
265-
266-
#[test]
267-
fn test_write_json_valid_string() {
257+
fn test_write_json_string() {
268258
let option = TimestampTypeOption::default();
269259
let json_value = json!("1672531200");
270260

271261
let cell = option.write_json(json_value);
272-
let data: i64 = cell.get_as(CELL_DATA).unwrap();
262+
let data = cell.get_as::<String>(CELL_DATA).unwrap();
273263

274-
assert_eq!(data, 1672531200);
264+
assert_eq!(data, "1672531200");
275265
}
276266

277267
#[test]

collab-database/src/template/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ pub mod option_parse;
88
pub mod relation_parse;
99
pub mod summary_parse;
1010
pub mod time_parse;
11+
pub mod timestamp_parse;
1112
pub mod translate_parse;
1213
pub mod util;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::entity::FieldType;
2+
use crate::rows::{new_cell_builder, Cell};
3+
use crate::template::entity::CELL_DATA;
4+
use collab::util::AnyMapExt;
5+
use serde::{Deserialize, Serialize};
6+
7+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
8+
pub struct TimestampCellData {
9+
pub timestamp: Option<i64>,
10+
}
11+
12+
impl TimestampCellData {
13+
pub fn new(timestamp: Option<i64>) -> Self {
14+
Self { timestamp }
15+
}
16+
17+
pub fn to_cell(&self, field_type: FieldType) -> Cell {
18+
let data: TimestampCellDataWrapper = (field_type, self.clone()).into();
19+
data.into()
20+
}
21+
}
22+
23+
impl ToString for TimestampCellData {
24+
fn to_string(&self) -> String {
25+
serde_json::to_string(self).unwrap()
26+
}
27+
}
28+
29+
impl From<&Cell> for TimestampCellData {
30+
fn from(cell: &Cell) -> Self {
31+
let timestamp = cell
32+
.get_as::<String>(CELL_DATA)
33+
.and_then(|data| data.parse::<i64>().ok());
34+
Self { timestamp }
35+
}
36+
}
37+
38+
/// Wrapper for DateCellData that also contains the field type.
39+
/// Handy struct to use when you need to convert a DateCellData to a Cell.
40+
struct TimestampCellDataWrapper {
41+
data: TimestampCellData,
42+
field_type: FieldType,
43+
}
44+
45+
impl From<(FieldType, TimestampCellData)> for TimestampCellDataWrapper {
46+
fn from((field_type, data): (FieldType, TimestampCellData)) -> Self {
47+
Self { data, field_type }
48+
}
49+
}
50+
51+
impl From<TimestampCellDataWrapper> for Cell {
52+
fn from(wrapper: TimestampCellDataWrapper) -> Self {
53+
let (field_type, data) = (wrapper.field_type, wrapper.data);
54+
let timestamp_string = data.timestamp.unwrap_or_default().to_string();
55+
56+
let mut cell = new_cell_builder(field_type);
57+
cell.insert(CELL_DATA.into(), timestamp_string.into());
58+
cell
59+
}
60+
}

0 commit comments

Comments
 (0)