Files
CHORUS/target/doc/src/ucxl/watcher.rs.html

91 lines
11 KiB
HTML

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `UCXL/src/watcher.rs`."><title>watcher.rs - source</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-916cea96.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="ucxl" data-themes="" data-resource-suffix="" data-rustdoc-version="1.87.0 (17067e9ac 2025-05-09)" data-channel="1.87.0" data-search-js="search-e7298875.js" data-settings-js="settings-d72f25bb.js" ><script src="../../static.files/storage-82c7156e.js"></script><script defer src="../../static.files/src-script-63605ae7.js"></script><script defer src="../../src-files.js"></script><script defer src="../../static.files/main-fb8c74a8.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc src"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="src-sidebar-title"><h2>Files</h2></div></nav><div class="sidebar-resizer"></div><main><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1><div class="sub-heading">ucxl/</div>watcher.rs</h1><rustdoc-toolbar></rustdoc-toolbar></div><div class="example-wrap digits-2"><pre class="rust"><code><a href=#1 id=1 data-nosnippet>1</a><span class="doccomment">//! UCXL filesystem watcher.
<a href=#2 id=2 data-nosnippet>2</a>//!
<a href=#3 id=3 data-nosnippet>3</a>//! This module provides a thin wrapper around the `notify` crate to watch a
<a href=#4 id=4 data-nosnippet>4</a>//! directory (or "project") for filesystem events. When a change is detected,
<a href=#5 id=5 data-nosnippet>5</a>//! the watcher attempts to construct a corresponding `UCXLAddress` using a
<a href=#6 id=6 data-nosnippet>6</a>//! simple heuristic and logs the event. This is primarily used by CHORUS for
<a href=#7 id=7 data-nosnippet>7</a>//! reactive workflows such as automatically updating metadata when files are
<a href=#8 id=8 data-nosnippet>8</a>//! added, modified or removed.
<a href=#9 id=9 data-nosnippet>9</a>
<a href=#10 id=10 data-nosnippet>10</a></span><span class="kw">use </span>notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
<a href=#11 id=11 data-nosnippet>11</a><span class="kw">use </span>std::path::Path;
<a href=#12 id=12 data-nosnippet>12</a><span class="kw">use </span>std::sync::mpsc::channel;
<a href=#13 id=13 data-nosnippet>13</a><span class="kw">use </span><span class="kw">crate</span>::UCXLAddress;
<a href=#14 id=14 data-nosnippet>14</a><span class="kw">use </span>std::str::FromStr;
<a href=#15 id=15 data-nosnippet>15</a>
<a href=#16 id=16 data-nosnippet>16</a><span class="doccomment">/// Represents a watcher rooted at a specific base path.
<a href=#17 id=17 data-nosnippet>17</a>///
<a href=#18 id=18 data-nosnippet>18</a>/// **What**: Holds the absolute path that the watcher monitors.
<a href=#19 id=19 data-nosnippet>19</a>///
<a href=#20 id=20 data-nosnippet>20</a>/// **How**: The path is stored as a `PathBuf`. The watcher is created via the
<a href=#21 id=21 data-nosnippet>21</a>/// `new` constructor which accepts any type that can be referenced as a `Path`.
<a href=#22 id=22 data-nosnippet>22</a>/// The underlying `notify::RecommendedWatcher` is configured with the default
<a href=#23 id=23 data-nosnippet>23</a>/// `Config` and set to watch recursively.
<a href=#24 id=24 data-nosnippet>24</a>///
<a href=#25 id=25 data-nosnippet>25</a>/// **Why**: Encapsulating the watcher logic in a dedicated struct makes it easy
<a href=#26 id=26 data-nosnippet>26</a>/// to instantiate multiple independent watchers and keeps the public API tidy.
<a href=#27 id=27 data-nosnippet>27</a></span><span class="kw">pub struct </span>UCXLWatcher {
<a href=#28 id=28 data-nosnippet>28</a> base_path: std::path::PathBuf,
<a href=#29 id=29 data-nosnippet>29</a>}
<a href=#30 id=30 data-nosnippet>30</a>
<a href=#31 id=31 data-nosnippet>31</a><span class="kw">impl </span>UCXLWatcher {
<a href=#32 id=32 data-nosnippet>32</a> <span class="doccomment">/// Creates a new `UCXLWatcher` for the given path.
<a href=#33 id=33 data-nosnippet>33</a> ///
<a href=#34 id=34 data-nosnippet>34</a> /// **What**: Accepts any generic `AsRef&lt;Path&gt;` so callers can pass a `&amp;str`,
<a href=#35 id=35 data-nosnippet>35</a> /// `Path`, or `PathBuf`.
<a href=#36 id=36 data-nosnippet>36</a> ///
<a href=#37 id=37 data-nosnippet>37</a> /// **How**: The provided path is converted to a `PathBuf` and stored.
<a href=#38 id=38 data-nosnippet>38</a> ///
<a href=#39 id=39 data-nosnippet>39</a> /// **Why**: Convenience constructor used throughout CHORUS when a watcher is
<a href=#40 id=40 data-nosnippet>40</a> /// needed for a project directory.
<a href=#41 id=41 data-nosnippet>41</a> </span><span class="kw">pub fn </span>new&lt;P: AsRef&lt;Path&gt;&gt;(path: P) -&gt; <span class="self">Self </span>{
<a href=#42 id=42 data-nosnippet>42</a> <span class="self">Self </span>{
<a href=#43 id=43 data-nosnippet>43</a> base_path: path.as_ref().to_path_buf(),
<a href=#44 id=44 data-nosnippet>44</a> }
<a href=#45 id=45 data-nosnippet>45</a> }
<a href=#46 id=46 data-nosnippet>46</a>
<a href=#47 id=47 data-nosnippet>47</a> <span class="doccomment">/// Starts the watch loop, blocking indefinitely while handling events.
<a href=#48 id=48 data-nosnippet>48</a> ///
<a href=#49 id=49 data-nosnippet>49</a> /// **What**: Sets up a channel, creates a `RecommendedWatcher`, and begins
<a href=#50 id=50 data-nosnippet>50</a> /// watching the `base_path` recursively. For each incoming event, it
<a href=#51 id=51 data-nosnippet>51</a> /// attempts to map the filesystem path to a UCXL address and prints a log.
<a href=#52 id=52 data-nosnippet>52</a> ///
<a href=#53 id=53 data-nosnippet>53</a> /// **How**: Uses the `notify` crate's event API. The heuristic address
<a href=#54 id=54 data-nosnippet>54</a> /// format is `ucxl://system:watcher@local:filesystem/#/&lt;relative_path&gt;`.
<a href=#55 id=55 data-nosnippet>55</a> /// It parses this string with `UCXLAddress::from_str` and logs the result.
<a href=#56 id=56 data-nosnippet>56</a> /// Errors from parsing are ignored (they simply aren't printed).
<a href=#57 id=57 data-nosnippet>57</a> ///
<a href=#58 id=58 data-nosnippet>58</a> /// **Why**: Provides a simple, observable bridge between raw filesystem
<a href=#59 id=59 data-nosnippet>59</a> /// changes and the UCXL addressing scheme, allowing other components to react
<a href=#60 id=60 data-nosnippet>60</a> /// to changes using a uniform identifier.
<a href=#61 id=61 data-nosnippet>61</a> </span><span class="kw">pub fn </span>watch_loop(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="prelude-ty">Result</span>&lt;(), Box&lt;<span class="kw">dyn </span>std::error::Error&gt;&gt; {
<a href=#62 id=62 data-nosnippet>62</a> <span class="kw">let </span>(tx, rx) = channel();
<a href=#63 id=63 data-nosnippet>63</a>
<a href=#64 id=64 data-nosnippet>64</a> <span class="kw">let </span><span class="kw-2">mut </span>watcher = RecommendedWatcher::new(tx, Config::default())<span class="question-mark">?</span>;
<a href=#65 id=65 data-nosnippet>65</a> watcher.watch(<span class="kw-2">&amp;</span><span class="self">self</span>.base_path, RecursiveMode::Recursive)<span class="question-mark">?</span>;
<a href=#66 id=66 data-nosnippet>66</a>
<a href=#67 id=67 data-nosnippet>67</a> <span class="macro">println!</span>(<span class="string">"UCXL Watcher started on {:?}"</span>, <span class="self">self</span>.base_path);
<a href=#68 id=68 data-nosnippet>68</a>
<a href=#69 id=69 data-nosnippet>69</a> <span class="kw">for </span>res <span class="kw">in </span>rx {
<a href=#70 id=70 data-nosnippet>70</a> <span class="kw">match </span>res {
<a href=#71 id=71 data-nosnippet>71</a> <span class="prelude-val">Ok</span>(event) =&gt; {
<a href=#72 id=72 data-nosnippet>72</a> <span class="kw">for </span>path <span class="kw">in </span>event.paths {
<a href=#73 id=73 data-nosnippet>73</a> <span class="kw">if let </span><span class="prelude-val">Some</span>(rel_path) = path.strip_prefix(<span class="kw-2">&amp;</span><span class="self">self</span>.base_path).ok() {
<a href=#74 id=74 data-nosnippet>74</a> <span class="kw">let </span>rel_str = rel_path.to_string_lossy();
<a href=#75 id=75 data-nosnippet>75</a> <span class="comment">// Heuristic address mapping: ucxl://system:watcher@local:filesystem/#/path
<a href=#76 id=76 data-nosnippet>76</a> </span><span class="kw">let </span>addr_str = <span class="macro">format!</span>(
<a href=#77 id=77 data-nosnippet>77</a> <span class="string">"ucxl://system:watcher@local:filesystem/#/{}"</span>,
<a href=#78 id=78 data-nosnippet>78</a> rel_str
<a href=#79 id=79 data-nosnippet>79</a> );
<a href=#80 id=80 data-nosnippet>80</a> <span class="kw">if let </span><span class="prelude-val">Ok</span>(addr) = UCXLAddress::from_str(<span class="kw-2">&amp;</span>addr_str) {
<a href=#81 id=81 data-nosnippet>81</a> <span class="macro">println!</span>(<span class="string">"[UCXL EVENT] {:?} -&gt; {}"</span>, event.kind, addr);
<a href=#82 id=82 data-nosnippet>82</a> }
<a href=#83 id=83 data-nosnippet>83</a> }
<a href=#84 id=84 data-nosnippet>84</a> }
<a href=#85 id=85 data-nosnippet>85</a> }
<a href=#86 id=86 data-nosnippet>86</a> <span class="prelude-val">Err</span>(e) =&gt; <span class="macro">println!</span>(<span class="string">"watch error: {:?}"</span>, e),
<a href=#87 id=87 data-nosnippet>87</a> }
<a href=#88 id=88 data-nosnippet>88</a> }
<a href=#89 id=89 data-nosnippet>89</a> <span class="prelude-val">Ok</span>(())
<a href=#90 id=90 data-nosnippet>90</a> }
<a href=#91 id=91 data-nosnippet>91</a>}</code></pre></div></section></main></body></html>