Open an existing file. If it doesn't exist return an empty File.
(path: &str)
| 47 | impl 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) { |
nothing calls this directly
no test coverage detected