Skip to content

Instantly share code, notes, and snippets.

@MrDwarf7
Created April 23, 2024 11:12
Show Gist options
  • Save MrDwarf7/b8d0eff14c1b9a55ee32f140357ee93c to your computer and use it in GitHub Desktop.
Save MrDwarf7/b8d0eff14c1b9a55ee32f140357ee93c to your computer and use it in GitHub Desktop.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ConfigSqlFiles {
files: Option<Vec<Option<PathBuf>>>,
}
impl ConfigSqlFiles {
pub fn get_files(&self) -> &Vec<Option<PathBuf>> {
self.files.as_ref().unwrap()
}
}
impl Deref for ConfigSqlFiles {
type Target = Vec<Option<PathBuf>>;
fn deref(&self) -> &Self::Target {
self.files.as_ref().unwrap()
}
}
impl DerefMut for ConfigSqlFiles {
fn deref_mut(&mut self) -> &mut Self::Target {
self.files.as_mut().unwrap()
}
}
impl PartialEq for ConfigSqlFiles {
fn eq(
&self,
other: &Self,
) -> bool {
let collected_self = self.files.as_ref().unwrap().iter().collect::<Vec<_>>();
let collected_other = other.files.as_ref().unwrap().iter().collect::<Vec<_>>();
self.clone().files.zip(other.clone().files).map(|(x, y)| x == y).unwrap_or(false)
}
}
impl Eq for ConfigSqlFiles {}
impl ConfigSqlFiles {
pub fn insert_files(&mut self) -> Option<Self> {
let files = discover_files(&get_current_dir()?.join(DEFAULT_DATA_DIRECTORY))?;
dbg!(&files);
// Compare the files in the dir to the files in the current struct instance.
// only grabbing the files that are sql files
// BUG: Logic error on checking value??
self.files.is_none().then(|| {
match files.first() {
Some(_) => {
let sql_files = files
.iter()
.filter(|file| file.extension() == Some(OsStr::new("sql")))
.map(|file| Some(file.to_path_buf()))
.collect();
self.files = Some(sql_files);
}
None => self.files = Some(vec![Some(PathBuf::new())]),
}
});
let res: Vec<_> = self
.to_owned()
//
.files
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>()
.into_iter()
.zip(
files
.iter()
.filter(|file| {
{
let a = file.extension().map_or(false, |ext| ext == OsStr::new("sql")).to_owned();
dbg!(&a);
a
}
//
})
// IMP: remove this flatten to see the entire structure generated ;)
.flatten()
.zip(
self.to_owned()
//
.files
.unwrap()
.into_iter()
.filter(|file| {
{
let b = file
.to_owned()
.unwrap()
.extension()
.map_or(false, |ext| ext == OsStr::new("sql"));
dbg!(&b);
b
}
})
.collect::<Vec<_>>()
.into_iter()
.flat_map(|x| {
x.into_iter().map(|x| {
dbg!(&x);
files
.iter()
.filter(|file| {
//
dbg!(&file);
file.extension().map_or(false, |ext| ext == OsStr::new("sql"))
})
.any(|file| {
//
dbg!(&file);
dbg!(&x);
file == &x
});
dbg!(&x);
})
}),
),
)
.collect();
dbg!(res);
// .iter()
// .for_each(|x| {
// //
// });
// let mut difference: Vec<_> = files
// .iter()
// .filter(|file| {
// //
// match self.files.as_ref().unwrap().into_iter().find(|&&x| *x.as_ref().unwrap() == **file.to_owned()) {
// Some(_) => false,
// None => true,
// }
// })
// .collect();
// dbg!(&difference);
// difference.iter_mut().for_each(|file| {
// let matching = self.files.iter().zip(difference.iter()).find(|(&x, &y)| &x == &y);
// dbg!(&matching);
// match matching {
// Some(_) => {
// self.files.as_mut().unwrap().push(Some(file.to_path_buf()));
// }
// None => {
// self.files.as_mut().unwrap().push(Some(file.to_path_buf()));
// }
// }
// self.files.as_mut().unwrap().push(Some(file.to_path_buf()));
todo!();
Some(self.clone())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment