-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rs
87 lines (80 loc) · 2.16 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use chrono::NaiveDate;
use serde::{Serialize, Deserialize};
use rgo_early_warning_system::data_model::{Farm, Status};
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
#[tauri::command]
fn farms() -> Vec<Farm> {
let mut farms = Vec::new();
farms.push(Farm {
id: 0,
name: "Joe's Farm".to_string(),
// measurements: Vec::new(),
timestamp: 20,
status: Status::Red,
});
farms.push(Farm {
id: 1,
name: "Sally's Farm".to_string(),
// measurements: Vec::new(),
timestamp: 5,
status: Status::Yellow,
});
farms.push(Farm {
id: 2,
name: "Joe's Other Farm".to_string(),
// measurements: Vec::new(),
timestamp: 10,
status: Status::Yellow,
});
farms.push(Farm {
id: 3,
name: "Sam's Farm".to_string(),
// measurements: Vec::new(),
timestamp: 1,
status: Status::Green,
});
farms
}
#[derive(Debug, Clone)]
enum LoadError {
File,
Format,
}
#[derive(Debug, Clone)]
enum SaveError {
File,
Write,
Format,
}
#[derive(Clone, Serialize, Deserialize)]
struct SavedState {
farms: Vec<Farm>
}
fn path() -> std::path::PathBuf {
let mut path = std::env::current_dir().unwrap_or_default();
path.push("rgo_early_warning.json");
path
}
#[tauri::command]
fn save_sync(data: SavedState) {
let f = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(path())
.expect("Couldn't open file");
serde_json::to_writer_pretty(f, &data).unwrap();
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![farms])
.invoke_handler(tauri::generate_handler![save_sync])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}