Skip to content

Commit

Permalink
fix: sort by date added
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Jun 18, 2024
1 parent fc9e4cc commit cffba3c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
4 changes: 1 addition & 3 deletions crates/cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ impl Cli {

index.save_to_file(index_path)?;
}
Self::Bookmarks {
url,
} => {
Self::Bookmarks { url } => {
let bookmarks = bookmarks::BookmarksIndex::new()?;

if let Some(url) = url {
Expand Down
26 changes: 21 additions & 5 deletions crates/cli/src/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub struct BookmarkEntry {
}

impl Hash for BookmarkEntry {
fn hash<H>(&self, state: &mut H) where H: Hasher {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
let _ = &self.url.hash(state);
}
}
Expand All @@ -39,7 +42,10 @@ pub struct BookmarksIndex(HashSet<BookmarkEntry>);
impl BookmarksIndex {
/// Opens the BookmarksIndex from the `bookmarks.json` file.
pub fn new() -> Result<Self> {
let path = current_dir()?.join("static").join("db").join(BOOKMARKS_INDEX_FILE);
let path = current_dir()?
.join("static")
.join("db")
.join(BOOKMARKS_INDEX_FILE);
let index = read_to_string(path)?;

Ok(serde_json::from_str(&index)?)
Expand All @@ -53,10 +59,20 @@ impl BookmarksIndex {
}

fn save(&self) -> Result<()> {
let path = current_dir()?.join("static").join("db").join(BOOKMARKS_INDEX_FILE);
let index = serde_json::to_string_pretty(&self)?;
let path = current_dir()?
.join("static")
.join("db")
.join(BOOKMARKS_INDEX_FILE);
let mut items = self.0
.iter()
.cloned()
.collect::<Vec<_>>();

std::fs::write(&path, index)?;
items.sort_by(|a, b| b.created_at.cmp(&a.created_at));

let index = serde_json::to_string_pretty(&items)?;

std::fs::write(path, index)?;

Ok(())
}
Expand Down
52 changes: 26 additions & 26 deletions static/db/bookmarks.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[
{
"title": "Why Turborepo is migrating from Go to Rust – Vercel",
"url": "https://vercel.com/blog/turborepo-migration-go-rust",
"title": "Why I retired from the tech crusades",
"url": "https://world.hey.com/dhh/why-i-retired-from-the-tech-crusades-107a51ea",
"tags": [],
"description": "How we're migrating from Go to Rust for better alignment with our tools and work",
"previewImageUrl": "https://assets.vercel.com/image/upload/contentful/image/e5382hct74si/2SPgq1lG92UICEIIX73ayg/97a66b1df0938e42dd6d74137cc1b7fd/OG_Image_____Template.png",
"createdAt": "2024-06-18T00:03:11.319663Z"
"description": "When Ruby on Rails was launched over twenty years ago, I was a twenty-some young programmer convinced that anyone who gave my stack a try would accept its universal superiority for solving The Web Problem. So I pursued the path of the crusade, attempting to convert the unenlightened masses by the edge of a pointed argument. And for a l...",
"previewImageUrl": "https://world.hey.com/dhh/avatar-fb368b1ee9b185dc2a09b03eabdb61678dd55244",
"createdAt": "2024-06-18T23:26:22.557526Z"
},
{
"title": "DDD Concepts and Patterns - Introduction and Overview | Opus Software AG",
"url": "https://opus.ch/en/ddd-concepts-and-patterns-introduction-and-overview/",
"title": "Master hexagonal architecture in Rust",
"url": "https://www.howtocodeit.com/articles/master-hexagonal-architecture-rust",
"tags": [],
"description": "Welcome to this blog post series about concepts and patterns of domain driven design (DDD). Although the original book about DDD was written almost 15 years",
"previewImageUrl": "https://opus.ch/wp-content/uploads/2018/11/blogddd.png",
"createdAt": "2024-06-18T00:00:29.742081Z"
"description": "Everything you need to write flexible, future-proof Rust applications using hexagonal architecture.",
"previewImageUrl": "https://res.cloudinary.com/dkh7xdo6x/image/upload/v1717697346/hexagonal_architecture_og_f851b96907.jpg",
"createdAt": "2024-06-18T23:26:09.889734Z"
},
{
"title": "Rust: Memory, Ownership and Borrowing",
Expand All @@ -31,22 +31,6 @@
"previewImageUrl": "https://www.freecodecamp.org/news/content/images/2023/01/systemdesign.png",
"createdAt": "2024-06-18T00:05:45.478752Z"
},
{
"title": "Why I retired from the tech crusades",
"url": "https://world.hey.com/dhh/why-i-retired-from-the-tech-crusades-107a51ea",
"tags": [],
"description": "When Ruby on Rails was launched over twenty years ago, I was a twenty-some young programmer convinced that anyone who gave my stack a try would accept its universal superiority for solving The Web Problem. So I pursued the path of the crusade, attempting to convert the unenlightened masses by the edge of a pointed argument. And for a l...",
"previewImageUrl": "https://world.hey.com/dhh/avatar-fb368b1ee9b185dc2a09b03eabdb61678dd55244",
"createdAt": "2024-06-18T23:26:22.557526Z"
},
{
"title": "Master hexagonal architecture in Rust",
"url": "https://www.howtocodeit.com/articles/master-hexagonal-architecture-rust",
"tags": [],
"description": "Everything you need to write flexible, future-proof Rust applications using hexagonal architecture.",
"previewImageUrl": "https://res.cloudinary.com/dkh7xdo6x/image/upload/v1717697346/hexagonal_architecture_og_f851b96907.jpg",
"createdAt": "2024-06-18T23:26:09.889734Z"
},
{
"title": "The technology behind GitHub’s new code search",
"url": "https://github.blog/2023-02-06-the-technology-behind-githubs-new-code-search/",
Expand All @@ -55,12 +39,28 @@
"previewImageUrl": "https://github.blog/wp-content/uploads/2023/02/code-search-header.png?fit=1600%2C736",
"createdAt": "2024-06-18T00:04:45.624411Z"
},
{
"title": "Why Turborepo is migrating from Go to Rust – Vercel",
"url": "https://vercel.com/blog/turborepo-migration-go-rust",
"tags": [],
"description": "How we're migrating from Go to Rust for better alignment with our tools and work",
"previewImageUrl": "https://assets.vercel.com/image/upload/contentful/image/e5382hct74si/2SPgq1lG92UICEIIX73ayg/97a66b1df0938e42dd6d74137cc1b7fd/OG_Image_____Template.png",
"createdAt": "2024-06-18T00:03:11.319663Z"
},
{
"title": "bliki: CQRS",
"url": "https://www.martinfowler.com/bliki/CQRS.html",
"tags": [],
"description": "CQRS (Command Query Responsibility Segregation) is the notion that you can use a different model to update information than the model you use to read information",
"previewImageUrl": "https://martinfowler.com/bliki/images/cqrs/cqrs.png",
"createdAt": "2024-06-18T00:01:44.245098Z"
},
{
"title": "DDD Concepts and Patterns - Introduction and Overview | Opus Software AG",
"url": "https://opus.ch/en/ddd-concepts-and-patterns-introduction-and-overview/",
"tags": [],
"description": "Welcome to this blog post series about concepts and patterns of domain driven design (DDD). Although the original book about DDD was written almost 15 years",
"previewImageUrl": "https://opus.ch/wp-content/uploads/2018/11/blogddd.png",
"createdAt": "2024-06-18T00:00:29.742081Z"
}
]

0 comments on commit cffba3c

Please sign in to comment.