1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use jsona_util::{environment::Environment, schema::Schemas};
use url::Url;

pub use crate::commands::{AppArgs, Colors, GeneralArgs};

use anyhow::anyhow;
pub mod commands;
pub mod printing;

pub struct App<E: Environment> {
    env: E,
    colors: bool,
    schemas: Schemas<E>,
}

impl<E: Environment> App<E> {
    pub fn new(env: E) -> Self {
        Self {
            schemas: Schemas::new(env.clone()),
            colors: env.atty_stderr(),
            env,
        }
    }
    pub async fn load_file(&self, path: &str) -> Result<(Url, String), anyhow::Error> {
        let url = self
            .env
            .to_file_uri(path)
            .ok_or_else(|| anyhow!("invalid file path"))?;
        let data = self.env.read_file(&url).await?;
        let content = std::str::from_utf8(&data).map_err(|_| anyhow!("invalid utf8 content"))?;
        Ok((url, content.to_string()))
    }
}