-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathcommit.rs
More file actions
88 lines (81 loc) · 3.52 KB
/
Copy pathcommit.rs
File metadata and controls
88 lines (81 loc) · 3.52 KB
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
88
// SPDX-FileCopyrightText: 2023 Joep Meindertsma <joep@ontola.io>
//
// SPDX-License-Identifier: MIT
use crate::Context;
use atomic_lib::{errors::AtomicResult, Storelike};
/// Apply a Commit using the Set method - create or update a value in a resource
pub fn set(context: &Context) -> AtomicResult<()> {
let subject = argument_to_url(context, "subject")?;
let property = argument_to_string(context, "property")?;
let value = argument_to_string(context, "value")?;
// If the resource is not found, create it
let mut resource = match context.store.get_resource(&subject) {
Ok(r) => r,
Err(_) => atomic_lib::Resource::new(subject),
};
resource.set_propval_shortname(&property, &value, &context.store)?;
resource.save(&context.store)?;
Ok(())
}
/// Apply a Commit using the Set method, where the value is edited in the user's text editor.
#[cfg(feature = "native")]
pub fn edit(context: &Context) -> AtomicResult<()> {
let subject = argument_to_url(context, "subject")?;
let prop = argument_to_string(context, "property")?;
// If the resource is not found, create it
let mut resource = match context.store.get_resource(&subject) {
Ok(r) => r,
Err(_) => atomic_lib::Resource::new(subject),
};
// If the prop is not found, create it
let current_val = match resource.get_shortname(&prop, &context.store) {
Ok(val) => val.to_string(),
Err(_) => "".to_string(),
};
let edited = edit::edit(current_val)?;
// Remove newline - or else I can's save shortnames or numbers using vim;
let trimmed = edited.trim_end_matches('\n');
resource.set_propval_shortname(&prop, trimmed, &context.store)?;
resource.save(&context.store)?;
Ok(())
}
/// Apply a Commit using the Remove method - removes a property from a resource
pub fn remove(context: &Context) -> AtomicResult<()> {
let subject = argument_to_url(context, "subject")?;
let prop = argument_to_string(context, "property")?;
let mut resource = context.store.get_resource(&subject)?;
resource.remove_propval_shortname(&prop, &context.store)?;
resource.save(&context.store)?;
Ok(())
}
/// Apply a Commit using the destroy method - removes a resource
pub fn destroy(context: &Context) -> AtomicResult<()> {
let subject = argument_to_url(context, "subject")?;
let mut resource = context.store.get_resource(&subject)?;
resource.destroy(&context.store)?;
Ok(())
}
/// Parses a single argument as string
fn argument_to_string(context: &Context, argument: &str) -> AtomicResult<String> {
let command_name = context.matches.subcommand_name().unwrap();
let subcommand_matches = context.matches.subcommand_matches(command_name).unwrap();
let user_arg = subcommand_matches
.get_one::<String>(argument)
.ok_or(format!("No argument value for {} found", argument))?;
Ok(user_arg.to_string())
}
/// Parses a single argument (URL or Bookmark), should return a valid URL
fn argument_to_url(context: &Context, argument: &str) -> AtomicResult<String> {
let command_name = context.matches.subcommand_name().unwrap();
let subcommand_matches = context.matches.subcommand_matches(command_name).unwrap();
let user_arg = subcommand_matches
.get_one::<String>(argument)
.ok_or(format!("No argument value for {} found", argument))?;
let id_url: String = context
.mapping
.lock()
.unwrap()
.try_mapping_or_url(&String::from(user_arg))
.ok_or(&*format!("No url found for {}", user_arg))?;
Ok(id_url)
}