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
use std::env;
use std::path::PathBuf;

/// Represents context about an Azure Function invocation.
#[derive(Debug)]
pub struct Context<'a> {
    invocation_id: &'a str,
    function_id: &'a str,
    name: &'a str,
}

impl<'a> Context<'a> {
    /// Creates a new function invocation context.
    pub fn new(invocation_id: &'a str, function_id: &'a str, name: &'a str) -> Self {
        Context {
            invocation_id,
            function_id,
            name,
        }
    }

    /// Gets the invocation identifier for the current Azure Function.
    pub fn invocation_id(&self) -> &str {
        self.invocation_id
    }

    /// Gets the function identifier for the current Azure Function.
    pub fn function_id(&self) -> &str {
        self.function_id
    }

    /// Gets the name of the current Azure Function.
    pub fn function_name(&self) -> &str {
        self.name
    }

    /// Gets the directory for the current Azure Function.
    pub fn function_directory(&self) -> Option<PathBuf> {
        self.app_directory().map(|p| p.join(self.name))
    }

    /// Gets the directory for the current Azure Function Application.
    pub fn app_directory(&self) -> Option<PathBuf> {
        env::current_exe()
            .map(|p| p.parent().map(|p| p.to_owned()))
            .ok()
            .unwrap_or(None)
    }
}