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.
ARK-Builders#19: Print URLs (ARK-Builders#27)
- Loading branch information
1 parent
a00c82b
commit 4717f45
Showing
13 changed files
with
675 additions
and
389 deletions.
There are no files selected for viewing
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,2 +1,3 @@ | ||
target | ||
.ark | ||
.ark | ||
.vscode |
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
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)?) | ||
} |
Oops, something went wrong.