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
//! Configuration structs for the provider and tracers

use rill_protocol::config::ConfigPatch;
use rill_protocol::io::provider::{EntryId, StreamType};
use serde::Deserialize;

/// The external user app can set this value to override default server.
/// If embedded server started it can put its socket address here.
pub static NODE: ConfigPatch<String> = ConfigPatch::new("RR_NODE");

/// The external user app can set this value to override the default name.
pub static NAME: ConfigPatch<EntryId> = ConfigPatch::new("RR_NAME");

/// Provider configuration
#[derive(Deserialize, Debug, Clone)]
pub struct EngineConfig {
    // TODO: Use default serde value instead
    /// Node where connect the provider
    pub node: Option<String>,
    // TODO: Use default serde value instead
    /// The name of the provider
    pub name: Option<EntryId>,
    /// The type of the provider
    pub provider_type: StreamType,
}

impl EngineConfig {
    /// Creates a new `EngineConfig` of the specified type.
    pub fn new(provider_type: StreamType) -> Self {
        Self {
            node: None,
            name: None,
            provider_type,
        }
    }
}

impl EngineConfig {
    /// Returns `true` if node explicitly specified.
    pub fn is_node_specified(&self) -> bool {
        NODE.env_var().transpose().is_some() || self.node.is_some()
    }

    /// Full url of the node
    pub fn node_url(&self) -> String {
        let host = NODE.get(|| self.node.clone(), || "localhost:1636".into());
        format!("ws://{}/live/provider", host)
    }

    /// Name of the provider
    pub fn provider_name(&self) -> EntryId {
        NAME.get(
            || self.name.clone(),
            || {
                std::env::current_exe()
                    .ok()
                    .as_ref()
                    .and_then(|path| path.as_path().file_name())
                    .and_then(|path| path.to_str().map(EntryId::from))
                    .unwrap_or_else(|| "rillrate".into())
            },
        )
    }

    /// The type of the provider
    pub fn provider_type(&self) -> StreamType {
        self.provider_type.clone()
    }
}