stof_github/
lib.rs

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::{collections::HashMap, time::Duration};
use anyhow::Result;
use stof::{Format, SDoc};
use ureq::{Agent, AgentBuilder};


/// Stof GitHub Format.
pub struct GitHubFormat {
    /// Format Repo ID.
    /// Ex. "formata" means format is "github:formata".
    pub repo_id: String,

    /// Repository owner.
    pub owner: String,

    /// Repository name.
    pub repo: String,

    /// Headers.
    pub headers: HashMap<String, String>,

    /// Agent.
    pub agent: Agent,
}
impl GitHubFormat {
    /// Create a new GitHub Format.
    pub fn new(repo: &str, owner: &str) -> Self {
        let mut headers = HashMap::new();
        headers.insert("Accept".to_string(), "application/vnd.github.raw+json".to_string());
        headers.insert("X-GitHub-Api-Version".to_string(), "2022-11-28".to_string());
        Self {
            repo_id: repo.to_owned(),
            owner: owner.to_owned(),
            repo: repo.to_owned(),
            headers,
            agent: AgentBuilder::new()
                .timeout_read(Duration::from_secs(3))
                .timeout_write(Duration::from_secs(3))
                .build(),
        }
    }

    /// The URL for a request into this GitHub repository.
    pub fn url(&self, path: &str) -> String {
        format!("https://api.github.com/repos/{}/{}/contents/{}", self.owner, self.repo, path)
    }

    /// Get the string contents for a file path into this GitHub repository.
    pub fn get(&self, file_path: &str) -> Result<String> {
        let url = self.url(file_path);
        let mut request = self.agent.get(&url);
        for (key, value) in &self.headers {
            request = request.set(key, value);
        }
        let response = request.call()?;
        Ok(response.into_string()?)
    }
}
impl Format for GitHubFormat {
    /// How this format will be accessed in Stof.
    /// For example, if repo_id is "formata", using this library would be the format identifier "github:formata".
    ///
    /// `import github:formata "myfile.stof" as Import;`
    fn format(&self) -> String {
        format!("github:{}", self.repo_id)
    }

    /// The GitHub format only allows a file import.
    /// Gets the contents of the file at a path in this GitHub repo, then imports it as a string using the file extension.
    /// Will error if a Format with the requested file extension is not available in the doc.
    fn file_import(&self, pid: &str, doc: &mut SDoc, _format: &str, full_path: &str, extension: &str, as_name: &str) -> Result<()> {
        let contents = self.get(full_path)?;
        doc.string_import(pid, extension, &contents, as_name)
    }
}


#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use stof::SDoc;
    use crate::GitHubFormat;

    #[test]
    fn test() {
        let mut doc = SDoc::default();
        doc.load_format(Arc::new(GitHubFormat::new("stof", "dev-formata-io"))); // github:stof

        doc.string_import("main", "stof", r#"
            
            import github:stof "web/deno.json"; // Will import deno.json using the "json" format into "root"

            #[test('@formata/stof')]
            fn name(): str {
                return self.name;
            }

            #[test('Apache-2.0')]
            fn license(): str {
                return self.license;
            }

        "#, "").unwrap();

        let res = doc.run_tests(true, None);
        match res {
            Ok(message) => {
                println!("{message}");
            },
            Err(error) => {
                panic!("{error}");
            }
        }
    }
}