forked from ARK-Builders/ark-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8133b6d
commit 95f7fad
Showing
11 changed files
with
655 additions
and
373 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,54 @@ | ||
use std::path::PathBuf; | ||
|
||
use arklib::{id::ResourceId, link::Link}; | ||
use std::path::PathBuf; | ||
use url::Url; | ||
|
||
use crate::util::provide_index; | ||
use crate::error::AppError; | ||
use crate::util::provide_index; // Import your custom AppError type | ||
|
||
pub async fn create_link( | ||
root: &PathBuf, | ||
url: &str, | ||
title: &str, | ||
desc: Option<String>, | ||
) -> Result<(), String> { | ||
let url = Url::parse(url).map_err(|_| "Invalid url")?; | ||
) -> Result<(), AppError> { | ||
let url = Url::parse(url) | ||
.map_err(|_| AppError::LinkCreationError("Invalid url".to_owned()))?; | ||
let link: Link = Link::new(url, title.to_owned(), desc.to_owned()); | ||
link.save(&root, true) | ||
link.save(root, true) | ||
.await | ||
.map_err(|e| e.to_string()) | ||
.map_err(|e| AppError::LinkCreationError(e.to_string())) | ||
} | ||
|
||
pub fn load_link( | ||
root: &PathBuf, | ||
file_path: &Option<PathBuf>, | ||
id: &Option<ResourceId>, | ||
) -> Result<Link, String> { | ||
) -> Result<Link, AppError> { | ||
let path_from_index = id.map(|id| { | ||
let index = provide_index(&root); | ||
let index = provide_index(root); | ||
index.id2path[&id].as_path().to_path_buf() | ||
}); | ||
let path_from_user = file_path; | ||
|
||
let path = match (path_from_user, path_from_index) { | ||
(Some(path), Some(path2)) => { | ||
if path.canonicalize().unwrap() != path2 { | ||
Err(format!( | ||
if path.canonicalize()? != path2 { | ||
Err(AppError::LinkLoadError(format!( | ||
"Path {:?} was requested. But id {} maps to path {:?}", | ||
path, | ||
id.unwrap(), | ||
path2, | ||
)) | ||
))) | ||
} else { | ||
Ok(path.to_path_buf()) | ||
} | ||
} | ||
(Some(path), None) => Ok(path.to_path_buf()), | ||
(None, Some(path)) => Ok(path), | ||
(None, None) => Err("Provide a path or id for request.".to_owned())?, | ||
(None, None) => Err(AppError::LinkLoadError( | ||
"Provide a path or id for request.".to_owned(), | ||
))?, | ||
}?; | ||
|
||
arklib::link::Link::load(root, &path).map_err(|e| e.to_string()) | ||
Ok(arklib::link::Link::load(root, &path)?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use arklib::ArklibError; | ||
use std::io; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum InlineJsonParseError { | ||
#[error("Invalid JSON: entries must be key-value pairs seperated by ':'")] | ||
InvalidKeyValPair, | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum AppError { | ||
#[error("Couldn't retrieve home directory!")] | ||
HomeDirNotFound, | ||
|
||
#[error("Couldn't create .ark directory: {0}")] | ||
ArkDirectoryCreationError(String), | ||
|
||
#[error("Couldn't load app id: {0}")] | ||
AppIdLoadError(String), | ||
|
||
#[error("Could not provide/read index: {0}")] | ||
IndexError(String), | ||
|
||
#[error("Could not create storage: {0}")] | ||
StorageCreationError(String), | ||
|
||
#[error("Failed to create link: {0}")] | ||
LinkCreationError(String), | ||
|
||
#[error("Could not load link: {0}")] | ||
LinkLoadError(String), | ||
|
||
#[error("File operation error: {0}")] | ||
FileOperationError(String), | ||
|
||
#[error("Failed to create backup: {0}")] | ||
BackupCreationError(String), | ||
|
||
#[error("Unknown render option")] | ||
InvalidRenderOption, | ||
|
||
#[error("Storage not found: {0}")] | ||
StorageNotFound(String), | ||
|
||
#[error("Invalid entry option")] | ||
InvalidEntryOption, | ||
|
||
#[error(transparent)] | ||
IoError(#[from] io::Error), | ||
|
||
#[error(transparent)] | ||
ArklibError(#[from] ArklibError), | ||
|
||
#[error(transparent)] | ||
InlineJsonParseError(#[from] InlineJsonParseError), | ||
} |
Oops, something went wrong.