1#![deny(unsafe_code)]
6
7pub mod cross_process_instant;
13pub mod generic_channel;
14#[macro_use]
15pub mod id;
16pub mod print_tree;
17mod rope;
18pub mod text;
19pub mod threadboost;
20pub mod threadpool;
21mod unicode_block;
22
23use std::fs::File;
24use std::io::{BufReader, BufWriter};
25use std::path::Path;
26
27use log::{trace, warn};
28use malloc_size_of_derive::MallocSizeOf;
29pub use rope::{Rope, RopeChars, RopeIndex, RopeMovement, RopeSlice};
30use serde::{Deserialize, Serialize};
31use webrender_api::Epoch as WebRenderEpoch;
32
33pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
34where
35 T: for<'de> Deserialize<'de>,
36{
37 let path = config_dir.join(filename);
38 let display = path.display();
39
40 let file = match File::open(&path) {
41 Err(why) => {
42 warn!("couldn't open {}: {}", display, why);
43 return;
44 },
45 Ok(file) => file,
46 };
47 let buf_reader = BufReader::new(file);
48
49 match serde_json::from_reader(buf_reader) {
50 Ok(decoded_buffer) => *data = decoded_buffer,
51 Err(why) => warn!("Could not deserialize file {}", why),
52 }
53}
54
55pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
56where
57 T: Serialize,
58{
59 let path = config_dir.join(filename);
60 let display = path.display();
61
62 let mut file = match File::create(&path) {
63 Err(why) => panic!("couldn't create {}: {}", display, why),
64 Ok(file) => file,
65 };
66 let mut writer = BufWriter::new(&mut file);
67 serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
68 trace!("successfully wrote to {display}");
69}
70
71#[derive(
73 Clone,
74 Copy,
75 Debug,
76 Default,
77 Deserialize,
78 Eq,
79 Hash,
80 Ord,
81 PartialEq,
82 PartialOrd,
83 Serialize,
84 MallocSizeOf,
85)]
86pub struct Epoch(pub u32);
87
88impl Epoch {
89 pub fn next(&self) -> Self {
90 Self(self.0 + 1)
91 }
92}
93
94impl From<Epoch> for WebRenderEpoch {
95 fn from(val: Epoch) -> Self {
96 WebRenderEpoch(val.0)
97 }
98}
99
100pub trait WebRenderEpochToU16 {
101 fn as_u16(&self) -> u16;
102}
103
104impl WebRenderEpochToU16 for WebRenderEpoch {
105 fn as_u16(&self) -> u16 {
109 (self.0 % u16::MAX as u32) as u16
110 }
111}