1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
5 #![allow(unused_variables)]
14 pub type Result<T, WE> = std::result::Result<T, Error<WE>>;
17 macro_rules! not_impl {
19 Err(Error::NotImpl(format!("{}:{}", file!(), line!())))
23 #[derive(Error, Debug)]
24 pub enum Error<WE: Debug> {
25 #[error("write error: {0:?}")]
28 #[error("a string may contain invalid utf-8")]
31 //TODO(hrust): This is a temp error during porting
32 #[error("NOT_IMPL: {0}")]
35 #[error("Failed: {0}")]
39 impl<WE: Debug> Error<WE> {
40 pub fn fail(s: impl Into<String>) -> Self {
47 fn write(&mut self, s: impl AsRef<str>) -> Result<(), Self::Error>;
49 fn write_if(&mut self, cond: bool, s: impl AsRef<str>) -> Result<(), Self::Error> {
58 impl<W: fmt::Write> Write for W {
59 type Error = fmt::Error;
60 fn write(&mut self, s: impl AsRef<str>) -> Result<(), Self::Error> {
61 self.write_str(s.as_ref()).map_err(Error::WriteError)
65 pub struct IoWrite(Box<dyn io::Write + Send>);
68 pub fn new(w: impl io::Write + 'static + Send) -> Self {
72 pub fn flush(&mut self) -> std::result::Result<(), io::Error> {
77 impl Write for IoWrite {
78 type Error = std::io::Error;
79 fn write(&mut self, s: impl AsRef<str>) -> Result<(), Self::Error> {
81 .write_all(s.as_ref().as_bytes())
82 .map_err(Error::WriteError)
86 pub fn newline<W: Write>(w: &mut W) -> Result<(), W::Error> {
90 pub fn wrap_by_<W, F>(w: &mut W, s: &str, e: &str, f: F) -> Result<(), W::Error>
93 F: FnOnce(&mut W) -> Result<(), W::Error>,
100 pub fn wrap_by<W: Write, F>(w: &mut W, s: &str, f: F) -> Result<(), W::Error>
102 F: FnOnce(&mut W) -> Result<(), W::Error>,
107 pub fn wrap_by_braces<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
109 F: FnOnce(&mut W) -> Result<(), W::Error>,
111 wrap_by_(w, "{", "}", f)
114 pub fn wrap_by_paren<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
116 F: FnOnce(&mut W) -> Result<(), W::Error>,
118 wrap_by_(w, "(", ")", f)
121 pub fn wrap_by_quotes<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
123 F: FnOnce(&mut W) -> Result<(), W::Error>,
125 wrap_by_(w, "\"", "\"", f)
128 pub fn wrap_by_triple_quotes<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
130 F: FnOnce(&mut W) -> Result<(), W::Error>,
132 wrap_by_(w, "\"\"\"", "\"\"\"", f)
135 pub fn wrap_by_angle<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
137 F: FnOnce(&mut W) -> Result<(), W::Error>,
139 wrap_by_(w, "<", ">", f)
142 pub fn wrap_by_square<W: Write, F>(w: &mut W, f: F) -> Result<(), W::Error>
144 F: FnOnce(&mut W) -> Result<(), W::Error>,
146 wrap_by_(w, "[", "]", f)
149 pub fn write_list<W: Write>(w: &mut W, items: &[impl AsRef<str>]) -> Result<(), W::Error> {
151 w.write(i.as_ref())?;
155 pub fn concat_str<W: Write, I: AsRef<str>>(w: &mut W, ss: impl AsRef<[I]>) -> Result<(), W::Error> {
156 concat(w, ss, |w, s| w.write(s))
159 pub fn concat_str_by<W: Write, I: AsRef<str>>(
161 sep: impl AsRef<str>,
163 ) -> Result<(), W::Error> {
164 concat_by(w, sep, ss, |w, s| w.write(s))
167 pub fn concat<W, T, F>(w: &mut W, items: impl AsRef<[T]>, f: F) -> Result<(), W::Error>
170 F: FnMut(&mut W, &T) -> Result<(), W::Error>,
172 concat_by(w, "", items, f)
175 pub fn concat_by<W, T, F>(
177 sep: impl AsRef<str>,
178 items: impl AsRef<[T]>,
180 ) -> Result<(), W::Error>
183 F: FnMut(&mut W, &T) -> Result<(), W::Error>,
185 let mut first = true;
186 let sep = sep.as_ref();
187 Ok(for i in items.as_ref() {
197 pub fn option<W: Write, T, F>(w: &mut W, i: impl Into<Option<T>>, f: F) -> Result<(), W::Error>
199 F: Fn(&mut W, T) -> Result<(), W::Error>,
207 pub fn option_or<W: Write, T, F>(
209 i: impl Into<Option<T>>,
211 default: impl AsRef<str>,
212 ) -> Result<(), W::Error>
214 F: Fn(&mut W, T) -> Result<(), W::Error>,
217 None => w.write(default),