diff options
| author | Teddy Wing | 2022-03-19 20:41:31 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2022-03-19 20:41:31 +0100 | 
| commit | 78358c6f9e084b27a5fc513095a972c6b0e3361a (patch) | |
| tree | c2d08284fec9978dff5c9376da52d95bafd61cac | |
| parent | c75a82361cb3566d196a1fb930ea4833f40b0ffd (diff) | |
| download | yaqlite-78358c6f9e084b27a5fc513095a972c6b0e3361a.tar.bz2 | |
main: Write YAML to standard output
Create an adapter from `std::fmt::Write` to `std::io::Write` based on
the following idea in the standard library:
https://github.com/rust-lang/rust/blob/master/library/std/src/io/mod.rs#L1634-L1652
Since `yaml_rust::YamlEmitter::new()` requires a `std::fmt::Write`,
adapt standard output so the emitter can write to it.
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/yaml.rs | 3 | ||||
| -rw-r--r-- | src/yaml/write.rs | 18 | 
3 files changed, 25 insertions, 3 deletions
| diff --git a/src/main.rs b/src/main.rs index 7f372ea..2ac689c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,9 +94,10 @@ fn main() {                  ).unwrap(),              }; -            let mut emitter = yaml_rust::YamlEmitter::new( -                &mut std::io::stdout().lock(), -            ); +            let stdout = std::io::stdout(); +            let mut stdout_handle = stdout.lock(); +            let mut buffer = yaqlite::yaml::IoAdapter::new(&mut stdout_handle); +            let mut emitter = yaml_rust::YamlEmitter::new(&mut buffer);              emitter.dump(&yaml_data).unwrap();              dbconn.close().unwrap(); diff --git a/src/yaml.rs b/src/yaml.rs index 77665b3..9b0988c 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -5,9 +5,12 @@ use std::collections::HashMap;  mod sql; +mod write;  pub(crate) use sql::*; +pub use write::*; +  // TODO: Separate functions to get a list of YAML hashes, and insert hashes into  // the database. diff --git a/src/yaml/write.rs b/src/yaml/write.rs new file mode 100644 index 0000000..a1e52a5 --- /dev/null +++ b/src/yaml/write.rs @@ -0,0 +1,18 @@ +pub struct IoAdapter<'a, T: std::io::Write> { +    inner: &'a mut T, +} + +impl<'a, T: std::io::Write> IoAdapter<'a, T> { +    pub fn new(writer: &'a mut T) -> Self { +        IoAdapter { inner: writer } +    } +} + +impl<T: std::io::Write> std::fmt::Write for IoAdapter<'_, T> { +    fn write_str(&mut self, s: &str) -> std::fmt::Result { +        match self.inner.write_all(s.as_bytes()) { +            Ok(()) => Ok(()), +            Err(_) => Err(std::fmt::Error), +        } +    } +} | 
