MCPcopy Create free account
hub / github.com/bendudson/EuraliOS / open

Method open

edit/src/main.rs:49–87  ·  view source on GitHub ↗

Open an existing file. If it doesn't exist return an empty File.

(path: &str)

Source from the content-addressed store, hash-verified

47impl File {
48 /// Open an existing file. If it doesn't exist return an empty File.
49 fn open(path: &str) -> File {
50 if let Ok(mut file) = fs::File::open(path) {
51 let mut buffer = Vec::<u8>::new();
52 file.read_to_end(&mut buffer).expect("Couldn't read file");
53 let len = buffer.len();
54 return File {
55 original: buffer,
56 add: Vec::new(),
57 pieces: vec![Piece::Original{start:0,
58 len: len}],
59 cursor: Cursor {
60 piece: 1,
61 pos: 0},
62
63 path: String::from(path),
64 changed: false,
65
66 display_start: Cursor {
67 piece: 0,
68 pos: 0},
69 line_start: 1};
70 } else {
71 // Doesn't exist (probably)
72 return File {
73 original: Vec::new(),
74 add: Vec::new(),
75 pieces: Vec::new(),
76 cursor: Cursor {
77 piece: 0,
78 pos: 0},
79 path: String::from(path),
80 changed: false,
81
82 display_start: Cursor {
83 piece: 0,
84 pos: 0},
85 line_start: 1};
86 }
87 }
88
89 /// Save contents of File to given path
90 fn save(&self) {

Callers

nothing calls this directly

Calls 3

read_to_endMethod · 0.80
openFunction · 0.50
lenMethod · 0.45

Tested by

no test coverage detected