Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#7 add custom file #59

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
4 changes: 4 additions & 0 deletions judge-control-app/src/custom_file.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
pub mod traits;

pub mod directory;
pub mod file_factory;
pub mod text_file;
56 changes: 56 additions & 0 deletions judge-control-app/src/custom_file/directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{
path::PathBuf,
sync::{Arc, RwLock},
};

use super::traits::{File, FileEntity, FileLink};

pub struct DirectoryEntity {
path: PathBuf,
}

impl FileEntity for DirectoryEntity {}

impl File for DirectoryEntity {
type InitArgs = ();
fn new(path: PathBuf, _args: Self::InitArgs) -> anyhow::Result<Self> {
std::fs::create_dir_all(&path)?;
Ok(Self { path })
}
}

impl Drop for DirectoryEntity {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
unimplemented!("error handling for file deletion failure");
}
}

pub struct DirectoryLink {
path: PathBuf,
entity: Arc<RwLock<DirectoryEntity>>,
}

impl FileLink for DirectoryLink {
fn create_symlink_to(&self, path: PathBuf) -> anyhow::Result<Self> {
Self::new(path, self.entity.clone())
}
}

impl File for DirectoryLink {
type InitArgs = Arc<RwLock<DirectoryEntity>>;
fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result<Self> {
std::os::unix::fs::symlink(&args.read().unwrap().path, &path)?;
Ok(Self {
path,
entity: args.clone(),
})
}
}

impl Drop for DirectoryLink {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
unimplemented!("error handling for file deletion failure");
}
}
27 changes: 27 additions & 0 deletions judge-control-app/src/custom_file/file_factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::path::PathBuf;

pub struct FileFactory {
path: PathBuf,
}

impl super::traits::FileFactory for FileFactory {
fn new(path: std::path::PathBuf) -> anyhow::Result<Self> {
Ok(Self { path })
}
fn create_file<FileType: super::traits::File>(
&self,
uuid: uuid::Uuid,
args: FileType::InitArgs,
) -> anyhow::Result<FileType> {
let path = self.path.join(uuid.to_string());
FileType::new(path, args)
}
fn create_symlink_of<FileType: super::traits::FileLink>(
&self,
uuid: uuid::Uuid,
original: &FileType,
) -> anyhow::Result<FileType> {
let path = self.path.join(uuid.to_string());
original.create_symlink_to(path)
}
}
58 changes: 58 additions & 0 deletions judge-control-app/src/custom_file/text_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::{
io::Write,
path::PathBuf,
sync::{Arc, RwLock},
};

use super::traits::{File, FileEntity, FileLink};

pub struct TextFileEntity {
path: PathBuf,
}

impl FileEntity for TextFileEntity {}

impl File for TextFileEntity {
type InitArgs = String;
fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result<Self> {
let mut file = std::fs::File::create(&path)?;
file.write_all(args.as_bytes())?;
Ok(Self { path })
}
}

impl Drop for TextFileEntity {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
unimplemented!("error handling for file deletion failure");
}
}

pub struct TextFileLink {
path: PathBuf,
entity: Arc<RwLock<TextFileEntity>>,
}

impl FileLink for TextFileLink {
fn create_symlink_to(&self, path: PathBuf) -> anyhow::Result<Self> {
Self::new(path, self.entity.clone())
}
}

impl File for TextFileLink {
type InitArgs = Arc<RwLock<TextFileEntity>>;
fn new(path: PathBuf, args: Self::InitArgs) -> anyhow::Result<Self> {
std::os::unix::fs::symlink(&args.read().unwrap().path, &path)?;
Ok(Self {
path,
entity: args.clone(),
})
}
}

impl Drop for TextFileLink {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
unimplemented!("error handling for file deletion failure");
}
}
11 changes: 8 additions & 3 deletions judge-control-app/src/custom_file/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ use uuid::Uuid;
pub trait File: Sized + Drop {
type InitArgs;
fn new(path: PathBuf, args: Self::InitArgs) -> Result<Self>;
fn create_hardlink_to(&self, path: PathBuf) -> Result<Self>;
}

pub trait FileEntity: File {}

pub trait FileLink: File {
fn create_symlink_to(&self, path: PathBuf) -> Result<Self>;
}

pub trait FileFactory: Sized {
fn new(path: PathBuf) -> Result<Self>;
fn create_file<FileType: File>(&self, uuid: Uuid, args: FileType::InitArgs)
-> Result<FileType>;
fn create_hardlink_of<FileType: File>(
fn create_symlink_of<FileType: FileLink>(
&self,
uuid: Uuid,
original: FileType,
original: &FileType,
) -> Result<FileType>;
}