File: load_json.rs

package info (click to toggle)
rust-scm-record 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 576 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download | duplicates (2)
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
#![warn(clippy::all, clippy::as_conversions)]
#![allow(clippy::too_many_arguments)]

use std::path::Path;

use scm_record::{
    helpers::CrosstermInput, FileMode, RecordError, RecordState, Recorder, SelectedChanges,
    SelectedContents,
};

#[cfg(feature = "serde")]
fn load_state(path: impl AsRef<Path>) -> RecordState<'static> {
    let json_file = std::fs::File::open(path).expect("opening JSON file");
    serde_json::from_reader(json_file).expect("deserializing state")
}

#[cfg(not(feature = "serde"))]
fn load_state(_path: impl AsRef<Path>) -> RecordState<'static> {
    panic!("load_json example requires `serde` feature")
}

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let json_filename = args.get(1).expect("expected JSON dump as first argument");
    let record_state: RecordState = load_state(json_filename);

    let mut input = CrosstermInput;
    let recorder = Recorder::new(record_state, &mut input);
    let result = recorder.run();
    match result {
        Ok(result) => {
            let RecordState {
                is_read_only: _,
                commits: _,
                files,
            } = result;
            for file in files {
                println!("--- Path {:?} final lines: ---", file.path);
                let (selected, _unselected) = file.get_selected_contents();

                let SelectedChanges {
                    contents,
                    file_mode,
                } = selected;

                if file_mode == FileMode::Absent {
                    println!("<absent>");
                } else {
                    print!(
                        "{}",
                        match contents {
                            SelectedContents::Unchanged => "<unchanged\n>".to_string(),
                            SelectedContents::Binary {
                                old_description: _,
                                new_description: None,
                            } => "<binary>\n".to_string(),
                            SelectedContents::Binary {
                                old_description: _,
                                new_description: Some(description),
                            } => format!("<binary description={description}>\n"),
                            SelectedContents::Text { contents } => contents.clone(),
                        }
                    );
                }
            }
        }
        Err(RecordError::Cancelled) => println!("Cancelled!\n"),
        Err(err) => {
            println!("Error: {err}");
        }
    }
}