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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#![cfg_attr(not(test), no_std)]
#![feature(error_in_core)]
#![feature(const_trait_impl)]

#[macro_use]
extern crate sys;
extern crate alloc;

use core::ffi::c_char;
use core::ffi::c_int;
use core::ffi::c_uint;
use core::ffi::c_void;
use alloc::string::String;
use alloc::vec::Vec;

use error::Error;
use file::AnyFile;
use options::FileOptionsExt;
use options::OpenOptions;
use seek::Whence;
pub use sys::ffi::FileStat;
pub use sys::ffi::FileOptions;
use sys::ffi::CString;
use sys::ffi::CStr;

use file::File;
use error::ApiError;


pub mod api;
pub mod file;
pub mod seek;
pub mod options;
pub mod error;


pub type Path = str;


/// Read the entire contents of a file into a bytes vector.
/// > Works similarly to [`std::fs::read`].
pub fn read<P: AsRef<Path>>(path: P, data_dir: bool) -> Result<Vec<u8>, ApiError> {
	let fs = Fs::Cached();
	let opts = FileOptions::new().read(true).read_data(data_dir);
	let mut file = fs.open_with(api::Default, &path, opts)?;

	// determine size of file:
	let size = fs.metadata(path).map(|m| m.size).ok().unwrap_or(0);

	// prepare prefilled buffer:
	let mut buf = alloc::vec![0; size as usize];

	fs.read(&mut file, &mut buf, size)?;
	Ok(buf)
}


/// Read the entire contents of a file into a string.
/// > Works similarly to [`std::fs::read_to_string`].
pub fn read_to_string<P: AsRef<Path>>(path: P, data_dir: bool) -> Result<String, ApiError> {
	let buf = read(path, data_dir)?;
	alloc::string::String::from_utf8(buf).map_err(Into::into)
}


/// Write a bytes of the entire `contents` of a file.
///
/// This function will create a file if it does not exist,
/// and will entirely replace its contents if it does.
///
/// > Works similarly to [`std::fs::write`].
///
/// Uses [`sys::ffi::playdate_file::open`] and [`sys::ffi::playdate_file::write`].
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<(), ApiError> {
	let mut file = File::options().write(true).append(false).open(&path)?;
	file.write(contents.as_ref())?;
	Ok(())
}


#[inline(always)]
/// Removes a file from the filesystem. Directory is a file too.
///
/// > Works similarly to [`std::fs::remove_file`] and [`std::fs::remove_dir`].
///
/// Uses [`sys::ffi::playdate_file::unlink`].
pub fn remove<P: AsRef<Path>>(path: P) -> Result<(), ApiError> { Fs::Default().remove(path) }


// TODO: metadata
/// Given a path, query the file system to get information about a file,
/// directory, etc.
#[inline(always)]
pub fn metadata<P: AsRef<Path>>(path: P) -> Result<FileStat, ApiError> { Fs::Default().metadata(path) }


/// Renames the file at `from` to `to`.
///
/// It will overwrite the file at `to`.
///
/// It does not create intermediate folders.
#[inline(always)]
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<(), ApiError> {
	Fs::Default().rename(from, to)
}

/// Creates the given `path` in the `Data/<gameid>` folder.
///
/// It does not create intermediate folders.
#[inline(always)]
pub fn create_dir<P: AsRef<Path>>(path: P) -> Result<(), ApiError> { Fs::Default().create_dir(path) }

// TODO: create_dir_all
// pub fn create_dir_all<P: AsRef<Path>>(path: P) -> Result<()>


#[inline(always)]
/// Removes a directory and all of its contents recursively.
/// > Works similarly to [`std::fs::remove_file`], [`std::fs::remove_dir`] and [`std::fs::remove_dir_all`].
///
/// Caution: it also can delete file without any error, if `path` is a file.
///
/// Calls [`sys::ffi::playdate_file::unlink`] with `recursive`.
// XXX: TODO: Should we validate that `path` is a directory?
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<(), ApiError> { Fs::Default().remove_dir_all(path) }

// TODO: read_dir -> iter ReadDir
// pub fn read_dir<P: AsRef<Path>>(path: P) -> Result<ReadDir>


/// Playdate File-system API.
///
/// Uses inner api end-point for all operations.
#[derive(Debug, Clone, Copy)]
pub struct Fs<Api = api::Default>(Api);

impl Fs<api::Default> {
	/// Creates default [`Fs`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl Fs<api::Cache> {
	/// Creates [`Fs`] without type parameter requirement.
	///
	/// Uses [`api::Cache`].
	#[allow(non_snake_case)]
	pub fn Cached() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> Default for Fs<Api> {
	fn default() -> Self { Self(Default::default()) }
}

impl<Api: Default + api::Api> Fs<Api> {
	pub fn new() -> Self { Self(Default::default()) }
}

impl<Api: api::Api> Fs<Api> {
	pub fn new_with(api: Api) -> Self { Self(api) }
}


mod ops {
	use super::*;

	pub fn open<Api: api::Api, P: AsRef<Path>, Opts: OpenOptions>(api: Api,
	                                                              path: P,
	                                                              options: Opts)
	                                                              -> Result<File<Api>, ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = api.open();
		let ptr = unsafe { f(path.as_ptr() as _, options.into()) };
		Ok(File(ptr as _, api))
	}

	pub fn open_with<UApi: api::Api, FApi: api::Api, P: AsRef<Path>, Opts: OpenOptions>(
		using: UApi,
		api: FApi,
		path: P,
		options: Opts)
		-> Result<File<FApi>, ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = using.open();
		let ptr = unsafe { f(path.as_ptr() as _, options.into()) };
		Ok(File(ptr as _, api))
	}

	pub fn close<Api: api::Api>(mut file: File<Api>) -> Result<(), Error> {
		let f = file.1.close();
		let result = unsafe { f(file.0 as _) };
		file.0 = core::ptr::null_mut();
		Error::ok_from_code(result)?;
		Ok(())
	}

	pub fn close_with<Api: api::Api, FApi: api::Api>(api: Api, mut file: File<FApi>) -> Result<(), Error> {
		let f = api.close();
		let result = unsafe { f(file.0) };
		file.0 = core::ptr::null_mut();
		Error::ok_from_code(result)?;
		Ok(())
	}

	pub fn seek<Api: api::Api>(file: &mut File<Api>, pos: c_int, whence: Whence) -> Result<(), Error> {
		let f = file.1.seek();
		let result = unsafe { f(file.0, pos, whence as _) };
		Error::ok_from_code(result)?;
		Ok(())
	}

	pub fn seek_with<Api: api::Api>(api: Api,
	                                file: &mut impl AnyFile,
	                                pos: c_int,
	                                whence: Whence)
	                                -> Result<(), Error> {
		let f = api.seek();
		let result = unsafe { f(file.as_raw(), pos, whence as _) };
		Error::ok_from_code(result)?;
		Ok(())
	}

	pub fn tell<Api: api::Api>(file: &mut File<Api>) -> Result<c_uint, Error> {
		let f = file.1.tell();
		let result = unsafe { f(file.0) };
		Error::ok_from_code(result)
	}

	pub fn tell_with<Api: api::Api>(api: Api, file: &mut impl AnyFile) -> Result<c_uint, Error> {
		let f = api.tell();
		let result = unsafe { f(file.as_raw()) };
		Error::ok_from_code(result)
	}


	pub fn read<Api: api::Api>(file: &mut File<Api>, to: &mut Vec<u8>, len: c_uint) -> Result<c_uint, Error> {
		let f = file.1.read();
		let result = unsafe { f(file.0, to.as_mut_ptr() as *mut _, len) };
		Error::ok_from_code(result)
	}

	pub fn write<Api: api::Api>(file: &mut File<Api>, from: &[u8]) -> Result<c_uint, Error> {
		let f = file.1.write();
		let result = unsafe { f(file.0, from.as_ptr() as *mut _, from.len() as _) };
		Error::ok_from_code(result)
	}

	pub fn flush<Api: api::Api>(file: &mut File<Api>) -> Result<c_uint, Error> {
		let f = file.1.flush();
		let result = unsafe { f(file.0) };
		Error::ok_from_code(result)
	}
}


impl<Api: api::Api> Fs<Api> {
	/// Open file for given `options`.
	///
	/// Creates new [`File`] instance with copy of inner api end-point.
	///
	/// Equivalent to [`sys::ffi::playdate_file::open`]
	#[doc(alias = "sys::ffi::playdate_file::open")]
	#[inline(always)]
	pub fn open<P: AsRef<Path>, Opts: OpenOptions>(&self, path: P, options: Opts) -> Result<File<Api>, ApiError>
		where Api: Copy {
		ops::open(self.0, path, options)
	}

	/// Open file for given `options`.
	///
	/// Creates new [`File`] instance with given `api`.
	///
	/// Equivalent to [`sys::ffi::playdate_file::open`]
	#[doc(alias = "sys::ffi::playdate_file::open")]
	#[inline(always)]
	pub fn open_with<T: api::Api, P: AsRef<Path>, Opts: OpenOptions>(&self,
	                                                                 api: T,
	                                                                 path: P,
	                                                                 options: Opts)
	                                                                 -> Result<File<T>, ApiError> {
		ops::open_with(&self.0, api, path, options)
	}

	/// Closes the given file.
	///
	/// Equivalent to [`sys::ffi::playdate_file::close`]
	#[doc(alias = "sys::ffi::playdate_file::close")]
	#[inline(always)]
	pub fn close<T: api::Api>(&self, file: File<T>) -> Result<(), Error> { ops::close_with(&self.0, file) }


	/// Returns the current read/write offset in the given file.
	///
	/// Equivalent to [`sys::ffi::playdate_file::tell`]
	#[doc(alias = "sys::ffi::playdate_file::tell")]
	#[inline(always)]
	pub fn tell(&self, file: &mut impl AnyFile) -> Result<c_uint, Error> { crate::ops::tell_with(&self.0, file) }

	/// Sets the read/write offset in the given file to pos, relative to the `whence`.
	/// - [`Whence::Start`] is relative to the beginning of the file,
	/// - [`Whence::Current`] is relative to the current position of the file,
	/// - [`Whence::End`] is relative to the end of the file.
	///
	/// Equivalent to [`sys::ffi::playdate_file::seek`]
	#[doc(alias = "sys::ffi::playdate_file::seek")]
	#[inline(always)]
	pub fn seek_raw(&self, file: &mut impl AnyFile, pos: c_int, whence: Whence) -> Result<(), Error> {
		crate::ops::seek_with(&self.0, file, pos, whence)
	}


	/// Reads up to `len` bytes from the file into the buffer `to`.
	///
	/// Returns the number of bytes read (0 indicating end of file).
	///
	/// Caution: Vector must be prefilled with `0`s.
	/// ```no_run
	/// let mut buf = Vec::<u8>::with_capacity(size);
	/// buf.resize(size, 0);
	/// fs.read(&mut file, &mut buf, size)?;
	/// ```
	///
	/// Equivalent to [`sys::ffi::playdate_file::read`]
	#[doc(alias = "sys::ffi::playdate_file::read")]
	pub fn read(&self, file: &mut impl AnyFile, to: &mut Vec<u8>, len: c_uint) -> Result<c_uint, Error> {
		let f = self.0.read();
		let result = unsafe { f(file.as_raw(), to.as_mut_ptr() as *mut _, len) };
		Error::ok_from_code(result)
	}


	/// Writes the buffer of bytes buf to the file.
	///
	/// Returns the number of bytes written.
	///
	/// Equivalent to [`sys::ffi::playdate_file::write`]
	#[doc(alias = "sys::ffi::playdate_file::write")]
	pub fn write(&self, file: &mut impl AnyFile, from: &[u8]) -> Result<c_uint, Error> {
		let f = self.0.write();
		let result = unsafe { f(file.as_raw(), from.as_ptr() as *mut _, from.len() as _) };
		Error::ok_from_code(result)
	}

	/// Flushes the output buffer of file immediately.
	///
	/// Returns the number of bytes written.
	///
	/// Equivalent to [`sys::ffi::playdate_file::flush`]
	#[doc(alias = "sys::ffi::playdate_file::flush")]
	pub fn flush(&self, file: &mut impl AnyFile) -> Result<c_uint, Error> {
		let f = self.0.flush();
		let result = unsafe { f(file.as_raw()) };
		Error::ok_from_code(result)
	}


	/// Populates the [`FileStat`] stat with information about the file at `path`.
	///
	/// Equivalent to [`sys::ffi::playdate_file::stat`]
	#[doc(alias = "sys::ffi::playdate_file::stat")]
	pub fn metadata<P: AsRef<Path>>(&self, path: P) -> Result<FileStat, ApiError> {
		let mut stat = FileStat { isdir: 0,
		                          size: 0,
		                          m_year: 0,
		                          m_month: 0,
		                          m_day: 0,
		                          m_hour: 0,
		                          m_minute: 0,
		                          m_second: 0 };
		self.metadata_to(path, &mut stat).map(|_| stat)
	}

	/// Writes into the given `metadata` information about the file at `path`.
	///
	/// Equivalent to [`sys::ffi::playdate_file::stat`]
	#[doc(alias = "sys::ffi::playdate_file::stat")]
	pub fn metadata_to<P: AsRef<Path>>(&self, path: P, metadata: &mut FileStat) -> Result<(), ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = self.0.stat();
		let result = unsafe { f(path.as_ptr() as _, metadata as *mut _) };
		Error::ok_from_code(result)?;
		Ok(())
	}


	// path- fs operations //

	/// Creates the given `path` in the `Data/<gameid>` folder.
	///
	/// It does not create intermediate folders.
	///
	/// Equivalent to [`sys::ffi::playdate_file::mkdir`]
	#[doc(alias = "sys::ffi::playdate_file::mkdir")]
	pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<(), ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = self.0.mkdir();
		let result = unsafe { f(path.as_ptr() as _) };
		Error::ok_from_code(result)?;
		Ok(())
	}

	/// Deletes the file at path.
	/// Directory is a file too, definitely.
	///
	/// See also [`remove_dir_all`](Fs::remove_dir_all).
	///
	/// Equivalent to [`sys::ffi::playdate_file::unlink`]
	#[doc(alias = "sys::ffi::playdate_file::unlink")]
	pub fn remove<P: AsRef<Path>>(&self, path: P) -> Result<(), ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = self.0.unlink();
		let result = unsafe { f(path.as_ptr() as _, 0) };
		Error::ok_from_code(result)?;
		Ok(())
	}

	/// Deletes the file at path.
	///
	/// If the `path` is a folder, this deletes everything inside the folder
	/// (including folders, folders inside those, and so on) as well as the folder itself.
	///
	/// Equivalent to [`sys::ffi::playdate_file::unlink`]
	#[doc(alias = "sys::ffi::playdate_file::unlink")]
	pub fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<(), ApiError> {
		let path = CString::new(path.as_ref())?;
		let f = self.0.unlink();
		let result = unsafe { f(path.as_ptr() as _, 1) };
		Error::ok_from_code(result)?;
		Ok(())
	}

	/// Renames the file at `from` to `to`.
	///
	/// It will overwrite the file at `to`.
	///
	/// It does not create intermediate folders.
	///
	/// Equivalent to [`sys::ffi::playdate_file::rename`]
	#[doc(alias = "sys::ffi::playdate_file::rename")]
	pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(&self, from: P, to: Q) -> Result<(), ApiError> {
		let from = CString::new(from.as_ref())?;
		let to = CString::new(to.as_ref())?;
		let f = self.0.rename();
		let result = unsafe { f(from.as_ptr() as _, to.as_ptr() as _) };
		Error::ok_from_code(result)?;
		Ok(())
	}


	// read dir //

	/// Calls the given callback function for every file at `path`.
	///
	/// Subfolders are indicated by a trailing slash '/' in filename.
	///
	/// This method does not recurse into subfolders.
	///
	/// If `include_hidden` is set, files beginning with a period will be included;
	/// otherwise, they are skipped.
	///
	/// Returns error if no folder exists at path or it can’t be opened.
	///
	/// Equivalent to [`sys::ffi::playdate_file::listfiles`]
	#[doc(alias = "sys::ffi::playdate_file::listfiles")]
	pub fn read_dir<P, Fn>(&self, path: P, mut callback: Fn, include_hidden: bool) -> Result<(), ApiError>
		where P: AsRef<Path>,
		      Fn: FnMut(String) {
		unsafe extern "C" fn proxy<Fn: FnMut(String)>(filename: *const c_char, userdata: *mut c_void) {
			// TODO: are we really need `into_owned` for storing in the temp vec?
			let filename = CStr::from_ptr(filename as _).to_string_lossy().into_owned();

			if let Some(callback) = (userdata as *mut _ as *mut Fn).as_mut() {
				callback(filename);
			} else {
				panic!("Fs.read_dir: missed callback");
			}
		}

		let path = CString::new(path.as_ref())?;

		// NOTE: that's safe because ref dies after internal listfiles() returns.
		let callback_ref = (&mut callback) as *mut Fn as *mut _;

		let f = self.0.listfiles();
		let result = unsafe {
			f(
			  path.as_ptr() as _,
			  Some(proxy::<Fn>),
			  callback_ref,
			  include_hidden as _,
			)
		};
		Error::ok_from_code(result)?;
		Ok(())
	}
}


pub mod prelude {
	pub use sys::ffi::FileStat;
	pub use sys::ffi::FileOptions;
	pub use crate::error::ApiError as FsApiError;
	pub use crate::error::Error as FsError;
	pub use crate::Path;
	pub use crate::Fs;
	pub use crate::file::*;
	pub use crate::options::*;
	pub use crate::seek::SeekFrom;
}