Skip to content

Commit

Permalink
feat: support exporting items and list to yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
suchapalaver committed Jan 4, 2024
1 parent 79a0237 commit c261dd0
Show file tree
Hide file tree
Showing 27 changed files with 606 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
!crates
!.env
!items.json
!list.json
!list.json
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ target/
# Local JSON 'items.json' and 'list.json' files
*.json

/sqlite.db
*.yaml

/sqlite.db
64 changes: 42 additions & 22 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ scraper = "0.18.1"
serde = { version = "*", features = ["derive"] }
serde_derive = "*"
serde_json = "*"
serde_yaml = "0.9.30"
thiserror = "1.0.48"
tokio = { version = "1", features = ["full"] }
tracing = "0.1.37"
Expand Down
18 changes: 17 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build help fetch read add list clear
.PHONY: build help fetch read add list export clear

build:
docker build --tag gust --file Dockerfile .
Expand Down Expand Up @@ -32,5 +32,21 @@ add:
list:
docker run --rm -v gust:/app gust read list

export:
if [ ! -f "$(PWD)/items.yaml" ]; then \
echo "Error: items.yaml not found in $(PWD)."; \
exit 1; \
fi
if [ ! -f "$(PWD)/list.yaml" ]; then \
echo "Error: list.yaml not found in $(PWD)."; \
exit 1; \
fi
docker run --rm \
-v gust_data:/app \
-v $(PWD)/items.yaml:/app/items.yaml \
-v $(PWD)/list.yaml:/app/list.yaml \
gust \
export

clear:
docker run --rm -v gust:/app gust update list clear
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ All you need is to [install Docker](https://docs.docker.com/install/).

- [help](./docs/cli.md#help)
- [fetching recipes](./docs/cli.md#fetching-recipes)
- [importing/exporting data](./docs/cli.md#importing-and-exporting-data)

### [database](./docs/database.md)

- [storage options](./docs/database.md#storage-options)
- [json](./docs/database.md#json)
- [sqlite](./docs/database.md#sqlite)
- [postgres](./docs/database.md#postgresql)

### [docker](./docs/docker.md)

- [data volumes](./docker.md#creating-a-gust_data-volume)
- [migrating from JSON to SQLite](./docker.md#migrate-a-json-gust-store-to-sqlite)
- [data volumes](./docs/docker.md#creating-a-gust_data-volume)
- [migrating from JSON to SQLite](./docs/docker.md#migrate-a-json-gust-store-to-sqlite)
- [exporting data to YAML](./docs/docker.md#export-data-to-yaml)

---
## getting started
Expand Down
15 changes: 14 additions & 1 deletion crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub enum ApiResponse {
Checklist(Vec<Item>),
DeletedRecipe(Recipe),
DeletedChecklistItem(Name),
Exported(Vec<Item>, List),
FetchedRecipe((Recipe, Ingredients)),
ItemAlreadyAdded(Name),
Items(Items),
Expand Down Expand Up @@ -150,6 +151,17 @@ impl Display for ApiResponse {
}
Self::DeletedChecklistItem(name) => writeln!(f, "\ndeleted from checklist: \n{name}"),
Self::DeletedRecipe(recipe) => writeln!(f, "\ndeleted recipe: \n{recipe}"),
Self::Exported(items, list) => {
writeln!(f, "\nexported items:")?;
for item in items {
writeln!(f, " {item}")?;
}
writeln!(f, "\nexported list:")?;
for item in list.items() {
writeln!(f, " {item}")?;
}
Ok(())
}
Self::FetchedRecipe((recipe, ingredients)) => {
writeln!(f, "\n{recipe}:")?;
for ingredient in ingredients.iter() {
Expand All @@ -165,7 +177,7 @@ impl Display for ApiResponse {
}
Ok(())
}
Self::ImportToSqlite => writeln!(f, "\nJSON to SQLite data store migration successful"),
Self::ImportToSqlite => writeln!(f, "\nImport successful"),
Self::List(list) => {
writeln!(f)?;
for item in list.items() {
Expand Down Expand Up @@ -214,6 +226,7 @@ impl From<StoreResponse> for ApiResponse {
StoreResponse::Checklist(item) => Self::Checklist(item),
StoreResponse::DeletedRecipe(item) => Self::DeletedRecipe(item),
StoreResponse::DeletedChecklistItem(item) => Self::DeletedChecklistItem(item),
StoreResponse::Exported(items, list) => Self::Exported(items, list),
StoreResponse::FetchedRecipe(item) => Self::FetchedRecipe(item),
StoreResponse::ItemAlreadyAdded(item) => Self::ItemAlreadyAdded(item),
StoreResponse::Items(item) => Self::Items(item),
Expand Down
1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ scraper = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/common/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
pub enum ApiCommand {
Add(Add),
Delete(Delete),
Export,
FetchRecipe(Url),
ImportFromJson,
Read(Read),
Expand Down
34 changes: 34 additions & 0 deletions crates/common/src/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{fs::File, path::Path};

use serde::Serialize;
use thiserror::Error;

use crate::{item::Item, list::List};

pub const ITEMS_YAML_PATH: &str = "items.yaml";
pub const LIST_YAML_PATH: &str = "list.yaml";

#[derive(Error, Debug)]
pub enum ExportError {
#[error("file error: {0}")]
FileError(#[from] std::io::Error),

#[error("'serde-yaml' error: {0}")]
SerdeYamlError(#[from] serde_yaml::Error),
}

pub trait YamlSerializable {
fn serialize_to_yaml_and_write<P>(&self, path: P) -> Result<(), ExportError>
where
P: AsRef<Path>,
Self: Serialize,
{
let file = File::create(path)?;
serde_yaml::to_writer(file, self)?;

Ok(())
}
}

impl YamlSerializable for Vec<Item> {}
impl YamlSerializable for List {}
6 changes: 5 additions & 1 deletion crates/common/src/items.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::{item::Item, recipes::Recipe, section::Section, Load};
use crate::{item::Item, load::Load, recipes::Recipe, section::Section};

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Items {
Expand Down Expand Up @@ -29,6 +29,10 @@ impl Items {
Self::default()
}

pub fn collection(&self) -> &[Item] {
&self.collection
}

pub fn collection_iter(&self) -> impl Iterator<Item = &Item> {
self.collection.iter()
}
Expand Down
Loading

0 comments on commit c261dd0

Please sign in to comment.