aws_iam/io/
mod.rs

1/*!
2Provides basic file read/write functions for policies.
3
4This module wraps basic read and write operations and the relevant Serde serialization and
5deserialization logic. Both read and write functions come in two forms, one which takes a
6file name in the form of a `PathBuf` and one which either takes an implementation of
7`std::io::Read` or `std::io::Write`.
8
9# Example
10
11The following reads a policy document from a JSON file and returns the parsed form.
12
13```rust
14use aws_iam::{io, model::*};
15use std::path::PathBuf;
16
17let policy = io::read_from_file(
18        &PathBuf::from("tests/data/good/example-021.json")
19    ).expect("Error reading file");
20```
21*/
22
23use crate::model::Policy;
24use std::fs::OpenOptions;
25use std::io;
26use std::io::{BufReader, BufWriter, Read, Write};
27use std::path::PathBuf;
28
29// ------------------------------------------------------------------------------------------------
30// Public Types
31// ------------------------------------------------------------------------------------------------
32
33///
34/// Errors possible with file read/write.
35///
36#[derive(Debug)]
37pub enum Error {
38    /// Wrapper for any IO error that occurs during reading.
39    ReadingFile(io::Error),
40    /// Wrapper for any IO error that occurs during writing.
41    WritingFile(io::Error),
42    /// Wrapper for Serde error serializing object to JSON.
43    SerializingPolicy(String),
44    /// Wrapper for Serde error de-serializing JSON to object.
45    DeserializingJson(String),
46    /// The policy read from a file is not valid.
47    InvalidPolicy,
48}
49
50// ------------------------------------------------------------------------------------------------
51// Public Functions
52// ------------------------------------------------------------------------------------------------
53
54///
55/// Read a `Policy` document from the file at `path`.
56///
57pub fn read_from_file(path: &PathBuf) -> Result<Policy, Error> {
58    match OpenOptions::new().read(true).open(path) {
59        Ok(f) => read_from_reader(f),
60        Err(e) => Err(Error::ReadingFile(e)),
61    }
62}
63
64///
65/// Read a `Policy` document from any implementation of `std::io::Read`.
66///
67pub fn read_from_reader<R>(reader: R) -> Result<Policy, Error>
68where
69    R: Read + Sized,
70{
71    let reader = BufReader::new(reader);
72    match serde_json::from_reader(reader) {
73        Ok(policy) => Ok(policy),
74        Err(e) => Err(Error::DeserializingJson(e.to_string())),
75    }
76}
77
78///
79/// Read a `Policy` document from a string.
80///
81pub fn read_from_string(s: &str) -> Result<Policy, Error> {
82    read_from_reader(s.as_bytes())
83}
84
85///
86/// Write the `policy` object to a file at `path`, this will create a file if it does
87/// not exist and overwrite any file if it exists.
88///
89pub fn write_to_file(path: &PathBuf, policy: &Policy) -> Result<(), Error> {
90    match OpenOptions::new()
91        .write(true)
92        .create(true)
93        .truncate(true)
94        .open(path)
95    {
96        Ok(f) => write_to_writer(f, policy),
97        Err(e) => Err(Error::WritingFile(e)),
98    }
99}
100
101///
102/// Write the `policy` object to any implementation of `std::io::Write`.
103///
104pub fn write_to_writer<W>(writer: W, policy: &Policy) -> Result<(), Error>
105where
106    W: Write + Sized,
107{
108    let writer = BufWriter::new(writer);
109    match serde_json::to_writer_pretty(writer, policy) {
110        Ok(_) => Ok(()),
111        Err(e) => Err(Error::SerializingPolicy(e.to_string())),
112    }
113}