laburnum 1.17.1

An LSP framework for building language servers and compilers, powered by an incremental query tree with content-addressed storage, task-based dataflow, and parallel queries.
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

use {
  crate::{
    Uri,
    fs::errors::{
      FsError,
      Result,
    },
  },
  error_stack::Report,
};

/// Trait that provides Path-like operations for Uris
pub trait UriPath {
  /// Gets the parent Uri, returns None if this Uri has no parent
  fn parent(&self) -> Option<Uri>;

  /// Returns true if this Uri's path ends with the given extension
  fn ends_with(&self, ext: &str) -> bool;

  /// Returns true if this Uri points to what appears to be a file
  fn is_file(&self) -> bool;

  /// Returns true if this Uri points to what appears to be a directory
  fn is_dir(&self) -> bool;

  /// Gets the filename portion of the Uri path
  fn file_name(&self) -> Option<String>;

  /// Gets all path segments as a Vec<String>
  fn path_segments_vec(&self) -> Vec<String>;

  fn normalize(&self) -> Uri;

  /// Create a new Uri by joining this Uri with a path
  fn join_path(&self, path: &str) -> Result<Uri>;

  /// Strips the base Uri from the beginning of this Uri
  fn strip_prefix(&self, base: &Uri) -> Uri;

  /// Converts the Uri to a string with directory separators
  /// replaced by the scope separator `::`
  fn to_scoped_string(&self) -> String;

  fn has_extension(&self, ext: &str) -> bool;

  /// Returns true if this Uri's path starts with the given base Uri's path.
  fn starts_with(&self, base: &Uri) -> bool;
}

impl UriPath for Uri {
  fn parent(&self) -> Option<Uri> {
    let path = self.path_str();
    if path == "/" {
      return None;
    }

    let path = path.trim_end_matches('/');

    if let Some(last_slash) = path.rfind('/') {
      let parent_path = &path[..last_slash + 1];
      self.join(parent_path)
    } else {
      None
    }
  }

  fn ends_with(&self, ext: &str) -> bool {
    self.path_str().ends_with(ext)
  }

  fn is_file(&self) -> bool {
    self
      .path_segments()
      .and_then(|mut segments| segments.next_back())
      .map(|last| last.contains('.'))
      .unwrap_or(false)
  }

  fn is_dir(&self) -> bool {
    !self.is_file()
  }

  fn file_name(&self) -> Option<String> {
    self.path_segments()?.next_back().map(String::from)
  }

  /// Returns a vector of path segments, not including the host.
  fn path_segments_vec(&self) -> Vec<String> {
    let mut segments = Vec::new();
    if let Some(path_segments) = self.path_segments() {
      segments
        .extend(path_segments.map(String::from).filter(|s| !s.is_empty()));
    }
    segments
  }

  fn normalize(&self) -> Uri {
    let path = self.path_str();
    let mut components = Vec::new();

    for component in path.split('/') {
      match component {
        | "" | "." => continue,
        | ".." => {
          components.pop();
        },
        | c => components.push(c),
      }
    }

    let mut normalized = self.clone();
    let normalized_path = format!("/{}", components.join("/"));
    normalized.set_path(&normalized_path);
    normalized
  }

  fn join_path(&self, path: &str) -> Result<Uri> {
    self.join(path).ok_or_else(|| {
      Report::new(FsError::uri_error("Failed to join uri"))
        .attach_printable(format!("Base uri: {self}"))
        .attach_printable(format!("Path: {path}"))
    })
  }

  fn strip_prefix(&self, base: &Uri) -> Uri {
    if let Some(relative) = self.make_relative(base) {
      if relative.is_empty() {
        // This Uri is the same as base, return self
        self.clone()
      } else if relative.contains("..") {
        // The target Uri is not under the base prefix, return original
        self.clone()
      } else {
        // Create a new Uri with the same scheme and authority but with the
        // relative path
        let authority_str = self
          .authority()
          .map(|a| format!("//{}", a.as_str()))
          .unwrap_or_default();
        let new_uri_str =
          format!("{}:{}/{}", self.scheme(), authority_str, relative);
        Uri::parse(&new_uri_str).unwrap_or_else(|_| self.clone())
      }
    } else {
      // Uris are not related, return original
      self.clone()
    }
  }

  fn to_scoped_string(&self) -> String {
    let mut result = self
      .path_segments_vec()
      .into_iter()
      .filter(|s| !s.is_empty() && !s.trim().is_empty())
      .map(|s| s.trim().to_string())
      .collect::<Vec<String>>()
      .join("::");

    if let Some(query) = self.query() {
      result.push('?');
      result.push_str(query.as_str());
    }

    result
  }

  fn has_extension(&self, ext: &str) -> bool {
    self
      .path_segments()
      .and_then(|mut segments| segments.next_back())
      .map(|last| last.ends_with(ext))
      .unwrap_or(false)
  }

  fn starts_with(&self, base: &Uri) -> bool {
    if self.scheme() != base.scheme() {
      return false;
    }

    let self_auth = self.authority().map(|a| a.as_str());
    let base_auth = base.authority().map(|a| a.as_str());
    if self_auth != base_auth {
      return false;
    }

    let base_path = base.path().as_str();
    let self_path = self.path().as_str();

    self_path.starts_with(base_path)
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_uri_parent() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(uri.parent().unwrap().as_str(), "file://localhost/path/to/");
  }

  #[test]
  fn test_uri_ends_with() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert!(uri.ends_with(".txt"));
    assert!(!uri.ends_with(".rs"));
  }

  #[test]
  fn test_is_file_and_dir() {
    let file = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    let dir = Uri::parse("file://localhost/path/to/").unwrap();

    assert!(file.is_file());
    assert!(!file.is_dir());
    assert!(dir.is_dir());
    assert!(!dir.is_file());
  }

  #[test]
  fn test_file_name() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(uri.file_name().unwrap(), "file.txt");
  }

  #[test]
  fn test_path_segments_vec() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(uri.path_segments_vec(), vec!["path", "to", "file.txt"]);
  }

  #[test]
  fn test_normalize() {
    {
      let uri =
        Uri::parse("file://localhost/path/./to/../to/file.txt").unwrap();
      assert_eq!(
        uri.normalize().as_str(),
        "file://localhost/path/to/file.txt"
      );
    }
    {
      let uri = Uri::parse("file://localhost/path/./to/././././../to/file.txt")
        .unwrap();
      assert_eq!(
        uri.normalize().as_str(),
        "file://localhost/path/to/file.txt"
      );
    }
  }

  #[test]
  fn test_join_path() {
    let uri = Uri::parse("file://localhost/path/to/").unwrap();
    let joined = uri.join_path("file.txt").unwrap();
    assert_eq!(joined.as_str(), "file://localhost/path/to/file.txt");
  }

  #[test]
  fn test_strip_prefix() {
    let base = Uri::parse("file://localhost/path/").unwrap();
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(
      uri.strip_prefix(&base).as_str(),
      "file://localhost/to/file.txt"
    );
  }

  #[test]
  fn test_strip_prefix_same_uri() {
    let base = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(
      uri.strip_prefix(&base).as_str(),
      "file://localhost/path/to/file.txt"
    );
  }

  #[test]
  fn test_strip_prefix_unrelated_uris() {
    let base = Uri::parse("file://other/path/").unwrap();
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(
      uri.strip_prefix(&base).as_str(),
      "file://localhost/path/to/file.txt"
    );
  }

  #[test]
  fn test_strip_prefix_different_schemes() {
    let base = Uri::parse("http://path/").unwrap();
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(
      uri.strip_prefix(&base).as_str(),
      "file://localhost/path/to/file.txt"
    );
  }

  #[test]
  fn test_strip_prefix_base_longer_than_uri() {
    let base = Uri::parse("file://localhost/path/to/very/long/").unwrap();
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    // When base is longer than Uri, Uri is not under base prefix, so return
    // original
    assert_eq!(
      uri.strip_prefix(&base).as_str(),
      "file://localhost/path/to/file.txt"
    );
  }

  #[test]
  fn test_to_scoped_string() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert_eq!(uri.to_scoped_string(), "path::to::file.txt");
  }

  #[test]
  fn test_to_scoped_string_with_host() {
    let uri = Uri::parse("file://example.com/path/to/file.txt").unwrap();
    assert_eq!(uri.to_scoped_string(), "path::to::file.txt");
  }

  #[test]
  fn test_to_scoped_string_with_empty_host() {
    let uri = Uri::parse("file:///path/to/file.txt").unwrap();
    assert_eq!(uri.to_scoped_string(), "path::to::file.txt");
  }

  #[test]
  fn test_has_extension() {
    let uri = Uri::parse("file://localhost/path/to/file.txt").unwrap();
    assert!(uri.has_extension(".txt"));
    assert!(!uri.has_extension(".rs"));
  }

  #[test]
  fn test_to_scoped_string_consecutive_slashes() {
    // Test with consecutive slashes in path
    let uri = Uri::parse("file://localhost/path//to///file.txt").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "path::to::file.txt");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_empty_segments() {
    // Test with trailing slashes and empty segments
    let uri = Uri::parse("file://localhost/path/to/dir/").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "path::to::dir");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_root_only() {
    // Test with just root path
    let uri = Uri::parse("file:///").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_single_segment() {
    // Test with single path segment
    let uri = Uri::parse("file://example/single").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "single");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_host_only() {
    // Test with host only, no path
    let uri = Uri::parse("file://example").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_whitespace_segments() {
    // Test with segments containing only whitespace (Uri encoded as %20)
    // Since we can't decode Uri encoding without additional dependencies,
    // we expect the encoded segment to be preserved
    let uri = Uri::parse("file:///path/%20/to/file.txt").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "path::%20::to::file.txt");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_multiple_empty_segments() {
    // Test with multiple consecutive empty segments that could cause ::::
    let uri = Uri::parse("file:///path///to////file.txt").unwrap();
    let scoped = uri.to_scoped_string();
    assert_eq!(scoped, "path::to::file.txt");
    assert!(!scoped.contains(":::"));
  }

  #[test]
  fn test_to_scoped_string_no_consecutive_colons() {
    // Comprehensive test to ensure no consecutive colons are ever produced
    let test_cases = vec![
      "file:///",
      "file:///path",
      "file:///path/",
      "file:///path//",
      "file:///path///",
      "file:///path/to/file.txt",
      "file:///path//to///file.txt",
      "file://host/path/to/file.txt",
      "file://host//path///to////file.txt",
    ];

    for case in test_cases {
      let uri = Uri::parse(case).unwrap();
      let scoped = uri.to_scoped_string();
      // Ensure no more than 2 consecutive colons (which would be ::)
      assert!(
        !scoped.contains(":::"),
        "Uri '{case}' produced scoped string '{scoped}' with consecutive colons",
      );
    }
  }

  #[test]
  fn test_to_scoped_string_with_query() {
    let uri =
      Uri::parse("file://localhost/path/to/file.txt?param=value").unwrap();
    assert_eq!(uri.to_scoped_string(), "path::to::file.txt?param=value");
  }

  #[test]
  fn test_to_scoped_string_with_multiple_query_params() {
    let uri = Uri::parse(
      "file://localhost/path/to/file.txt?param1=value1&param2=value2",
    )
    .unwrap();
    assert_eq!(
      uri.to_scoped_string(),
      "path::to::file.txt?param1=value1&param2=value2"
    );
  }

  #[test]
  fn test_to_scoped_string_with_query_no_path() {
    let uri = Uri::parse("file://localhost?param=value").unwrap();
    assert_eq!(uri.to_scoped_string(), "?param=value");
  }

  #[test]
  fn test_to_scoped_string_with_query_root_path() {
    let uri = Uri::parse("file:////?param=value").unwrap();
    assert_eq!(uri.to_scoped_string(), "?param=value");
  }

  #[test]
  fn test_to_scoped_string_with_empty_query() {
    let uri = Uri::parse("file://localhost/path/to/file.txt?").unwrap();
    assert_eq!(uri.to_scoped_string(), "path::to::file.txt?");
  }

  #[test]
  fn test_starts_with_file_under_base() {
    let base = Uri::parse("file:///project/src/").unwrap();
    let uri = Uri::parse("file:///project/src/utils/helpers.bl").unwrap();
    assert!(uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_same_uri() {
    let uri = Uri::parse("file:///project/src/").unwrap();
    assert!(uri.starts_with(&uri));
  }

  #[test]
  fn test_starts_with_different_path() {
    let base = Uri::parse("file:///project/src/").unwrap();
    let uri = Uri::parse("file:///other/path/file.bl").unwrap();
    assert!(!uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_different_scheme() {
    let base = Uri::parse("https:///project/src/").unwrap();
    let uri = Uri::parse("file:///project/src/file.bl").unwrap();
    assert!(!uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_different_authority() {
    let base = Uri::parse("file://other/project/src/").unwrap();
    let uri = Uri::parse("file://localhost/project/src/file.bl").unwrap();
    assert!(!uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_partial_segment_no_match() {
    // "/project/src" is a prefix string of "/project/srclib", but
    // this should still match since starts_with is string-based on the path.
    // This documents current behaviour — callers should use trailing-slash
    // roots if segment-boundary matching is needed.
    let base = Uri::parse("file:///project/src").unwrap();
    let uri = Uri::parse("file:///project/srclib/file.bl").unwrap();
    assert!(uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_trailing_slash_prevents_partial() {
    let base = Uri::parse("file:///project/src/").unwrap();
    let uri = Uri::parse("file:///project/srclib/file.bl").unwrap();
    assert!(!uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_deeply_nested() {
    let base = Uri::parse("file:///project/").unwrap();
    let uri = Uri::parse("file:///project/a/b/c/d/e.bl").unwrap();
    assert!(uri.starts_with(&base));
  }

  #[test]
  fn test_starts_with_base_longer_than_uri() {
    let base = Uri::parse("file:///project/src/deep/nested/").unwrap();
    let uri = Uri::parse("file:///project/src/").unwrap();
    assert!(!uri.starts_with(&base));
  }
}