Skip to content

Commit

Permalink
checked in file
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbuffalo committed Oct 2, 2023
1 parent 09dcbef commit b23a8c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/lib/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use url::Url;

#[derive(Debug)]
pub struct GitHubRepo {
username: String,
repository: String,
}

impl GitHubRepo {
/// Create a new GitHubRepo from a URL string
pub fn new(url_str: &str) -> Result<Self, String> {
let parsed_url = Url::parse(url_str).map_err(|e| e.to_string())?;
let path_segments: Vec<&str> = parsed_url.path_segments().ok_or("Invalid path".to_string())?.collect();

if path_segments.len() < 2 {
return Err("URL should contain both username and repository".to_string());
}

Ok(Self {
username: path_segments[0].to_string(),
repository: path_segments[1].to_string(),
})
}

/// Create the URL to download a file from the GitHub repository.
pub fn url(&self, file_path: &str) -> String {
format!(
"https://github.com/{}/{}/raw/main/{}",
self.username, self.repository, file_path
)
}
}

0 comments on commit b23a8c9

Please sign in to comment.