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 struct ReadJSONFromFileError;
34
35pub fn read_json_from_file<T>(config_dir: &Path, filename: &str) -> Result<T, ReadJSONFromFileError>
36where
37 T: for<'de> Deserialize<'de>,
38{
39 let path = config_dir.join(filename);
40 let display = path.display();
41
42 let file = match File::open(&path) {
43 Err(why) => {
44 warn!("couldn't open {}: {}", display, why);
45 return Err(ReadJSONFromFileError);
46 },
47 Ok(file) => file,
48 };
49 let buf_reader = BufReader::new(file);
50
51 match serde_json::from_reader(buf_reader) {
52 Ok(decoded_buffer) => Ok(decoded_buffer),
53 Err(why) => {
54 warn!("Could not deserialize file {}", why);
55 Err(ReadJSONFromFileError)
56 },
57 }
58}
59
60pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
61where
62 T: Serialize,
63{
64 let path = config_dir.join(filename);
65 let display = path.display();
66
67 let mut file = match File::create(&path) {
68 Err(why) => panic!("couldn't create {}: {}", display, why),
69 Ok(file) => file,
70 };
71 let mut writer = BufWriter::new(&mut file);
72 serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
73 trace!("successfully wrote to {display}");
74}
75
76#[derive(
78 Clone,
79 Copy,
80 Debug,
81 Default,
82 Deserialize,
83 Eq,
84 Hash,
85 Ord,
86 PartialEq,
87 PartialOrd,
88 Serialize,
89 MallocSizeOf,
90)]
91pub struct Epoch(pub u32);
92
93impl Epoch {
94 pub fn next(&self) -> Self {
95 Self(self.0 + 1)
96 }
97}
98
99impl From<Epoch> for WebRenderEpoch {
100 fn from(val: Epoch) -> Self {
101 WebRenderEpoch(val.0)
102 }
103}
104
105pub trait WebRenderEpochToU16 {
106 fn as_u16(&self) -> u16;
107}
108
109impl WebRenderEpochToU16 for WebRenderEpoch {
110 fn as_u16(&self) -> u16 {
114 (self.0 % u16::MAX as u32) as u16
115 }
116}