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