dragonfly_client_rs/client/
models.rs1use color_eyre::Result;
2use serde::Serialize;
3use serde::{self, Deserialize};
4use std::collections::HashMap;
5use std::fmt::Display;
6use yara::{Compiler, Rules};
7
8pub type ScanResult = Result<SubmitJobResultsSuccess, SubmitJobResultsError>;
9
10#[derive(Serialize, Debug)]
11#[serde(untagged)]
12#[serde(remote = "ScanResult")]
13enum ScanResultDef {
14 Ok(SubmitJobResultsSuccess),
15 Err(SubmitJobResultsError),
16}
17
18#[derive(Serialize)]
19pub struct ScanResultSerializer(#[serde(with = "ScanResultDef")] ScanResult);
20
21impl From<ScanResult> for ScanResultSerializer {
22 fn from(value: ScanResult) -> Self {
23 Self(value)
24 }
25}
26
27#[derive(Debug, Serialize, PartialEq)]
28pub struct SubmitJobResultsSuccess {
29 pub name: String,
30 pub version: String,
31 pub score: i64,
32 pub inspector_url: Option<String>,
33
34 pub rules_matched: Vec<String>,
36
37 pub commit: String,
39}
40
41#[derive(Debug, Serialize)]
42pub struct SubmitJobResultsError {
43 pub name: String,
44 pub version: String,
45 pub reason: String,
46}
47
48impl Display for SubmitJobResultsError {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 writeln!(f, "Name: {}", self.name)?;
51 writeln!(f, "Version: {}", self.version)?;
52 writeln!(f, "Reason: {}", self.reason)?;
53
54 Ok(())
55 }
56}
57
58#[derive(Debug, Deserialize)]
59pub struct Job {
60 pub hash: String,
61 pub name: String,
62 pub version: String,
63 pub distributions: Vec<String>,
64}
65
66#[derive(Debug, Deserialize)]
67pub struct RulesResponse {
68 pub hash: String,
69 pub rules: HashMap<String, String>,
70}
71
72impl RulesResponse {
73 pub fn compile(&self) -> Result<Rules> {
75 let rules_str = self
76 .rules
77 .values()
78 .map(String::as_ref)
79 .collect::<Vec<&str>>()
80 .join("\n");
81
82 let compiled_rules = Compiler::new()?
83 .add_rules_str(&rules_str)?
84 .compile_rules()?;
85
86 Ok(compiled_rules)
87 }
88}
89
90#[derive(Debug, Deserialize)]
91pub struct AuthResponse {
92 pub access_token: String,
93 pub expires_in: u32,
94}
95
96#[derive(Debug, Serialize)]
97pub struct AuthBody<'a> {
98 pub client_id: &'a str,
99 pub client_secret: &'a str,
100 pub audience: &'a str,
101 pub grant_type: &'a str,
102 pub username: &'a str,
103 pub password: &'a str,
104}