use std::path::PathBuf;
use polars::prelude::DataFrame;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use crate::core::df::tabular;
use crate::{
error::OxenError,
model::{CommitEntry, ContentType, Schema},
opts::DFOpts,
};
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub enum ModType {
Append,
Delete,
Modify,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct NewMod {
pub content_type: ContentType,
pub mod_type: ModType,
pub entry: CommitEntry, pub data: String,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ModEntry {
pub uuid: String,
pub modification_type: ModType, pub content_type: ContentType, pub schema: Option<Schema>,
pub data: String,
pub path: PathBuf,
#[serde(with = "time::serde::rfc3339")]
pub timestamp: OffsetDateTime,
}
impl ModEntry {
pub fn to_df(&self) -> Result<DataFrame, OxenError> {
const ID_COL: &str = "_id";
let schema = self.schema.clone().unwrap();
let mut df =
tabular::parse_data_into_df(&self.data, &schema, self.content_type.to_owned())?;
df = tabular::add_col(df, ID_COL, &self.uuid, "str").unwrap();
let mut columns = schema.fields_names();
columns.insert(0, ID_COL.to_string());
let opts = DFOpts::from_column_names(columns);
df = tabular::transform(df, opts)?;
Ok(df)
}
}